Merge pull request #1542 from ninjarobot/UriTemplateMatchException
[mono.git] / mcs / class / System.Data / System.Data.Common / DbDataAdapter.cs
index 70e67bffe437127a877f9a2db1ab5f63227db9af..918da04f502013c941a25eccf00e34355621c468 100644 (file)
@@ -5,13 +5,14 @@
 //   Rodrigo Moya (rodrigo@ximian.com)
 //   Tim Coleman (tim@timcoleman.com)
 //   Sureshkumar T <tsureshkumar@novell.com>
+//   Veerapuram Varadhan  <vvaradhan@novell.com>
 //
 // (C) Ximian, Inc
 // Copyright (C) Tim Coleman, 2002-2003
 //
 
 //
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2009 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -40,34 +41,30 @@ using System.Data;
 using System.Reflection;
 using System.Runtime.InteropServices;
 
-namespace System.Data.Common {
-#if NET_2_0
+namespace System.Data.Common
+{
        public abstract class DbDataAdapter : DataAdapter, IDbDataAdapter, IDataAdapter, ICloneable
-#else
-       public abstract class DbDataAdapter : DataAdapter, ICloneable
-#endif
        {
                #region Fields
 
                public const string DefaultSourceTableName = "Table";
                const string DefaultSourceColumnName = "Column";
+               CommandBehavior _behavior = CommandBehavior.Default;
 
-#if NET_2_0
                IDbCommand _selectCommand;
                IDbCommand _updateCommand;
                IDbCommand _deleteCommand;
                IDbCommand _insertCommand;
-#endif
 
                #endregion // Fields
                
                #region Constructors
 
-               protected DbDataAdapter() 
+               protected DbDataAdapter ()
                {
                }
 
-               protected DbDataAdapter(DbDataAdapter adapter) : base(adapter)
+               protected DbDataAdapter (DbDataAdapter adapter) : base (adapter)
                {
                }
 
@@ -75,100 +72,104 @@ namespace System.Data.Common {
 
                #region Properties
 
-#if NET_2_0
                protected internal CommandBehavior FillCommandBehavior {
-                       get { throw new NotImplementedException (); }
-                       set { throw new NotImplementedException (); }
+                       get { return _behavior; }
+                       set { _behavior = value; }
                }
 
                IDbCommand IDbDataAdapter.SelectCommand {
-                       get { return _selectCommand; }
-                       set { _selectCommand = value; }
+                   get { return ((DbDataAdapter)this).SelectCommand; }
+                   set { ((DbDataAdapter)this).SelectCommand = (DbCommand)value; }
                }
 
                IDbCommand IDbDataAdapter.UpdateCommand{
-                       get { return _updateCommand; }
-                       set { _updateCommand = value; }
+                   get { return ((DbDataAdapter)this).UpdateCommand; }
+                   set { ((DbDataAdapter)this).UpdateCommand = (DbCommand)value; }
                }
-
+               
                IDbCommand IDbDataAdapter.DeleteCommand{
-                       get { return _deleteCommand; }
-                       set { _deleteCommand = value; }
+                   get { return ((DbDataAdapter)this).DeleteCommand; }
+                   set { ((DbDataAdapter)this).DeleteCommand = (DbCommand)value; }
                }
 
                IDbCommand IDbDataAdapter.InsertCommand{
-                       get { return _insertCommand; }
-                       set { _insertCommand = value; }
+                   get { return ((DbDataAdapter)this).InsertCommand; }
+                   set { ((DbDataAdapter)this).InsertCommand = (DbCommand)value; }
                }
                
                [Browsable (false)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public DbCommand SelectCommand {
-                       get { return (DbCommand) ((IDbDataAdapter) this).SelectCommand; }
-                       set { ((IDbDataAdapter) this).SelectCommand = value; }
+                   get {
+                                       return (DbCommand) _selectCommand;
+                                       //return (DbCommand) ((IDbDataAdapter)this).SelectCommand; 
+                       }
+                   set {
+                                       if (_selectCommand != value) {
+                                               _selectCommand = value;
+                                               ((IDbDataAdapter)this).SelectCommand = value; 
+                                       }
+                       }
                }
 
                [Browsable (false)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public DbCommand DeleteCommand {
-                       get { return (DbCommand) ((IDbDataAdapter) this).DeleteCommand; }
-                       set { ((IDbDataAdapter) this).DeleteCommand = value; }
+                   get {
+                                       return (DbCommand) _deleteCommand;
+                                       //return (DbCommand) ((IDbDataAdapter)this).DeleteCommand; 
+                       }
+                   set {
+                                       if (_deleteCommand != value) {
+                                               _deleteCommand = value;
+                                               ((IDbDataAdapter)this).DeleteCommand = value; 
+                                       }
+                       }
                }
 
                [Browsable (false)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public DbCommand InsertCommand {
-                       get { return (DbCommand) ((IDbDataAdapter) this).InsertCommand; }
-                       set { ((IDbDataAdapter) this).InsertCommand = value; }
+                   get {
+                                       return (DbCommand) _insertCommand;
+                                       //return (DbCommand) ((IDbDataAdapter)this).InsertCommand; 
+                       }
+                   set {
+                                       if (_insertCommand != value) {
+                                               _insertCommand = value;
+                                               ((IDbDataAdapter)this).InsertCommand = value; 
+                                       }
+                       }
                }
 
                [Browsable (false)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public DbCommand UpdateCommand {
-                       get { return (DbCommand) ((IDbDataAdapter) this).UpdateCommand; }
-                       set { ((IDbDataAdapter) this).UpdateCommand = value; }
+                   get {
+                                       return (DbCommand) _updateCommand;
+                                       //return (DbCommand) ((IDbDataAdapter)this).DeleteCommand; 
+                       }
+                   set {
+                                       if (_updateCommand != value) {
+                                               _updateCommand = value;
+                                               ((IDbDataAdapter)this).UpdateCommand = value; 
+                                       }
+                       }
                }
 
-               [MonoTODO]
                [DefaultValue (1)]
                public virtual int UpdateBatchSize {
                        get { return 1; }
-                       set { throw new NotSupportedException (); }
-               }
-#else
-               IDbCommand SelectCommand {
-                       get { return ((IDbDataAdapter) this).SelectCommand; }
-               }
-
-               IDbCommand UpdateCommand {
-                       get { return ((IDbDataAdapter) this).UpdateCommand; }
-               }
-
-               IDbCommand DeleteCommand {
-                       get { return ((IDbDataAdapter) this).DeleteCommand; }
+                       set {
+                               if (value != 1)
+                                       throw new NotSupportedException ();
+                       }
                }
 
-               IDbCommand InsertCommand {
-                       get { return ((IDbDataAdapter) this).InsertCommand; }
-               } 
-#endif
-
                #endregion // Properties
                
-               #region Events
-
-#if ONLY_1_0 || ONLY_1_1
-
-               [DataCategory ("Fill")]
-               [DataSysDescription ("Event triggered when a recoverable error occurs during Fill.")]
-               public event FillErrorEventHandler FillError;
-
-#endif
-               #endregion // Events
-
                #region Methods
 
-#if NET_2_0
                protected virtual RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
                                                                             StatementType statementType,
                                                                             DataTableMapping tableMapping)
@@ -204,18 +205,6 @@ namespace System.Data.Common {
                                }
                        }
                }
-#else
-               protected abstract RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
-                                                                            StatementType statementType,
-                                                                            DataTableMapping tableMapping);
-
-               protected abstract RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
-                                                                              StatementType statementType,
-                                                                              DataTableMapping tableMapping);
-
-               protected abstract void OnRowUpdated (RowUpdatedEventArgs value);
-               protected abstract void OnRowUpdating (RowUpdatingEventArgs value);
-#endif
 
                protected override void Dispose (bool disposing)
                {
@@ -242,50 +231,42 @@ namespace System.Data.Common {
 
                public override int Fill (DataSet dataSet)
                {
-                       return Fill (dataSet, 0, 0, DefaultSourceTableName, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+                       return Fill (dataSet, 0, 0, DefaultSourceTableName, ((IDbDataAdapter) this).SelectCommand, _behavior);
                }
 
-               public int Fill (DataTable dataTable) 
+               public int Fill (DataTable dataTable)
                {
                        if (dataTable == null)
                                throw new ArgumentNullException ("DataTable");
 
-                       return Fill (dataTable, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+                       return Fill (dataTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
                }
 
-               public int Fill (DataSet dataSet, string srcTable) 
+               public int Fill (DataSet dataSet, string srcTable)
                {
-                       return Fill (dataSet, 0, 0, srcTable, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+                       return Fill (dataSet, 0, 0, srcTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
                }
 
-#if !NET_2_0
-               protected virtual int Fill (DataTable dataTable, IDataReader dataReader) 
-               {
-                       return base.FillInternal (dataTable, dataReader);
-               }
-#endif
 
-               protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior) 
+               protected virtual int Fill (DataTable dataTable, IDbCommand command, CommandBehavior behavior)
                {
                        CommandBehavior commandBehavior = behavior;
 
                        // first see that the connection is not close.
-                       if (command.Connection.State == ConnectionState.Closed) 
-                       {
+                       if (command.Connection.State == ConnectionState.Closed) {
                                command.Connection.Open ();
                                commandBehavior |= CommandBehavior.CloseConnection;
                        }
                        return Fill (dataTable, command.ExecuteReader (commandBehavior));
                }
 
-               public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable) 
+               public int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable)
                {
-                       return this.Fill (dataSet, startRecord, maxRecords, srcTable, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+                       return this.Fill (dataSet, startRecord, maxRecords, srcTable, ((IDbDataAdapter) this).SelectCommand, _behavior);
                }
 
-#if NET_2_0
                [MonoTODO]
-               public int Fill (int startRecord, int maxRecords, DataTable[] dataTables)
+               public int Fill (int startRecord, int maxRecords, params DataTable[] dataTables)
                {
                        throw new NotImplementedException ();
                }
@@ -295,21 +276,12 @@ namespace System.Data.Common {
                {
                        throw new NotImplementedException ();
                }
-#endif
-               
-               
-#if !NET_2_0
-               protected virtual int Fill (DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords) 
-               {
-                       return base.FillInternal (dataSet, srcTable, dataReader, startRecord, maxRecords);
-               }
-#endif
-               
-               protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior) 
+
+               protected virtual int Fill (DataSet dataSet, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior)
                {
-                       if (command.Connection == null) {
+                       if (command.Connection == null)
                                throw new InvalidOperationException ("Connection state is closed");
-                       }
+
                        if (MissingSchemaAction == MissingSchemaAction.AddWithKey)
                                behavior |= CommandBehavior.KeyInfo;
                        CommandBehavior commandBehavior = behavior;
@@ -318,68 +290,62 @@ namespace System.Data.Common {
                                command.Connection.Open ();
                                commandBehavior |= CommandBehavior.CloseConnection;
                        }
-                       return Fill (dataSet, srcTable, command.ExecuteReader (commandBehavior), startRecord, maxRecords);
+                       return Fill (dataSet, srcTable, command.ExecuteReader (commandBehavior),
+                               startRecord, maxRecords);
                }
 
-#if NET_2_0
                /// <summary>
-               ///     Fills the given datatable using values from reader. if a value 
-               ///     for a column is  null, that will be filled with default value. 
+               /// Fills the given datatable using values from reader. if a value 
+               /// for a column is  null, that will be filled with default value. 
                /// </summary>
                /// <returns>No. of rows affected </returns>
                internal static int FillFromReader (DataTable table,
                                                     IDataReader reader,
-                                                    int start, 
+                                                    int start,
                                                     int length,
                                                     int [] mapping,
                                                     LoadOption loadOption
                                                     )
-                {
-                        if (reader.FieldCount == 0)
+               {
+                       if (reader.FieldCount == 0)
                                return 0 ;
 
-                        for (int i = 0; i < start; i++)
-                                reader.Read ();
-
-                        int counter = 0;
-                        object [] values = new object [mapping.Length];
-                        while (reader.Read () &&
-                               (length == 0 || counter < length)) {
-                                
-                                for (int i = 0 ; i < mapping.Length; i++)
-                                        values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
-                                        
-                                table.BeginLoadData ();
-                                table.LoadDataRow (values, loadOption);
-                                table.EndLoadData ();
-                                counter++;
-                        }
-                        return counter;
-                }
+                       for (int i = 0; i < start; i++)
+                               reader.Read ();
+
+                       int counter = 0;
+                       object [] values = new object [mapping.Length];
+                       while (reader.Read () && (length == 0 || counter < length)) {
+                               for (int i = 0 ; i < mapping.Length; i++)
+                                       values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
+                               table.BeginLoadData ();
+                               table.LoadDataRow (values, loadOption);
+                               table.EndLoadData ();
+                               counter++;
+                       }
+                       return counter;
+               }
 
                internal static int FillFromReader (DataTable table,
                                                     IDataReader reader,
-                                                    int start, 
+                                                    int start,
                                                     int length,
                                                     int [] mapping,
                                                     LoadOption loadOption,
                                                     FillErrorEventHandler errorHandler)
-                {
-                        if (reader.FieldCount == 0)
+               {
+                       if (reader.FieldCount == 0)
                                return 0 ;
 
-                        for (int i = 0; i < start; i++)
-                                reader.Read ();
-
-                        int counter = 0;
-                        object [] values = new object [mapping.Length];
-                        while (reader.Read () &&
-                               (length == 0 || counter < length)) {
-                                
-                                for (int i = 0 ; i < mapping.Length; i++)
-                                        values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
-                                        
-                                table.BeginLoadData ();
+                       for (int i = 0; i < start; i++)
+                               reader.Read ();
+
+                       int counter = 0;
+                       object [] values = new object [mapping.Length];
+                       while (reader.Read () && (length == 0 || counter < length)) {
+                               for (int i = 0 ; i < mapping.Length; i++)
+                                       values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
+                               table.BeginLoadData ();
                                try {
                                        table.LoadDataRow (values, loadOption);
                                } catch (Exception e) {
@@ -391,30 +357,28 @@ namespace System.Data.Common {
                                        if(!args.Continue)
                                                throw e;
                                }
-                                table.EndLoadData ();
-                                counter++;
-                        }
-                        return counter;
-                }
-
-#endif // NET_2_0
+                               table.EndLoadData ();
+                               counter++;
+                       }
+                       return counter;
+               }
 
-               public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType) 
+               public override DataTable [] FillSchema (DataSet dataSet, SchemaType schemaType)
                {
-                       return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, DefaultSourceTableName, CommandBehavior.Default);
+                       return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, DefaultSourceTableName, _behavior);
                }
 
-               public DataTable FillSchema (DataTable dataTable, SchemaType schemaType) 
+               public DataTable FillSchema (DataTable dataTable, SchemaType schemaType)
                {
-                       return FillSchema (dataTable, schemaType, ((IDbDataAdapter) this).SelectCommand, CommandBehavior.Default);
+                       return FillSchema (dataTable, schemaType, ((IDbDataAdapter) this).SelectCommand, _behavior);
                }
 
-               public DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable) 
+               public DataTable [] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable)
                {
-                       return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, srcTable, CommandBehavior.Default);
+                       return FillSchema (dataSet, schemaType, ((IDbDataAdapter) this).SelectCommand, srcTable, _behavior);
                }
 
-               protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior) 
+               protected virtual DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDbCommand command, CommandBehavior behavior)
                {
                        if (dataTable == null)
                                throw new ArgumentNullException ("DataTable");
@@ -426,11 +390,9 @@ namespace System.Data.Common {
                        }
 
                        IDataReader reader = command.ExecuteReader (behavior);
-                       try
-                       {
+                       try {
                                string tableName =  SetupSchema (schemaType, dataTable.TableName);
-                               if (tableName != null)
-                               {
+                               if (tableName != null) {
                                        // FillSchema should add the KeyInfo unless MissingSchemaAction
                                        // is set to Ignore or Error.
                                        MissingSchemaAction schemaAction = MissingSchemaAction;
@@ -441,15 +403,13 @@ namespace System.Data.Common {
                                        BuildSchema (reader, dataTable, schemaType, schemaAction,
                                                MissingMappingAction, TableMappings);
                                }
-                       }
-                       finally
-                       {
+                       } finally {
                                reader.Close ();
                        }
                        return dataTable;
                }
 
-               protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior) 
+               protected virtual DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, IDbCommand command, string srcTable, CommandBehavior behavior)
                {
                        if (dataSet == null)
                                throw new ArgumentNullException ("DataSet");
@@ -465,8 +425,7 @@ namespace System.Data.Common {
                        string tableName = srcTable;
                        int index = 0;
                        DataTable table;
-                       try
-                       {
+                       try {
                                // FillSchema should add the KeyInfo unless MissingSchemaAction
                                // is set to Ignore or Error.
                                MissingSchemaAction schemaAction = MissingSchemaAction;
@@ -476,12 +435,10 @@ namespace System.Data.Common {
 
                                do {
                                        tableName = SetupSchema (schemaType, tableName);
-                                       if (tableName != null)
-                                       {
+                                       if (tableName != null) {
                                                if (dataSet.Tables.Contains (tableName))
-                                                       table = dataSet.Tables [tableName];     
-                                               else
-                                               {
+                                                       table = dataSet.Tables [tableName];
+                                               else {
                                                        // Do not create schema if MissingSchemAction is set to Ignore
                                                        if (this.MissingSchemaAction == MissingSchemaAction.Ignore)
                                                                continue;
@@ -494,43 +451,29 @@ namespace System.Data.Common {
                                                tableName = String.Format ("{0}{1}", srcTable, ++index);
                                        }
                                }while (reader.NextResult ());
-                       }
-                       finally
-                       {
+                       } finally {
                                reader.Close ();
                        }
-                       return (DataTable[]) output.ToArray (typeof (DataTable));
-               }
-
-#if NET_2_0
-               [MonoTODO]
-               protected override DataTable FillSchema (DataTable dataTable, SchemaType schemaType, IDataReader dataReader)
-               {
-                       throw new NotImplementedException ();
-               }
-
-               [MonoTODO]
-               protected override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType, string srcTable, IDataReader dataReader)
-               {
-                       throw new NotImplementedException ();
+                       return (DataTable []) output.ToArray (typeof (DataTable));
                }
-#endif
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
-               public override IDataParameter[] GetFillParameters () 
+               public override IDataParameter[] GetFillParameters ()
                {
-                       IDataParameter[] parameters = new IDataParameter [SelectCommand.Parameters.Count];
-                       SelectCommand.Parameters.CopyTo (parameters, 0);
+                       IDbCommand selectCmd = ((IDbDataAdapter) this).SelectCommand;
+                       IDataParameter[] parameters = new IDataParameter [selectCmd.Parameters.Count];
+                       selectCmd.Parameters.CopyTo (parameters, 0);
                        return parameters;
                }
                
                [MonoTODO]
+               [Obsolete ("use 'protected DbDataAdapter(DbDataAdapter)' ctor")]
                object ICloneable.Clone ()
                {
                        throw new NotImplementedException ();
                }
 
-               public int Update (DataRow[] dataRows) 
+               public int Update (DataRow [] dataRows)
                {
                        if (dataRows == null)
                                throw new ArgumentNullException("dataRows");
@@ -538,19 +481,18 @@ namespace System.Data.Common {
                        if (dataRows.Length == 0)
                                return 0;
 
-                       if (dataRows[0] == null)
+                       if (dataRows [0] == null)
                                throw new ArgumentException("dataRows[0].");
 
-                       DataTable table = dataRows[0].Table;
+                       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)
+                       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
@@ -559,8 +501,7 @@ namespace System.Data.Common {
                        
                        // get table mapping for this rows
                        DataTableMapping tableMapping = TableMappings.GetByDataSetTable(table.TableName);
-                       if (tableMapping == null)
-                       {
+                       if (tableMapping == null) {
                                tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(
                                                                                                        TableMappings,
                                                                                                        table.TableName,
@@ -587,17 +528,17 @@ namespace System.Data.Common {
                                }
                        }
 
-                       DataRow[] copy = table.NewRowArray(dataRows.Length);
-                       Array.Copy(dataRows, 0, copy, 0, dataRows.Length);
-                       return Update(copy, tableMapping);
+                       DataRow[] copy = table.NewRowArray (dataRows.Length);
+                       Array.Copy (dataRows, 0, copy, 0, dataRows.Length);
+                       return Update (copy, tableMapping);
                }
 
-               public override int Update (DataSet dataSet) 
+               public override int Update (DataSet dataSet)
                {
                        return Update (dataSet, DefaultSourceTableName);
                }
 
-               public int Update (DataTable dataTable) 
+               public int Update (DataTable dataTable)
                {
                        /*
                          int index = TableMappings.IndexOfDataSetTable (dataTable.TableName);
@@ -606,8 +547,7 @@ namespace System.Data.Common {
                          return Update (dataTable, TableMappings [index]);
                        */
                        DataTableMapping tableMapping = TableMappings.GetByDataSetTable (dataTable.TableName);
-                       if (tableMapping == null)
-                       {
+                       if (tableMapping == null) {
                                tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (
                                                                                                         TableMappings,
                                                                                                         dataTable.TableName,
@@ -638,12 +578,12 @@ namespace System.Data.Common {
 
                private int Update (DataTable dataTable, DataTableMapping tableMapping)
                {
-                       DataRow[] rows = dataTable.NewRowArray(dataTable.Rows.Count);
+                       DataRow [] rows = dataTable.NewRowArray(dataTable.Rows.Count);
                        dataTable.Rows.CopyTo (rows, 0);
                        return Update (rows, tableMapping);
                }
 
-               protected virtual int Update (DataRow[] dataRows, DataTableMapping tableMapping) 
+               protected virtual int Update (DataRow [] dataRows, DataTableMapping tableMapping)
                {
                        int updateCount = 0;
                        foreach (DataRow row in dataRows) {
@@ -673,20 +613,18 @@ namespace System.Data.Common {
                                }
 
                                RowUpdatingEventArgs argsUpdating = CreateRowUpdatingEvent (row, command, statementType, tableMapping);
-                               row.RowError = null;
+                               row.RowError = String.Empty;
                                OnRowUpdating (argsUpdating);
                                switch (argsUpdating.Status) {
                                case UpdateStatus.Continue :
                                        //continue in update operation
                                        break;
                                case UpdateStatus.ErrorsOccurred :
-                                       if (argsUpdating.Errors == null) {
+                                       if (argsUpdating.Errors == null)
                                                argsUpdating.Errors = ExceptionHelper.RowUpdatedError();
-                                       }
                                        row.RowError += argsUpdating.Errors.Message;
-                                       if (!ContinueUpdateOnError) {
+                                       if (!ContinueUpdateOnError)
                                                throw argsUpdating.Errors;
-                                       }
                                        continue;
                                case UpdateStatus.SkipAllRemainingRows :
                                        return updateCount;
@@ -700,43 +638,43 @@ namespace System.Data.Common {
                                try {
                                        if (command != null) {
                                                DataColumnMappingCollection columnMappings = tableMapping.ColumnMappings;
-                                               IDataParameter nullCheckParam = null;
                                                foreach (IDataParameter parameter in command.Parameters) {
-                                                       if ((parameter.Direction & ParameterDirection.Input) != 0) {
-                                                               string dsColumnName = parameter.SourceColumn;
-                                                               if (columnMappings.Contains(parameter.SourceColumn))
-                                                                       dsColumnName = columnMappings [parameter.SourceColumn].DataSetColumn;
-                                                               if (dsColumnName == null || dsColumnName.Length <= 0) {
-                                                                       nullCheckParam = parameter;
-                                                                       continue;
-                                                               }
+                                                       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;
+                                                       DataRowVersion rowVersion = parameter.SourceVersion;
+                                                       // Parameter version is ignored for non-update commands
+                                                       if (statementType == StatementType.Delete)
+                                                               rowVersion = DataRowVersion.Original;
 
+                                                       string dsColumnName = parameter.SourceColumn;
+                                                       if (columnMappings.Contains(dsColumnName)) {
+                                                               dsColumnName = columnMappings [dsColumnName].DataSetColumn;
                                                                parameter.Value = row [dsColumnName, rowVersion];
-                                                               if (nullCheckParam != null && (parameter.Value != null
-                                                                       && parameter.Value != DBNull.Value)) {
+                                                       } else {
+                                                               parameter.Value = null;
+                                                       }
+
+                                                       DbParameter nullCheckParam = parameter as DbParameter;
+
+                                                       if (nullCheckParam != null && nullCheckParam.SourceColumnNullMapping) {
+                                                               if (parameter.Value != null && parameter.Value != DBNull.Value)
                                                                        nullCheckParam.Value = 0;
-                                                                       nullCheckParam = null;
-                                                               }
+                                                               else
+                                                                       nullCheckParam.Value = 1;
+                                                               nullCheckParam = null;
                                                        }
                                                }
                                        }
-                               }
-                               catch (Exception e) {
+                               } catch (Exception e) {
                                        argsUpdating.Errors = e;
                                        argsUpdating.Status = UpdateStatus.ErrorsOccurred;
                                }
 
-                               
                                IDataReader reader = null;
-                               try {                                                           
-                                       if (command == null) {
+                               try {
+                                       if (command == null)
                                                throw ExceptionHelper.UpdateRequiresCommand (commandName);
-                                       }                               
                                
                                        CommandBehavior commandBehavior = CommandBehavior.Default;
                                        if (command.Connection.State == ConnectionState.Closed) {
@@ -776,7 +714,7 @@ namespace System.Data.Common {
                                                                        row [dstColumnName] = reader [columnName];
                                                                } finally {
                                                                        dstColumn.ReadOnly = readOnlyState;
-                                                               }                                    
+                                                               }
                                                        }
                                                }
                                        }
@@ -786,9 +724,10 @@ namespace System.Data.Common {
                                        // 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.");
+                                                       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
@@ -814,112 +753,123 @@ namespace System.Data.Common {
                                                        } 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) {
+                                               if (updatedArgs.Errors == null)
                                                        updatedArgs.Errors = ExceptionHelper.RowUpdatedError();
-                                               }
                                                row.RowError += updatedArgs.Errors.Message;
-                                               if (!ContinueUpdateOnError) {
+                                               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) {
+                               } catch (Exception e) {
                                        row.RowError = e.Message;
-                                       if (!ContinueUpdateOnError) {
+                                       if (!ContinueUpdateOnError)
                                                throw e;
-                                       }
                                } finally {
-                                       if (reader != null && ! reader.IsClosed) {
+                                       if (reader != null && ! reader.IsClosed)
                                                reader.Close ();
-                                       }
-                               }       
-                       }               
+                               }
+                       }
                        return updateCount;
                }
 
-               public int Update (DataSet dataSet, string sourceTable) 
+               public int Update (DataSet dataSet, string srcTable)
                {
                        MissingMappingAction mappingAction = MissingMappingAction;
 
                        if (mappingAction == MissingMappingAction.Ignore)
                                mappingAction = MissingMappingAction.Error;
 
-                       DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, sourceTable, sourceTable, mappingAction);
+                       DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, srcTable, srcTable, mappingAction);
 
-                       DataTable dataTable = dataSet.Tables[tableMapping.DataSetTable];
+                       DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable];
                        if (dataTable == null)
                                throw new ArgumentException (String.Format ("Missing table {0}",
-                                                                           sourceTable));
+                                                                           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);
                }
-               
-#if NET_2_0
+
                // All the batch methods, should be implemented, if supported,
-               // by individual providers 
+               // by individual providers
 
-               protected virtual int AddToBatch (IDbCommand cmd)
+               protected virtual int AddToBatch (IDbCommand command)
                {
-                       throw new NotSupportedException ();
+                       throw CreateMethodNotSupportedException ();
                }
 
                protected virtual void ClearBatch ()
                {
-                       throw new NotSupportedException ();
+                       throw CreateMethodNotSupportedException ();
                }
 
                protected virtual int ExecuteBatch ()
                {
-                       throw new NotSupportedException ();
+                       throw CreateMethodNotSupportedException ();
                }
 
-               protected virtual IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIdentifer)
+               protected virtual IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
                {
-                       throw new NotSupportedException ();
+                       throw CreateMethodNotSupportedException ();
                }
 
-               protected virtual void InitializeBatching ()
+               protected virtual bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
                {
-                       throw new NotSupportedException ();
+                       recordsAffected = 1;
+                       error = null;
+                       return true;
                }
 
-               protected virtual void TerminateBatching ()
+               protected virtual void InitializeBatching ()
                {
-                       throw new NotSupportedException ();
+                       throw CreateMethodNotSupportedException ();
                }
-#endif
 
-#if ONLY_1_0 || ONLY_1_1
-               internal override void OnFillErrorInternal (FillErrorEventArgs value)
+               protected virtual void TerminateBatching ()
                {
-                       OnFillError (value);
+                       throw CreateMethodNotSupportedException ();
                }
 
-               protected virtual void OnFillError (FillErrorEventArgs value) 
+               Exception CreateMethodNotSupportedException ()
                {
-                       if (FillError != null)
-                               FillError (this, value);
+                       return new NotSupportedException ("Method is not supported.");
                }
-#endif
-
                #endregion // Methods
        }
 }