コード例 #1
0
 public static void Persist(IDbConnection db, T t)
 {
     t.UpdateDate = DateTime.Now;
     if (t.Id != 0)
     {
         db.Update <T>(t);
     }
     else
     {
         var factory = new SqlServerDbConnectionFactory(db.ConnectionString);
         using (var connection = ProfiledDbConnectionFactory.New(factory, CustomDbProfiler.Current))
         {
             t.CreateDate = t.UpdateDate;
             try
             {
                 db.Insert <T>(t);
             }
             catch (Exception)
             {
                 var commands = CustomDbProfiler.Current.ProfilerContext.GetCommands();
                 Console.WriteLine(commands);
                 throw;
             }
         }
     }
 }
コード例 #2
0
        private async Task CreateRandomQuery()
        {
            StringBuilder sql             = new StringBuilder("SELECT inc, UserId, GroupId, Status, Name, Age FROM Table1 WHERE 1=1 ");
            object        parameterValues = AddWhere(sql);

            var profiler = new CustomDbProfiler();

            using (var connection = ProfiledDbConnectionFactory.New(new SqlServerDbConnectionFactory(_connectionString), profiler))
            {
                connection.Open();
                try
                {
                    Type[] types   = new [] { typeof(IncClass), typeof(UserClass), typeof(GroupClass), typeof(StatusClass), typeof(NameClass) };
                    string splitOn = "UserId, GroupId, Status, Name";
                    var    result  = await connection.QueryAsync(sql.ToString(), map : (objects) =>
                    {
                        var restaurantOrder       = (IncClass)objects[0];
                        restaurantOrder.User      = (UserClass)objects[1];
                        restaurantOrder.Group     = (GroupClass)objects[2];
                        restaurantOrder.StatusObj = (StatusClass)objects[3];
                        restaurantOrder.NameObj   = (NameClass)objects[4];
                        return(restaurantOrder);
                    }, types : types, splitOn : splitOn, param : parameterValues).ConfigureAwait(false);
                }
                catch (SqlException e)
                {
                    e.Data["profile-query-after-dapper"] = profiler.GetCommands();
                    e.AppendExceptionData(sql.ToString(), parameterValues);
                    throw;
                }
            }
        }
コード例 #3
0
        public async Task <IDbConnection> GetOpenedSqlConnectionAsync()
        {
            CustomDbProfiler.Current.ProfilerContext.Reset();

            var dbConnection = ProfiledDbConnectionFactory.New(new SqlServerDbConnectionFactory(_appSettingsService.ConnectionString), CustomDbProfiler.Current);
            await dbConnection.OpenAsync();

            return(dbConnection);
        }
コード例 #4
0
        private T[] GetValues <T>(string field)
        {
            var profiler = new CustomDbProfiler();

            using (var connection = ProfiledDbConnectionFactory.New(new SqlServerDbConnectionFactory(_connectionString), profiler))
            {
                var result = connection.Query <T>($"SELECT DISTINCT {field} FROM Table1");
                return(result.ToArray());
            }
        }
コード例 #5
0
        static void Main(string[] args)
        {
            const string connectionString = @"Server=.\SqlExpress;Integrated Security=True;";

            var profiler = CustomDbProfiler.Current;

            using (var dbConnection = ProfiledDbConnectionFactory.New(new SqlServerDbConnectionFactory(connectionString), profiler))
            {
                dbConnection.Execute("SELECT GETDATE() as ServerTime");
            }

            var commands = profiler.GetCommands();
        }