コード例 #1
0
        public async Task BulkInsertAsync <T>(IDbConnection connection, IEnumerable <T> data, IDbTransaction transaction = null, int batchSize = 0, int bulkCopyTimeout = 30)
        {
            var       type       = typeof(T);
            var       tableName  = SqlMapperExtensions.GetTableName(type);
            DataTable dataTables = data.ToDataTable();

            using (var bulkCopy = new SqlBulkCopy((SqlConnection)connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction))
            {
                bulkCopy.BulkCopyTimeout      = bulkCopyTimeout;
                bulkCopy.BatchSize            = batchSize;
                bulkCopy.DestinationTableName = tableName;
                bulkCopy.ToColumnMapping <T>();
                await bulkCopy.WriteToServerAsync(dataTables);
            }
        }