internal static string[] MapColumnNames(DataColumnMappingCollection mappingCollection, string[] names, MappingDirection direction) { if ((mappingCollection == null) || (names == null)) { return new string[0]; } ArrayList list = new ArrayList(); foreach (string str2 in names) { string sourceColumn; try { if (direction == MappingDirection.DataSetToSource) { sourceColumn = mappingCollection.GetByDataSetColumn(str2).SourceColumn; } else { DataColumnMapping mapping = mappingCollection[str2]; sourceColumn = mapping.DataSetColumn; } } catch (IndexOutOfRangeException) { sourceColumn = str2; } list.Add(sourceColumn); } return (string[]) list.ToArray(typeof(string)); }
public static DataTableMapping CreateDataTableMapping(string sourceTable, string dataSetTable, DataColumnMappingCollection dataColumnMappings) { DataTableMapping dataTableMapping = new DataTableMapping(sourceTable, dataSetTable); foreach (DataColumnMapping dataColumnMapping in dataColumnMappings) dataTableMapping.ColumnMappings.Add(dataColumnMapping); return dataTableMapping; }
public void AddException1() { Assert.Throws<InvalidCastException>(() => { DataColumnMappingCollection c = new DataColumnMappingCollection(); _columnMapCollection.Add(c); }); }
public void GetReady() { cols=new DataColumnMapping[5]; cols[0]=new DataColumnMapping("sourceName","dataSetName"); cols[1]=new DataColumnMapping("sourceID","dataSetID"); cols[2]=new DataColumnMapping("sourceAddress","dataSetAddress"); cols[3]=new DataColumnMapping("sourcePhone","dataSetPhone"); cols[4]=new DataColumnMapping("sourcePIN","dataSetPIN"); columnMapCollection=new DataColumnMappingCollection(); }
public DataColumnMappingCollectionTest() { _cols = new DataColumnMapping[5]; _cols[0] = new DataColumnMapping("sourceName", "dataSetName"); _cols[1] = new DataColumnMapping("sourceID", "dataSetID"); _cols[2] = new DataColumnMapping("sourceAddress", "dataSetAddress"); _cols[3] = new DataColumnMapping("sourcePhone", "dataSetPhone"); _cols[4] = new DataColumnMapping("sourcePIN", "dataSetPIN"); _columnMapCollection = new DataColumnMappingCollection(); }
public int Update(DataSet dataSet, string srcTable) { MissingMappingAction mappingAction = MissingMappingAction; if (mappingAction == MissingMappingAction.Ignore) { mappingAction = MissingMappingAction.Error; } DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, srcTable, srcTable, mappingAction); DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable]; if (dataTable == null) { throw new ArgumentException(String.Format("Missing table {0}", srcTable)); } /** Copied from another Update function **/ if (tableMapping != null) { foreach (DataColumn col in dataTable.Columns) { if (tableMapping.ColumnMappings.IndexOf(col.ColumnName) >= 0) { continue; } DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction); if (columnMapping == null) { columnMapping = new DataColumnMapping(col.ColumnName, col.ColumnName); } tableMapping.ColumnMappings.Add(columnMapping); } } else { ArrayList cmc = new ArrayList(); foreach (DataColumn col in dataTable.Columns) { cmc.Add(new DataColumnMapping(col.ColumnName, col.ColumnName)); } tableMapping = new DataTableMapping( dataTable.TableName, dataTable.TableName, cmc.ToArray(typeof(DataColumnMapping)) as DataColumnMapping []); } /**end insert from another update**/ return(Update(dataTable, tableMapping)); }
[EditorBrowsableAttribute(EditorBrowsableState.Advanced)] // MDAC 69508 static public DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) { if (null != columnMappings) { int index = columnMappings.IndexOf(sourceColumn); if (-1 != index) { #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine("mapping match on SourceColumn \"" + sourceColumn + "\""); } #endif return(columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction)); } } if (ADP.IsEmpty(sourceColumn)) { throw ADP.InvalidSourceColumn("sourceColumn"); } switch (mappingAction) { case MissingMappingAction.Passthrough: #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine("mapping passthrough of SourceColumn \"" + sourceColumn + "\""); } #endif return(DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction)); case MissingMappingAction.Ignore: #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine("mapping filter of SourceColumn \"" + sourceColumn + "\""); } #endif return(null); case MissingMappingAction.Error: #if DEBUG if (AdapterSwitches.DataSchema.TraceError) { Debug.WriteLine("mapping error on SourceColumn \"" + sourceColumn + "\""); } #endif throw ADP.MissingColumnMapping(sourceColumn); } throw ADP.InvalidMissingMappingAction(mappingAction); }
public static DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) { if (null != columnMappings) { int index = columnMappings.IndexOf(sourceColumn); if (-1 != index) { #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine($"mapping match on SourceColumn \"{sourceColumn}\""); } #endif return(columnMappings._items[index]); } } if (string.IsNullOrEmpty(sourceColumn)) { throw ADP.InvalidSourceColumn(nameof(sourceColumn)); } switch (mappingAction) { case MissingMappingAction.Passthrough: #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine($"mapping passthrough of SourceColumn \"{sourceColumn}\""); } #endif return(new DataColumnMapping(sourceColumn, sourceColumn)); case MissingMappingAction.Ignore: #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine($"mapping filter of SourceColumn \"{sourceColumn}\""); } #endif return(null); case MissingMappingAction.Error: #if DEBUG if (AdapterSwitches.DataSchema.TraceError) { Debug.WriteLine($"mapping error on SourceColumn \"{sourceColumn}\""); } #endif throw ADP.MissingColumnMapping(sourceColumn); } throw ADP.InvalidMissingMappingAction(mappingAction); }
public int Update(DataTable dataTable) { /* * int index = TableMappings.IndexOfDataSetTable (dataTable.TableName); * if (index < 0) * throw new ArgumentException (); * return Update (dataTable, TableMappings [index]); */ DataTableMapping tableMapping = TableMappings.GetByDataSetTable(dataTable.TableName); if (tableMapping == null) { tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction( TableMappings, dataTable.TableName, dataTable.TableName, MissingMappingAction); if (tableMapping != null) { foreach (DataColumn col in dataTable.Columns) { if (tableMapping.ColumnMappings.IndexOf(col.ColumnName) >= 0) { continue; } DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction); if (columnMapping == null) { columnMapping = new DataColumnMapping(col.ColumnName, col.ColumnName); } tableMapping.ColumnMappings.Add(columnMapping); } } else { ArrayList cmc = new ArrayList(); foreach (DataColumn col in dataTable.Columns) { cmc.Add(new DataColumnMapping(col.ColumnName, col.ColumnName)); } tableMapping = new DataTableMapping( dataTable.TableName, dataTable.TableName, cmc.ToArray(typeof(DataColumnMapping)) as DataColumnMapping []); } } return(Update(dataTable, tableMapping)); }
object ICloneable.Clone() { DataTableMapping clone = new DataTableMapping(); clone._dataSetTableName = _dataSetTableName; clone._sourceTableName = _sourceTableName; if ((null != _columnMappings) && (0 < ColumnMappings.Count)) { DataColumnMappingCollection parameters = clone.ColumnMappings; foreach (ICloneable parameter in ColumnMappings) { parameters.Add(parameter.Clone()); } } return clone; }
public static DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) { if (columnMappings.Contains(sourceColumn)) { return(columnMappings[sourceColumn]); } if (mappingAction == MissingMappingAction.Ignore) { return(null); } if (mappingAction == MissingMappingAction.Error) { throw new InvalidOperationException(String.Format("Missing SourceColumn mapping for '{0}'", sourceColumn)); } return(new DataColumnMapping(sourceColumn, sourceColumn)); }
object ICloneable.Clone() { DataTableMapping mapping = new DataTableMapping { _dataSetTableName = this._dataSetTableName, _sourceTableName = this._sourceTableName }; if ((this._columnMappings != null) && (0 < this.ColumnMappings.Count)) { DataColumnMappingCollection columnMappings = mapping.ColumnMappings; foreach (ICloneable cloneable in this.ColumnMappings) { columnMappings.Add(cloneable.Clone()); } } return(mapping); }
/// <include file='doc\DataTableMapping.uex' path='docs/doc[@for="DataTableMapping.ICloneable.Clone"]/*' /> /// <internalonly/> object ICloneable.Clone() { DataTableMapping clone = new DataTableMapping(); // MDAC 81448 clone.SourceTable = SourceTable; clone.DataSetTable = DataSetTable; if ((null != this.columnMappings) && (0 < ColumnMappings.Count)) { DataColumnMappingCollection parameters = clone.ColumnMappings; foreach (ICloneable parameter in ColumnMappings) { parameters.Add(parameter.Clone()); } } return(clone); }
public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) { throw new NotImplementedException (); }
/// <summary> /// Creates or Modifies the schema of the given DataTable based on the schema of /// the reader and the arguments passed. /// </summary> internal static int[] BuildSchema(IDataReader reader, DataTable table, SchemaType schemaType, MissingSchemaAction missingSchAction, MissingMappingAction missingMapAction, DataTableMappingCollection dtMapping ) { int readerIndex = 0; // FIXME : this fails if query has fewer columns than a table int[] mapping = new int[table.Columns.Count]; // mapping the reader indexes to the datatable indexes for (int i = 0; i < mapping.Length; i++) { mapping[i] = -1; } ArrayList primaryKey = new ArrayList(); ArrayList sourceColumns = new ArrayList(); bool createPrimaryKey = true; DataTable schemaTable = reader.GetSchemaTable(); DataColumn ColumnNameCol = schemaTable.Columns["ColumnName"]; DataColumn DataTypeCol = schemaTable.Columns["DataType"]; DataColumn IsAutoIncrementCol = schemaTable.Columns["IsAutoIncrement"]; DataColumn AllowDBNullCol = schemaTable.Columns["AllowDBNull"]; DataColumn IsReadOnlyCol = schemaTable.Columns["IsReadOnly"]; DataColumn IsKeyCol = schemaTable.Columns["IsKey"]; DataColumn IsUniqueCol = schemaTable.Columns["IsUnique"]; DataColumn ColumnSizeCol = schemaTable.Columns["ColumnSize"]; foreach (DataRow schemaRow in schemaTable.Rows) { // generate a unique column name in the source table. string sourceColumnName; string realSourceColumnName; if (ColumnNameCol == null || schemaRow.IsNull(ColumnNameCol) || (string)schemaRow [ColumnNameCol] == String.Empty) { sourceColumnName = DefaultSourceColumnName; realSourceColumnName = DefaultSourceColumnName + "1"; } else { sourceColumnName = (string)schemaRow [ColumnNameCol]; realSourceColumnName = sourceColumnName; } for (int i = 1; sourceColumns.Contains(realSourceColumnName); i += 1) { realSourceColumnName = String.Format("{0}{1}", sourceColumnName, i); } sourceColumns.Add(realSourceColumnName); // generate DataSetColumnName from DataTableMapping, if any DataTableMapping tableMapping = null; //FIXME : The sourcetable name shud get passed as a parameter.. int index = dtMapping.IndexOfDataSetTable(table.TableName); string srcTable = (index != -1 ? dtMapping[index].SourceTable : table.TableName); tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(dtMapping, ADP.IsEmpty(srcTable) ? " " : srcTable, table.TableName, missingMapAction); if (tableMapping != null) { table.TableName = tableMapping.DataSetTable; // check to see if the column mapping exists DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, missingMapAction); if (columnMapping != null) { Type columnType = schemaRow[DataTypeCol] as Type; DataColumn col = columnType != null?columnMapping.GetDataColumnBySchemaAction( table, columnType, missingSchAction) : null; if (col != null) { // if the column is not in the table - add it. if (table.Columns.IndexOf(col) == -1) { if (missingSchAction == MissingSchemaAction.Add || missingSchAction == MissingSchemaAction.AddWithKey) { table.Columns.Add(col); } int[] tmp = new int[mapping.Length + 1]; Array.Copy(mapping, 0, tmp, 0, col.Ordinal); Array.Copy(mapping, col.Ordinal, tmp, col.Ordinal + 1, mapping.Length - col.Ordinal); mapping = tmp; } if (missingSchAction == MissingSchemaAction.AddWithKey) { object value = (AllowDBNullCol != null) ? schemaRow[AllowDBNullCol] : null; bool allowDBNull = value is bool?(bool)value : true; value = (IsKeyCol != null) ? schemaRow[IsKeyCol] : null; bool isKey = value is bool?(bool)value : false; value = (IsAutoIncrementCol != null) ? schemaRow[IsAutoIncrementCol] : null; bool isAutoIncrement = value is bool?(bool)value : false; value = (IsReadOnlyCol != null) ? schemaRow[IsReadOnlyCol] : null; bool isReadOnly = value is bool?(bool)value : false; value = (IsUniqueCol != null) ? schemaRow[IsUniqueCol] : null; bool isUnique = value is bool?(bool)value : false; col.AllowDBNull = allowDBNull; // fill woth key info if (isAutoIncrement && DataColumn.CanAutoIncrement(columnType)) { col.AutoIncrement = true; if (!allowDBNull) { col.AllowDBNull = false; } } if (columnType == DbTypes.TypeOfString) { col.MaxLength = (ColumnSizeCol != null) ? (int)schemaRow[ColumnSizeCol] : 0; } if (isReadOnly) { col.ReadOnly = true; } if (!allowDBNull && (!isReadOnly || isKey)) { col.AllowDBNull = false; } if (isUnique && !isKey && !columnType.IsArray) { col.Unique = true; if (!allowDBNull) { col.AllowDBNull = false; } } // This might not be set by all DataProviders bool isHidden = false; if (schemaTable.Columns.Contains("IsHidden")) { value = schemaRow["IsHidden"]; isHidden = ((value is bool) ? (bool)value : false); } if (isKey && !isHidden) { primaryKey.Add(col); if (allowDBNull) { createPrimaryKey = false; } } } // add the ordinal of the column as a key and the index of the column in the datareader as a value. mapping[col.Ordinal] = readerIndex++; } } } } if (primaryKey.Count > 0) { DataColumn[] colKey = (DataColumn[])(primaryKey.ToArray(typeof(DataColumn))); if (createPrimaryKey) { table.PrimaryKey = colKey; } else { UniqueConstraint uConstraint = new UniqueConstraint(colKey); for (int i = 0; i < table.Constraints.Count; i++) { if (table.Constraints[i].Equals(uConstraint)) { uConstraint = null; break; } } if (uConstraint != null) { table.Constraints.Add(uConstraint); } } } return(mapping); }
internal void ConvertTableTypeTo(System.Data.Design.TableType newTableType) { if (newTableType != this.tableType) { IComponentChangeService service = (IComponentChangeService) this.GetService(typeof(IComponentChangeService)); if (service != null) { service.OnComponentChanging(this, null); } try { this.TableType = newTableType; this.mainSource = null; this.sources = null; this.mappings = null; this.provider = string.Empty; this.OnTableTypeChanged(); } finally { if (service != null) { service.OnComponentChanged(this, null, null, null); } } } }
public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) { if (columnMappings.Contains (sourceColumn)) return columnMappings[sourceColumn]; if (mappingAction == MissingMappingAction.Ignore) return null; if (mappingAction == MissingMappingAction.Error) throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn)); return new DataColumnMapping (sourceColumn, sourceColumn); }
public DataColumnMapping?GetColumnMappingBySchemaAction(string sourceColumn, MissingMappingAction mappingAction) { return(DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMappings, sourceColumn, mappingAction)); }
public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) { if (null != columnMappings) { int index = columnMappings.IndexOf(sourceColumn); if (-1 != index) { #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine($"mapping match on SourceColumn \"{sourceColumn}\""); } #endif return columnMappings._items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction); } } if (string.IsNullOrEmpty(sourceColumn)) { throw ADP.InvalidSourceColumn(nameof(sourceColumn)); } switch (mappingAction) { case MissingMappingAction.Passthrough: #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine($"mapping passthrough of SourceColumn \"{sourceColumn}\""); } #endif return DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction); case MissingMappingAction.Ignore: #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine($"mapping filter of SourceColumn \"{sourceColumn}\""); } #endif return null; case MissingMappingAction.Error: #if DEBUG if (AdapterSwitches.DataSchema.TraceError) { Debug.WriteLine($"mapping error on SourceColumn \"{sourceColumn}\""); } #endif throw ADP.MissingColumnMapping(sourceColumn); } throw ADP.InvalidMissingMappingAction(mappingAction); }
public DataTableMapping () { dataSetTable = String.Empty; sourceTable = String.Empty; columnMappings = new DataColumnMappingCollection (); }
public DataColumn?GetDataColumn(string sourceColumn, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type?dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) { return(DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction)); }
public DataTableMapping() { dataSetTable = String.Empty; sourceTable = String.Empty; columnMappings = new DataColumnMappingCollection(); }
public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) { if (columnMappings != null) { int index = columnMappings.IndexOf(sourceColumn); if (-1 != index) { return columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction); } } if (ADP.IsEmpty(sourceColumn)) { throw ADP.InvalidSourceColumn("sourceColumn"); } switch (mappingAction) { case MissingMappingAction.Passthrough: return DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction); case MissingMappingAction.Ignore: return null; case MissingMappingAction.Error: throw ADP.MissingColumnMapping(sourceColumn); } throw ADP.InvalidMissingMappingAction(mappingAction); }
public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) { throw new NotImplementedException(); }
public int Update(DataRow [] dataRows) { if (dataRows == null) { throw new ArgumentNullException("dataRows"); } if (dataRows.Length == 0) { return(0); } if (dataRows [0] == null) { throw new ArgumentException("dataRows[0]."); } DataTable table = dataRows [0].Table; if (table == null) { throw new ArgumentException("table is null reference."); } // all rows must be in the same table for (int i = 0; i < dataRows.Length; i++) { if (dataRows [i] == null) { throw new ArgumentException("dataRows[" + i + "]."); } if (dataRows [i].Table != table) { throw new ArgumentException( " DataRow[" + i + "] is from a different DataTable than DataRow[0]."); } } // get table mapping for this rows DataTableMapping tableMapping = TableMappings.GetByDataSetTable(table.TableName); if (tableMapping == null) { tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction( TableMappings, table.TableName, table.TableName, MissingMappingAction); if (tableMapping != null) { foreach (DataColumn col in table.Columns) { if (tableMapping.ColumnMappings.IndexOf(col.ColumnName) >= 0) { continue; } DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction); if (columnMapping == null) { columnMapping = new DataColumnMapping(col.ColumnName, col.ColumnName); } tableMapping.ColumnMappings.Add(columnMapping); } } else { ArrayList cmc = new ArrayList(); foreach (DataColumn col in table.Columns) { cmc.Add(new DataColumnMapping(col.ColumnName, col.ColumnName)); } tableMapping = new DataTableMapping( table.TableName, table.TableName, cmc.ToArray(typeof(DataColumnMapping)) as DataColumnMapping []); } } DataRow[] copy = table.NewRowArray(dataRows.Length); Array.Copy(dataRows, 0, copy, 0, dataRows.Length); return(Update(copy, tableMapping)); }
public DesignTable(System.Data.DataTable dataTable, System.Data.Design.TableType tableType, DataColumnMappingCollection mappings) : this(dataTable, tableType) { this.mappings = mappings; }
public DataColumn? GetDataColumn(string sourceColumn, Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) { return DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction); }
public void AddException1() { DataColumnMappingCollection c=new DataColumnMappingCollection(); columnMapCollection.Add((Object)c); }
[ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508 static public DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) { if (null != columnMappings) { int index = columnMappings.IndexOf(sourceColumn); if (-1 != index) { #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine("mapping match on SourceColumn \"" + sourceColumn + "\""); } #endif return columnMappings.items[index]; } } if (ADP.IsEmpty(sourceColumn)) { throw ADP.InvalidSourceColumn("sourceColumn"); } switch (mappingAction) { case MissingMappingAction.Passthrough: #if DEBUG if (AdapterSwitches.DataSchema.TraceInfo) { Debug.WriteLine("mapping passthrough of SourceColumn \"" + sourceColumn + "\""); } #endif return new DataColumnMapping(sourceColumn, sourceColumn); case MissingMappingAction.Ignore: #if DEBUG if (AdapterSwitches.DataSchema.TraceWarning) { Debug.WriteLine("mapping filter of SourceColumn \"" + sourceColumn + "\""); } #endif return null; case MissingMappingAction.Error: #if DEBUG if (AdapterSwitches.DataSchema.TraceError) { Debug.WriteLine("mapping error on SourceColumn \"" + sourceColumn + "\""); } #endif throw ADP.MissingColumnMapping(sourceColumn); } throw ADP.InvalidMissingMappingAction(mappingAction); }
protected virtual int Update(DataRow [] dataRows, DataTableMapping tableMapping) { int updateCount = 0; foreach (DataRow row in dataRows) { StatementType statementType = StatementType.Update; IDbCommand command = null; string commandName = String.Empty; switch (row.RowState) { case DataRowState.Added: statementType = StatementType.Insert; command = ((IDbDataAdapter)this).InsertCommand; commandName = "Insert"; break; case DataRowState.Deleted: statementType = StatementType.Delete; command = ((IDbDataAdapter)this).DeleteCommand; commandName = "Delete"; break; case DataRowState.Modified: statementType = StatementType.Update; command = ((IDbDataAdapter)this).UpdateCommand; commandName = "Update"; break; case DataRowState.Unchanged: case DataRowState.Detached: continue; } RowUpdatingEventArgs argsUpdating = CreateRowUpdatingEvent(row, command, statementType, tableMapping); row.RowError = null; OnRowUpdating(argsUpdating); switch (argsUpdating.Status) { case UpdateStatus.Continue: //continue in update operation break; case UpdateStatus.ErrorsOccurred: if (argsUpdating.Errors == null) { argsUpdating.Errors = ExceptionHelper.RowUpdatedError(); } row.RowError += argsUpdating.Errors.Message; if (!ContinueUpdateOnError) { throw argsUpdating.Errors; } continue; case UpdateStatus.SkipAllRemainingRows: return(updateCount); case UpdateStatus.SkipCurrentRow: updateCount++; continue; default: throw ExceptionHelper.InvalidUpdateStatus(argsUpdating.Status); } command = argsUpdating.Command; try { if (command != null) { DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings; #if ONLY_1_1 IDataParameter nullCheckParam = null; #endif foreach (IDataParameter parameter in command.Parameters) { if ((parameter.Direction & ParameterDirection.Input) == 0) { continue; } DataRowVersion rowVersion = parameter.SourceVersion; // Parameter version is ignored for non-update commands if (statementType == StatementType.Delete) { rowVersion = DataRowVersion.Original; } string dsColumnName = parameter.SourceColumn; #if NET_2_0 if (columnMappings.Contains(dsColumnName)) { dsColumnName = columnMappings [dsColumnName].DataSetColumn; } DbParameter nullCheckParam = parameter as DbParameter; #else if (columnMappings.Contains(dsColumnName)) { dsColumnName = columnMappings [dsColumnName].DataSetColumn; } if (dsColumnName == null || dsColumnName.Length == 0) { nullCheckParam = parameter; continue; } #endif // Fetch columnName from column-mapping if found one, // otherwise, use the source column name to fetch parameter value. parameter.Value = row [dsColumnName, rowVersion]; #if NET_2_0 if (nullCheckParam != null && nullCheckParam.SourceColumnNullMapping) { #else if (nullCheckParam != null) { #endif if (parameter.Value != null && parameter.Value != DBNull.Value) { nullCheckParam.Value = 0; } else { nullCheckParam.Value = 1; } nullCheckParam = null; } } } } catch (Exception e) { argsUpdating.Errors = e; argsUpdating.Status = UpdateStatus.ErrorsOccurred; } IDataReader reader = null; try { if (command == null) { throw ExceptionHelper.UpdateRequiresCommand(commandName); } CommandBehavior commandBehavior = CommandBehavior.Default; if (command.Connection.State == ConnectionState.Closed) { command.Connection.Open(); commandBehavior |= CommandBehavior.CloseConnection; } // use ExecuteReader because we want to use the commandbehavior parameter. // so the connection will be closed if needed. reader = command.ExecuteReader(commandBehavior); // update the current row, if the update command returns any resultset // ignore other than the first record. DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings; if (command.UpdatedRowSource == UpdateRowSource.Both || command.UpdatedRowSource == UpdateRowSource.FirstReturnedRecord) { if (reader.Read()) { DataTable retSchema = reader.GetSchemaTable(); foreach (DataRow dr in retSchema.Rows) { string columnName = dr ["ColumnName"].ToString(); string dstColumnName = columnName; if (columnMappings != null && columnMappings.Contains(columnName)) { dstColumnName = columnMappings [dstColumnName].DataSetColumn; } DataColumn dstColumn = row.Table.Columns [dstColumnName]; if (dstColumn == null || (dstColumn.Expression != null && dstColumn.Expression.Length > 0)) { continue; } // info from : http://www.error-bank.com/microsoft.public.dotnet.framework.windowsforms.databinding/ // [email protected]_Thread.aspx // disable readonly for non-expression columns. bool readOnlyState = dstColumn.ReadOnly; dstColumn.ReadOnly = false; try { row [dstColumnName] = reader [columnName]; } finally { dstColumn.ReadOnly = readOnlyState; } } } } reader.Close(); int tmp = reader.RecordsAffected; // records affected is valid only after closing reader // if the execute does not effect any rows we throw an exception. if (tmp == 0) { throw new DBConcurrencyException("Concurrency violation: the " + commandName + "Command affected 0 records.", null, new DataRow [] { row }); } updateCount += tmp; if (command.UpdatedRowSource == UpdateRowSource.Both || command.UpdatedRowSource == UpdateRowSource.OutputParameters) { // Update output parameters to row values foreach (IDataParameter parameter in command.Parameters) { if (parameter.Direction != ParameterDirection.InputOutput && parameter.Direction != ParameterDirection.Output && parameter.Direction != ParameterDirection.ReturnValue) { continue; } string dsColumnName = parameter.SourceColumn; if (columnMappings != null && columnMappings.Contains(parameter.SourceColumn)) { dsColumnName = columnMappings [parameter.SourceColumn].DataSetColumn; } DataColumn dstColumn = row.Table.Columns [dsColumnName]; if (dstColumn == null || (dstColumn.Expression != null && dstColumn.Expression.Length > 0)) { continue; } bool readOnlyState = dstColumn.ReadOnly; dstColumn.ReadOnly = false; try { row [dsColumnName] = parameter.Value; } finally { dstColumn.ReadOnly = readOnlyState; } } } RowUpdatedEventArgs updatedArgs = CreateRowUpdatedEvent(row, command, statementType, tableMapping); OnRowUpdated(updatedArgs); switch (updatedArgs.Status) { case UpdateStatus.Continue: break; case UpdateStatus.ErrorsOccurred: if (updatedArgs.Errors == null) { updatedArgs.Errors = ExceptionHelper.RowUpdatedError(); } row.RowError += updatedArgs.Errors.Message; if (!ContinueUpdateOnError) { throw updatedArgs.Errors; } break; case UpdateStatus.SkipCurrentRow: continue; case UpdateStatus.SkipAllRemainingRows: return(updateCount); } #if NET_2_0 if (!AcceptChangesDuringUpdate) { continue; } #endif row.AcceptChanges(); } catch (Exception e) { row.RowError = e.Message; if (!ContinueUpdateOnError) { throw e; } } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } } } return(updateCount); }