2 // System.Data.Common.DbDataAdapter.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
7 // Sureshkumar T <tsureshkumar@novell.com>
8 // Veerapuram Varadhan <vvaradhan@novell.com>
11 // Copyright (C) Tim Coleman, 2002-2003
15 // Copyright (C) 2004, 2009 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Collections;
39 using System.ComponentModel;
41 using System.Reflection;
42 using System.Runtime.InteropServices;
44 namespace System.Data.Common
46 public abstract class DbDataAdapter : DataAdapter, IDbDataAdapter, IDataAdapter, ICloneable
50 public const string DefaultSourceTableName = "Table";
51 const string DefaultSourceColumnName = "Column";
52 CommandBehavior _behavior = CommandBehavior.Default;
54 IDbCommand _selectCommand;
55 IDbCommand _updateCommand;
56 IDbCommand _deleteCommand;
57 IDbCommand _insertCommand;
63 protected DbDataAdapter ()
67 protected DbDataAdapter (DbDataAdapter adapter) : base (adapter)
75 protected internal CommandBehavior FillCommandBehavior {
76 get { return _behavior; }
77 set { _behavior = value; }
80 IDbCommand IDbDataAdapter.SelectCommand {
81 get { return ((DbDataAdapter)this).SelectCommand; }
82 set { ((DbDataAdapter)this).SelectCommand = (DbCommand)value; }
85 IDbCommand IDbDataAdapter.UpdateCommand{
86 get { return ((DbDataAdapter)this).UpdateCommand; }
87 set { ((DbDataAdapter)this).UpdateCommand = (DbCommand)value; }
90 IDbCommand IDbDataAdapter.DeleteCommand{
91 get { return ((DbDataAdapter)this).DeleteCommand; }
92 set { ((DbDataAdapter)this).DeleteCommand = (DbCommand)value; }
95 IDbCommand IDbDataAdapter.InsertCommand{
96 get { return ((DbDataAdapter)this).InsertCommand; }
97 set { ((DbDataAdapter)this).InsertCommand = (DbCommand)value; }
101 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
102 public DbCommand SelectCommand {
104 return (DbCommand) _selectCommand;
105 //return (DbCommand) ((IDbDataAdapter)this).SelectCommand;
108 if (_selectCommand != value) {
109 _selectCommand = value;
110 ((IDbDataAdapter)this).SelectCommand = value;
116 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
117 public DbCommand DeleteCommand {
119 return (DbCommand) _deleteCommand;
120 //return (DbCommand) ((IDbDataAdapter)this).DeleteCommand;
123 if (_deleteCommand != value) {
124 _deleteCommand = value;
125 ((IDbDataAdapter)this).DeleteCommand = value;
131 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
132 public DbCommand InsertCommand {
134 return (DbCommand) _insertCommand;
135 //return (DbCommand) ((IDbDataAdapter)this).InsertCommand;
138 if (_insertCommand != value) {
139 _insertCommand = value;
140 ((IDbDataAdapter)this).InsertCommand = value;
146 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
147 public DbCommand UpdateCommand {
149 return (DbCommand) _updateCommand;
150 //return (DbCommand) ((IDbDataAdapter)this).DeleteCommand;
153 if (_updateCommand != value) {
154 _updateCommand = value;
155 ((IDbDataAdapter)this).UpdateCommand = value;
161 public virtual int UpdateBatchSize {
165 throw new NotSupportedException ();
169 #endregion // Properties
173 protected virtual RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
174 StatementType statementType,
175 DataTableMapping tableMapping)
177 return new RowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
180 protected virtual RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
181 StatementType statementType,
182 DataTableMapping tableMapping)
184 return new RowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
187 protected virtual void OnRowUpdated (RowUpdatedEventArgs value)
189 if (Events ["RowUpdated"] != null) {
190 Delegate [] rowUpdatedList = Events ["RowUpdated"].GetInvocationList ();
191 foreach (Delegate rowUpdated in rowUpdatedList) {
192 MethodInfo rowUpdatedMethod = rowUpdated.Method;
193 rowUpdatedMethod.Invoke (value, null);
198 protected virtual void OnRowUpdating (RowUpdatingEventArgs value)
200 if (Events ["RowUpdating"] != null) {
201 Delegate [] rowUpdatingList = Events ["RowUpdating"].GetInvocationList ();
202 foreach (Delegate rowUpdating in rowUpdatingList) {
203 MethodInfo rowUpdatingMethod = rowUpdating.Method;
204 rowUpdatingMethod.Invoke (value, null);
209 protected override void Dispose (bool disposing)
212 IDbDataAdapter da = (IDbDataAdapter) this;
213 if (da.SelectCommand != null) {
214 da.SelectCommand.Dispose();
215 da.SelectCommand = null;
217 if (da.InsertCommand != null) {
218 da.InsertCommand.Dispose();
219 da.InsertCommand = null;
221 if (da.UpdateCommand != null) {
222 da.UpdateCommand.Dispose();
223 da.UpdateCommand = null;
225 if (da.DeleteCommand != null) {
226 da.DeleteCommand.Dispose();
227 da.DeleteCommand = null;
232 public override int Fill (DataSet dataSet)
234 return Fill (dataSet, 0, 0, DefaultSourceTableName, ((IDbDataAdapter) this).SelectCommand, _behavior);
237 public int Fill (DataTable dataTable)
239 if (dataTable == null)
240 throw new ArgumentNullException ("DataTable");
242 return Fill (dataTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
245 public int Fill (DataSet dataSet, string srcTable)
247 return Fill (dataSet, 0, 0, srcTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
251 protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior)
253 CommandBehavior commandBehavior = behavior;
255 // first see that the connection is not close.
256 if (command.Connection.State == ConnectionState.Closed) {
257 command.Connection.Open ();
258 commandBehavior |= CommandBehavior.CloseConnection;
260 return Fill (dataTable, command.ExecuteReader (commandBehavior));
263 public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable)
265 return this.Fill (dataSet, startRecord, maxRecords, srcTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
269 public int Fill (int startRecord, int maxRecords, params DataTable[] dataTables)
271 throw new NotImplementedException ();
275 protected virtual int Fill (DataTable[] dataTables, int startRecord, int maxRecords, IDbCommand command, CommandBehavior behavior)
277 throw new NotImplementedException ();
280 protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
282 if (command.Connection == null)
283 throw new InvalidOperationException ("Connection state is closed");
285 if (MissingSchemaAction == MissingSchemaAction.AddWithKey)
286 behavior |= CommandBehavior.KeyInfo;
287 CommandBehavior commandBehavior = behavior;
289 if (command.Connection.State == ConnectionState.Closed) {
290 command.Connection.Open ();
291 commandBehavior |= CommandBehavior.CloseConnection;
293 return Fill (dataSet, srcTable, command.ExecuteReader (commandBehavior),
294 startRecord, maxRecords);
298 /// Fills the given datatable using values from reader. if a value
299 /// for a column is null, that will be filled with default value.
301 /// <returns>No. of rows affected </returns>
302 internal static int FillFromReader (DataTable table,
307 LoadOption loadOption
310 if (reader.FieldCount == 0)
313 for (int i = 0; i < start; i++)
317 object [] values = new object [mapping.Length];
318 while (reader.Read () && (length == 0 || counter < length)) {
319 for (int i = 0 ; i < mapping.Length; i++)
320 values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
321 table.BeginLoadData ();
322 table.LoadDataRow (values, loadOption);
323 table.EndLoadData ();
329 internal static int FillFromReader (DataTable table,
334 LoadOption loadOption,
335 FillErrorEventHandler errorHandler)
337 if (reader.FieldCount == 0)
340 for (int i = 0; i < start; i++)
344 object [] values = new object [mapping.Length];
345 while (reader.Read () && (length == 0 || counter < length)) {
346 for (int i = 0 ; i < mapping.Length; i++)
347 values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
348 table.BeginLoadData ();
350 table.LoadDataRow (values, loadOption);
351 } catch (Exception e) {
352 FillErrorEventArgs args = new FillErrorEventArgs (table, values);
354 args.Continue = false;
355 errorHandler (table, args);
356 // if args.Continue is not set to true or if a handler is not set, rethrow the error..
360 table.EndLoadData ();
366 public override DataTable [] FillSchema (DataSet dataSet, SchemaType schemaType)
368 return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, DefaultSourceTableName, _behavior);
371 public DataTable FillSchema (DataTable dataTable, SchemaType schemaType)
373 return FillSchema (dataTable, schemaType, ((IDbDataAdapter) this).SelectCommand, _behavior);
376 public DataTable [] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable)
378 return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, srcTable, _behavior);
381 protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
383 if (dataTable == null)
384 throw new ArgumentNullException ("DataTable");
386 behavior |= CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
387 if (command.Connection.State == ConnectionState.Closed) {
388 command.Connection.Open ();
389 behavior |= CommandBehavior.CloseConnection;
392 IDataReader reader = command.ExecuteReader (behavior);
394 string tableName = SetupSchema (schemaType, dataTable.TableName);
395 if (tableName != null) {
396 // FillSchema should add the KeyInfo unless MissingSchemaAction
397 // is set to Ignore or Error.
398 MissingSchemaAction schemaAction = MissingSchemaAction;
399 if (!(schemaAction == MissingSchemaAction.Ignore ||
400 schemaAction == MissingSchemaAction.Error))
401 schemaAction = MissingSchemaAction.AddWithKey;
403 BuildSchema (reader, dataTable, schemaType, schemaAction,
404 MissingMappingAction, TableMappings);
412 protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
415 throw new ArgumentNullException ("DataSet");
417 behavior |= CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo;
418 if (command.Connection.State == ConnectionState.Closed) {
419 command.Connection.Open ();
420 behavior |= CommandBehavior.CloseConnection;
423 IDataReader reader = command.ExecuteReader (behavior);
424 ArrayList output = new ArrayList ();
425 string tableName = srcTable;
429 // FillSchema should add the KeyInfo unless MissingSchemaAction
430 // is set to Ignore or Error.
431 MissingSchemaAction schemaAction = MissingSchemaAction;
432 if (!(MissingSchemaAction == MissingSchemaAction.Ignore ||
433 MissingSchemaAction == MissingSchemaAction.Error))
434 schemaAction = MissingSchemaAction.AddWithKey;
437 tableName = SetupSchema (schemaType, tableName);
438 if (tableName != null) {
439 if (dataSet.Tables.Contains (tableName))
440 table = dataSet.Tables [tableName];
442 // Do not create schema if MissingSchemAction is set to Ignore
443 if (this.MissingSchemaAction == MissingSchemaAction.Ignore)
445 table = dataSet.Tables.Add (tableName);
448 BuildSchema (reader, table, schemaType, schemaAction,
449 MissingMappingAction, TableMappings);
451 tableName = String.Format ("{0}{1}", srcTable, ++index);
453 }while (reader.NextResult ());
457 return (DataTable []) output.ToArray (typeof (DataTable));
460 [EditorBrowsable (EditorBrowsableState.Advanced)]
461 public override IDataParameter[] GetFillParameters ()
463 IDbCommand selectCmd = ((IDbDataAdapter) this).SelectCommand;
464 IDataParameter[] parameters = new IDataParameter [selectCmd.Parameters.Count];
465 selectCmd.Parameters.CopyTo (parameters, 0);
470 [Obsolete ("use 'protected DbDataAdapter(DbDataAdapter)' ctor")]
471 object ICloneable.Clone ()
473 throw new NotImplementedException ();
476 public int Update (DataRow [] dataRows)
478 if (dataRows == null)
479 throw new ArgumentNullException("dataRows");
481 if (dataRows.Length == 0)
484 if (dataRows [0] == null)
485 throw new ArgumentException("dataRows[0].");
487 DataTable table = dataRows [0].Table;
489 throw new ArgumentException("table is null reference.");
491 // all rows must be in the same table
492 for (int i = 0; i < dataRows.Length; i++) {
493 if (dataRows [i] == null)
494 throw new ArgumentException ("dataRows[" + i + "].");
495 if (dataRows [i].Table != table)
496 throw new ArgumentException(
499 + "] is from a different DataTable than DataRow[0].");
502 // get table mapping for this rows
503 DataTableMapping tableMapping = TableMappings.GetByDataSetTable(table.TableName);
504 if (tableMapping == null) {
505 tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(
509 MissingMappingAction);
510 if (tableMapping != null) {
511 foreach (DataColumn col in table.Columns) {
512 if (tableMapping.ColumnMappings.IndexOf (col.ColumnName) >= 0)
514 DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction (tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction);
515 if (columnMapping == null)
516 columnMapping = new DataColumnMapping (col.ColumnName, col.ColumnName);
517 tableMapping.ColumnMappings.Add (columnMapping);
520 ArrayList cmc = new ArrayList ();
521 foreach (DataColumn col in table.Columns)
522 cmc.Add (new DataColumnMapping (col.ColumnName, col.ColumnName));
524 new DataTableMapping (
527 cmc.ToArray (typeof (DataColumnMapping)) as DataColumnMapping []);
531 DataRow[] copy = table.NewRowArray (dataRows.Length);
532 Array.Copy (dataRows, 0, copy, 0, dataRows.Length);
533 return Update (copy, tableMapping);
536 public override int Update (DataSet dataSet)
538 return Update (dataSet, DefaultSourceTableName);
541 public int Update (DataTable dataTable)
544 int index = TableMappings.IndexOfDataSetTable (dataTable.TableName);
546 throw new ArgumentException ();
547 return Update (dataTable, TableMappings [index]);
549 DataTableMapping tableMapping = TableMappings.GetByDataSetTable (dataTable.TableName);
550 if (tableMapping == null) {
551 tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (
555 MissingMappingAction);
556 if (tableMapping != null) {
557 foreach (DataColumn col in dataTable.Columns) {
558 if (tableMapping.ColumnMappings.IndexOf (col.ColumnName) >= 0)
560 DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction (tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction);
561 if (columnMapping == null)
562 columnMapping = new DataColumnMapping (col.ColumnName, col.ColumnName);
563 tableMapping.ColumnMappings.Add (columnMapping);
566 ArrayList cmc = new ArrayList ();
567 foreach (DataColumn col in dataTable.Columns)
568 cmc.Add (new DataColumnMapping (col.ColumnName, col.ColumnName));
570 new DataTableMapping (
573 cmc.ToArray (typeof (DataColumnMapping)) as DataColumnMapping []);
576 return Update (dataTable, tableMapping);
579 private int Update (DataTable dataTable, DataTableMapping tableMapping)
581 DataRow [] rows = dataTable.NewRowArray(dataTable.Rows.Count);
582 dataTable.Rows.CopyTo (rows, 0);
583 return Update (rows, tableMapping);
586 protected virtual int Update (DataRow [] dataRows, DataTableMapping tableMapping)
589 foreach (DataRow row in dataRows) {
590 StatementType statementType = StatementType.Update;
591 IDbCommand command = null;
592 string commandName = String.Empty;
594 switch (row.RowState) {
595 case DataRowState.Added:
596 statementType = StatementType.Insert;
597 command = ((IDbDataAdapter) this).InsertCommand;
598 commandName = "Insert";
600 case DataRowState.Deleted:
601 statementType = StatementType.Delete;
602 command = ((IDbDataAdapter) this).DeleteCommand;
603 commandName = "Delete";
605 case DataRowState.Modified:
606 statementType = StatementType.Update;
607 command = ((IDbDataAdapter) this).UpdateCommand;
608 commandName = "Update";
610 case DataRowState.Unchanged:
611 case DataRowState.Detached:
615 RowUpdatingEventArgs argsUpdating = CreateRowUpdatingEvent (row, command, statementType, tableMapping);
616 row.RowError = String.Empty;
617 OnRowUpdating (argsUpdating);
618 switch (argsUpdating.Status) {
619 case UpdateStatus.Continue :
620 //continue in update operation
622 case UpdateStatus.ErrorsOccurred :
623 if (argsUpdating.Errors == null)
624 argsUpdating.Errors = ExceptionHelper.RowUpdatedError();
625 row.RowError += argsUpdating.Errors.Message;
626 if (!ContinueUpdateOnError)
627 throw argsUpdating.Errors;
629 case UpdateStatus.SkipAllRemainingRows :
631 case UpdateStatus.SkipCurrentRow :
635 throw ExceptionHelper.InvalidUpdateStatus (argsUpdating.Status);
637 command = argsUpdating.Command;
639 if (command != null) {
640 DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings;
641 foreach (IDataParameter parameter in command.Parameters) {
642 if ((parameter.Direction & ParameterDirection.Input) == 0)
645 DataRowVersion rowVersion = parameter.SourceVersion;
646 // Parameter version is ignored for non-update commands
647 if (statementType == StatementType.Delete)
648 rowVersion = DataRowVersion.Original;
650 string dsColumnName = parameter.SourceColumn;
651 if (columnMappings.Contains(dsColumnName)) {
652 dsColumnName = columnMappings [dsColumnName].DataSetColumn;
653 parameter.Value = row [dsColumnName, rowVersion];
655 parameter.Value = null;
658 DbParameter nullCheckParam = parameter as DbParameter;
660 if (nullCheckParam != null && nullCheckParam.SourceColumnNullMapping) {
661 if (parameter.Value != null && parameter.Value != DBNull.Value)
662 nullCheckParam.Value = 0;
664 nullCheckParam.Value = 1;
665 nullCheckParam = null;
669 } catch (Exception e) {
670 argsUpdating.Errors = e;
671 argsUpdating.Status = UpdateStatus.ErrorsOccurred;
674 IDataReader reader = null;
677 throw ExceptionHelper.UpdateRequiresCommand (commandName);
679 CommandBehavior commandBehavior = CommandBehavior.Default;
680 if (command.Connection.State == ConnectionState.Closed) {
681 command.Connection.Open ();
682 commandBehavior |= CommandBehavior.CloseConnection;
685 // use ExecuteReader because we want to use the commandbehavior parameter.
686 // so the connection will be closed if needed.
687 reader = command.ExecuteReader (commandBehavior);
689 // update the current row, if the update command returns any resultset
690 // ignore other than the first record.
691 DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings;
693 if (command.UpdatedRowSource == UpdateRowSource.Both ||
694 command.UpdatedRowSource == UpdateRowSource.FirstReturnedRecord) {
696 DataTable retSchema = reader.GetSchemaTable ();
697 foreach (DataRow dr in retSchema.Rows) {
698 string columnName = dr ["ColumnName"].ToString ();
699 string dstColumnName = columnName;
700 if (columnMappings != null &&
701 columnMappings.Contains(columnName))
702 dstColumnName = columnMappings [dstColumnName].DataSetColumn;
703 DataColumn dstColumn = row.Table.Columns [dstColumnName];
704 if (dstColumn == null
705 || (dstColumn.Expression != null
706 && dstColumn.Expression.Length > 0))
708 // info from : http://www.error-bank.com/microsoft.public.dotnet.framework.windowsforms.databinding/
709 // _35_hcsyiv0dha.2328@tk2msftngp10.phx.gbl_Thread.aspx
710 // disable readonly for non-expression columns.
711 bool readOnlyState = dstColumn.ReadOnly;
712 dstColumn.ReadOnly = false;
714 row [dstColumnName] = reader [columnName];
716 dstColumn.ReadOnly = readOnlyState;
723 int tmp = reader.RecordsAffected; // records affected is valid only after closing reader
724 // if the execute does not effect any rows we throw an exception.
726 throw new DBConcurrencyException("Concurrency violation: the " +
727 commandName +"Command affected 0 records.", null,
728 new DataRow [] { row });
731 if (command.UpdatedRowSource == UpdateRowSource.Both ||
732 command.UpdatedRowSource == UpdateRowSource.OutputParameters) {
733 // Update output parameters to row values
734 foreach (IDataParameter parameter in command.Parameters) {
735 if (parameter.Direction != ParameterDirection.InputOutput
736 && parameter.Direction != ParameterDirection.Output
737 && parameter.Direction != ParameterDirection.ReturnValue)
740 string dsColumnName = parameter.SourceColumn;
741 if (columnMappings != null &&
742 columnMappings.Contains(parameter.SourceColumn))
743 dsColumnName = columnMappings [parameter.SourceColumn].DataSetColumn;
744 DataColumn dstColumn = row.Table.Columns [dsColumnName];
745 if (dstColumn == null
746 || (dstColumn.Expression != null
747 && dstColumn.Expression.Length > 0))
749 bool readOnlyState = dstColumn.ReadOnly;
750 dstColumn.ReadOnly = false;
752 row [dsColumnName] = parameter.Value;
754 dstColumn.ReadOnly = readOnlyState;
759 RowUpdatedEventArgs updatedArgs = CreateRowUpdatedEvent (row, command, statementType, tableMapping);
760 OnRowUpdated (updatedArgs);
761 switch (updatedArgs.Status) {
762 case UpdateStatus.Continue:
764 case UpdateStatus.ErrorsOccurred:
765 if (updatedArgs.Errors == null)
766 updatedArgs.Errors = ExceptionHelper.RowUpdatedError();
767 row.RowError += updatedArgs.Errors.Message;
768 if (!ContinueUpdateOnError)
769 throw updatedArgs.Errors;
771 case UpdateStatus.SkipCurrentRow:
773 case UpdateStatus.SkipAllRemainingRows:
776 if (!AcceptChangesDuringUpdate)
778 row.AcceptChanges ();
779 } catch (Exception e) {
780 row.RowError = e.Message;
781 if (!ContinueUpdateOnError)
784 if (reader != null && ! reader.IsClosed)
791 public int Update (DataSet dataSet, string srcTable)
793 MissingMappingAction mappingAction = MissingMappingAction;
795 if (mappingAction == MissingMappingAction.Ignore)
796 mappingAction = MissingMappingAction.Error;
798 DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, srcTable, srcTable, mappingAction);
800 DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable];
801 if (dataTable == null)
802 throw new ArgumentException (String.Format ("Missing table {0}",
805 /** Copied from another Update function **/
806 if (tableMapping != null) {
807 foreach (DataColumn col in dataTable.Columns) {
808 if (tableMapping.ColumnMappings.IndexOf (col.ColumnName) >= 0)
810 DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction (tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction);
811 if (columnMapping == null)
812 columnMapping = new DataColumnMapping (col.ColumnName, col.ColumnName);
813 tableMapping.ColumnMappings.Add (columnMapping);
816 ArrayList cmc = new ArrayList ();
817 foreach (DataColumn col in dataTable.Columns)
818 cmc.Add (new DataColumnMapping (col.ColumnName, col.ColumnName));
820 new DataTableMapping (
823 cmc.ToArray (typeof (DataColumnMapping)) as DataColumnMapping []);
825 /**end insert from another update**/
826 return Update (dataTable, tableMapping);
829 // All the batch methods, should be implemented, if supported,
830 // by individual providers
832 protected virtual int AddToBatch (IDbCommand command)
834 throw CreateMethodNotSupportedException ();
837 protected virtual void ClearBatch ()
839 throw CreateMethodNotSupportedException ();
842 protected virtual int ExecuteBatch ()
844 throw CreateMethodNotSupportedException ();
847 protected virtual IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
849 throw CreateMethodNotSupportedException ();
852 protected virtual bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
859 protected virtual void InitializeBatching ()
861 throw CreateMethodNotSupportedException ();
864 protected virtual void TerminateBatching ()
866 throw CreateMethodNotSupportedException ();
869 Exception CreateMethodNotSupportedException ()
871 return new NotSupportedException ("Method is not supported.");
873 #endregion // Methods