Ejemplo n.º 1
0
 public static void BulkInsert(
     this SqlConnection connection,
     string tableName,
     IReadOnlyCollection <object> source,
     Func <SqlBulkCopyColumnMappingCollection, IMappingInfoProvider> providerBuilder,
     SqlTransaction transaction = null,
     int?batchSize = null,
     int?timeout   = null)
 {
     using (var bulkCopy = CreateSqlBulkCopy(connection, tableName, transaction, batchSize, timeout))
     {
         var provider = providerBuilder(bulkCopy.ColumnMappings);
         using (var reader = new CollectonReader(source, provider))
             bulkCopy.WriteToServer(reader);
     }
 }
Ejemplo n.º 2
0
        public static void BulkInsert <TSource>(
            this SqlConnection connection,
            string tableName,
            IReadOnlyCollection <TSource> source,
            Func <IColumnsMappingConfigurator <TSource>, IColumnsMappingConfigurator <TSource> > columnsMappingSetup,
            SqlTransaction transaction = null,
            int?batchSize = null,
            int?timeout   = null)
            where TSource : class
        {
            using (var bulkCopy = CreateSqlBulkCopy(connection, tableName, transaction, batchSize, timeout))
            {
                var provider = new ManualConfiguredStrictTypePropertyInfoProvider <TSource>(bulkCopy.ColumnMappings);
                columnsMappingSetup(provider);

                using (var reader = new CollectonReader(source, provider))
                    bulkCopy.WriteToServer(reader);
            }
        }
Ejemplo n.º 3
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);
         }
 }
Ejemplo n.º 4
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);
                }
            }
        }
        public static void BulkInsert(
            this SqlConnection connection,
            string tableName,
            IReadOnlyCollection <object> source,
            Func <SqlBulkCopyColumnMappingCollection, IMappingInfoProvider> providerBuilder,
            SqlTransaction transaction = null,
            int?batchSize    = null,
            TimeSpan?timeout = null)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (providerBuilder == null)
            {
                throw new ArgumentNullException(nameof(providerBuilder));
            }

            if (source.Any() == false)
            {
                return;
            }

            using (var bulkCopy = CreateSqlBulkCopy(connection, tableName, transaction, batchSize, timeout))
            {
                var provider = providerBuilder(bulkCopy.ColumnMappings);
                using (var reader = new CollectonReader(source, provider))
                    bulkCopy.WriteToServer(reader);
            }
        }