示例#1
0
 public static void BulkInsert(
     this SqlConnection connection,
     string tableName,
     IReadOnlyCollection <object> source,
     IPropertyInfoProvider provider,
     SqlTransaction transaction = null,
     int?batchSize = null,
     int?timeout   = null)
 {
     using (var bulkCopy = CreateSqlBulkCopy(connection, tableName, transaction, batchSize, timeout))
         using (var reader = new CollectonReader(source, provider))
         {
             for (var i = 0; i < reader.FieldCount; i++)
             {
                 bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(i, reader.GetName(i)));
             }
             bulkCopy.WriteToServer(reader);
         }
 }
示例#2
0
        private static void BulkInsertInternal(SqlConnection connection, string tableName,
                                               IReadOnlyCollection <object> source, IPropertyInfoProvider provider,
                                               int?batchSize = null, int?timeout = null)
        {
            using (var bulkCopy = new SqlBulkCopy(connection))
            {
                bulkCopy.DestinationTableName = tableName;
                bulkCopy.BatchSize            = batchSize ?? 4096;
                bulkCopy.BulkCopyTimeout      = timeout ?? 0;

                using (var reader = new CollectonReader(source, provider))
                {
                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(i, reader.GetName(i)));
                    }
                    bulkCopy.WriteToServer(reader);
                }
            }
        }