Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

SQLBulkCopy con inserimento identità nella tabella di destinazione

Alla fine ho funzionato in questo modo

    using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
                {
                    bulkCopy.BatchSize = (int)DetailLines;
                    bulkCopy.DestinationTableName = "dbo.myTable";
                    bulkCopy.ColumnMappings.Clear();
                    bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName");
                    bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName");
                    bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName");
                    bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName");
                    .
                    .
                    .
                    .
                    bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName");

                    bulkCopy.WriteToServer(datatable);
                }