コード例 #1
0
        /// <summary>
        /// 插入实体列表
        /// </summary>
        public static async Task <int> InsertAsync <T>(this DbConnection connection, IEnumerable <T> objList, DbTransaction transaction = null) where T : BaseEntity
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (objList == null)
            {
                throw new ArgumentNullException(nameof(objList));
            }
            int count = 0;

            using (DbCommand command = connection.CreateCommand())
            {
                if (transaction != null)
                {
                    command.Transaction = transaction;
                }
                string sql = CodeSegmentHelper.GenerateInsertql <T>();
                command.CommandText = sql;
                foreach (var item in objList)
                {
                    command.Parameters.Clear();
                    command.AppendEntityParameters <T>(item);
                    int result = await command.ExecuteNonQueryAsync();

                    count = result + count;
                }
            }
            return(count);
        }
コード例 #2
0
 /// <summary>
 /// 插入实体对象
 /// </summary>
 public static async Task <int> InsertAsync <T>(this DbConnection connection, T obj, DbTransaction transaction = null) where T : BaseEntity
 {
     if (connection == null)
     {
         throw new ArgumentNullException(nameof(connection));
     }
     if (obj == null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     using (DbCommand command = connection.CreateCommand())
     {
         if (transaction != null)
         {
             command.Transaction = transaction;
         }
         string sql = CodeSegmentHelper.GenerateInsertql <T>();
         command.CommandText = sql;
         command.Parameters.Clear();
         command.AppendEntityParameters <T>(obj);
         return(await command.ExecuteNonQueryAsync());
     }
 }