/// <summary> /// Initializes the source table. /// </summary> private void InitSourceTable() { SessionLogger.Information(@"Creating source data table structure for Target ""{0}"" from table ""{1}""", typeof(T).FullName, TargetTableName); TargetTableSource = new DataTable(); var sql = string.Format("SELECT * from [{0}]", TargetTableName); using (var conn = new SqlConnection(ConfigService.ConnectionSettings.ConnectionString)) { conn.Open(); using (var da = new SqlDataAdapter(sql, conn)) { da.FillSchema(TargetTableSource, SchemaType.Source); } conn.Close(); } var idCol = TargetTableSource.Columns["Id"]; if (idCol != null) { // if the table has an "Id" column, assume that it's an IDENTITY column and we're not providing values (i.e.: all of our values are 0 and will fail the unique constraint) TargetTableSource.PrimaryKey = null; idCol.Unique = false; } SessionLogger.Information(@"Source data table structure created for Target ""{0}"" from table ""{1}""", typeof(T).FullName, TargetTableName); }
/// <summary> /// Initializes the bulk copy. /// </summary> private void InitBulkCopy() { ColumnMappings = new List <SqlBulkCopyColumnMapping>(); SessionLogger.Information(@"Building Bulk Insert Map for Target ""{0}"" into table ""{1}""", typeof(T).FullName, TargetTableName); foreach (var col in TargetTableSource.Columns.OfType <DataColumn>()) { if (!col.AutoIncrement) { var mapping = new SqlBulkCopyColumnMapping(col.ColumnName, col.ColumnName); ColumnMappings.Add(mapping); } } SessionLogger.Information(@"Bulk Insert Mapped for Target ""{0}"" into table ""{1}""", typeof(T).FullName, TargetTableName); }