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 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); }
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); }