private void ResizeColumnsIfRequired(DataTable toProcess, IDataLoadEventListener listener) { var tbl = _database.ExpectTable(TargetTableName); var typeTranslater = tbl.GetQuerySyntaxHelper().TypeTranslater; //Get the current estimates from the datatype computer Dictionary <string, string> oldTypes = _dataTypeDictionary.ToDictionary(k => k.Key, v => v.Value.GetSqlDBType(typeTranslater), StringComparer.CurrentCultureIgnoreCase); //adjust the computer to //work out the max sizes - expensive bit foreach (DataRow row in toProcess.Rows) { //for each destination column foreach (string col in _dataTypeDictionary.Keys) { //if it appears in the toProcess DataTable if (toProcess.Columns.Contains(col)) { //run the datatype computer over it to compute max lengths _dataTypeDictionary[col].AdjustToCompensateForValue(row[col]); } } } //see if any have changed foreach (DataColumn column in toProcess.Columns) { //get what is required for the current batch and the current type that is configured in the live table string oldSqlType = oldTypes[column.ColumnName]; string newSqlType = _dataTypeDictionary[column.ColumnName].GetSqlDBType(typeTranslater); bool changesMade = false; //if the SQL data type has degraded e.g. varchar(10) to varchar(50) or datetime to varchar(20) if (oldSqlType != newSqlType) { listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Resizing column '" + column + "' from '" + oldSqlType + "' to '" + newSqlType + "'")); var col = tbl.DiscoverColumn(column.ColumnName, _managedConnection.ManagedTransaction); //try changing the Type to the legit type col.DataType.AlterTypeTo(newSqlType, _managedConnection.ManagedTransaction, AlterTimeout); changesMade = true; } if (changesMade) { _bulkcopy.InvalidateTableSchema(); } } }