예제 #1
0
 public static async Task AddEntityAsync <T>(this WindowsDbContext db, T model) where T : ICreateEntity <int>
 {
     model.CreatedBy     = CurrentUser.UserId;
     model.CreatedByName = CurrentUser.UserName;
     model.CreatedOn     = DateTime.Now;
     await db.AddAsync(model);
 }
예제 #2
0
        /// <summary>
        /// 执行sql返回受影响行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="sql"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        private static async Task <int> ExecuteNoQuery(this WindowsDbContext db, string sql, params SqlParameter[] sqlParams)
        {
            DbConnection connection = db.Database.GetDbConnection();
            DbCommand    cmd        = connection.CreateCommand();

            db.Database.OpenConnection();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            if (sqlParams != null)
            {
                cmd.Parameters.AddRange(sqlParams);
            }
            int result = await cmd.ExecuteNonQueryAsync();

            db.Database.CloseConnection();
            return(result);
        }
예제 #3
0
        /// <summary>
        /// 执行sql返回列表数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        private static async Task <IEnumerable <T> > Execute <T>(this WindowsDbContext db, string sql, CommandType type, SqlParameter[] sqlParams) where T : new()
        {
            DbConnection connection = db.Database.GetDbConnection();
            DbCommand    cmd        = connection.CreateCommand();

            db.Database.OpenConnection();
            cmd.CommandText = sql;
            cmd.CommandType = type;
            if (sqlParams != null)
            {
                cmd.Parameters.AddRange(sqlParams);
            }
            DataTable dt = new DataTable();

            using (DbDataReader reader = await cmd.ExecuteReaderAsync())
            {
                dt.Load(reader);
            }
            db.Database.CloseConnection();
            return(dt.ToCollection <T>());
        }
예제 #4
0
 /// <summary>
 /// 执行sql返回IEnumerable数据集
 /// </summary>
 public static async Task <IEnumerable <T> > ExecSqlReader <T>(this WindowsDbContext db, string sql, params SqlParameter[] sqlParams) where T : new()
 {
     return(await Execute <T>(db, sql, CommandType.Text, sqlParams));
 }
예제 #5
0
 /// <summary>
 /// 执行sql返回受影响的行数
 /// </summary>
 public static async Task <int> ExecSqlNoQuery(this WindowsDbContext db, string sql, params SqlParameter[] sqlParams)
 {
     return(await ExecuteNoQuery(db, sql, sqlParams));
 }
예제 #6
0
 public static void  ModifyEntity <T>(this WindowsDbContext db, T model) where T : IUpdateEntity <int>
 {
     model.UpdatedBy     = CurrentUser.UserId;
     model.UpdatedByName = CurrentUser.UserName;
     model.UpdatedOn     = DateTime.Now;
 }