asp.net.ph

DbDataAdapter.Update Method ( DataSet, String )

System.Data.Common Namespace   DbDataAdapter Class


Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet with the specified DataTable name.

[ VB ]
Overloads Public Function Update ( _
   ByVal dataSet As DataSet, _
   ByVal srcTable As String _
) As Integer

[ C# ]
public int Update (
   DataSet dataSet,
   string srcTable
);

[ C++ ]
public: int Update (
   DataSet* dataSet,
   String* srcTable
);

[ JScript ]
public function Update (
   dataSet : DataSet,
   srcTable : String
) : int

Parameters

dataSet
The DataSet to use to update the data source.
srcTable
The name of the source table to use for table mapping.

Return Value

The number of rows successfully updated from the DataSet.

Exceptions


Exception Type Condition
ArgumentNullException Occurs when the DataSet is invalid.
InvalidOperationException Occurs when the source table is invalid.
DBConcurrencyException Occurs when an attempt to execute an INSERT, UPDATE, or DELETE statement resulted in zero records affected.

Remarks

When an application calls the Update method, the DbDataAdapter examines the RowState property, and executes the required INSERT, UPDATE, or DELETE statements based on the order of the indexes configured in the DataSet. For example, Update might execute a DELETE statement, followed by an INSERT statement, and then another DELETE statement, due to the ordering of the rows in the DataTable. An application can call the GetChanges method in situations where you must control the sequence of statement types ( for example, INSERTs before UPDATEs ). For more information, see Updating the Database with a DataAdapter and the DataSet.

If INSERT, UPDATE, or DELETE statements have not been specified, the Update method generates an exception. However, you can create a SqlCommandBuilder or OleDbCommandBuilder object to automatically generate SQL statements for single-table updates if you set the SelectCommand property of a .NET data provider. Then, any additional SQL statements that you do not set are generated by the CommandBuilder. This generation logic requires key column information to be present in the DataSet. For more information see Automatically Generated Commands.

The Update method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. In such situations, Update performs a case-sensitive comparison to find the corresponding table, and generates an exception if no exact match exists. The following C# code illustrates this behavior.

DataSet dataset = new DataSet ( );
dataset.Tables.Add ( "aaa" );
dataset.Tables.Add ( "AAA" );
adapter.Update ( dataset, "aaa" ); 
// updates "aaa", which already exists in the DataSet.

If Update is called and the DataSet contains only one DataTable whose name differs only by case, that DataTable is updated. In this scenario, the comparison is case insensitive. the following C# code illustrates this behavior.

DataSet dataset = new DataSet ( );
dataset.Tables.Add ( "aaa" );
adapter.Update ( dataset, "AAA" ); 
// updates table "aaa" because only one similarly named table is in the DataSet.

The Update method retrieves rows from the table listed in the first mapping before performing an update. The Update then refreshes the row using the value of the UpdatedRowSource property. Any additional rows returned are ignored.

After any data is loaded back into the DataSet, the OnRowUpdated event is raised, allowing the user to inspect the reconciled DataSet row and any output parameters returned by the command. After a row updates successfully, the changes to that row are accepted.

When using Update, the order of execution is as follows:

  1. the values in the DataRow are moved to the parameter values.
  2. the OnRowUpdating event is raised.
  3. the command executes.
  4. If the command is set to FirstReturnedRecord, then the first returned result is placed in the DataRow.
  5. If there are output parameters, they are placed in the DataRow.
  6. the OnRowUpdated event is raised.
  7. AcceptChanges is called.

Each command associated with the DbDataAdapter usually has a parameters collection associated with it. Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a .NET data provider's Parameter class. SourceColumn refers to a DataTable column that the DbDataAdapter references to obtain parameter values for the current row.

SourceColumn refers to the unmapped column name before any table mappings have been applied. If SourceColumn refers to a non-existent column, the action taken depends on one of the following MissingMappingAction values.

Enumeration Value Action Taken
Passthrough Use the source column names and table names in the DataSet if no mapping is present.
MissingMappingAction.Ignore A SystemException is generated. When the mappings are explicitly set, a missing mapping for an input parameter is usually the result of an error.
MissingMappingAction.Error A SystemException is generated.

The SourceColumn property is also used to map the value for output or input/output parameters back to the DataSet. An exception is generated if it refers to a non-existent column.

The SourceVersion property of a .NET data provider's Parameter class determines whether to use the Original, Current, or Proposed version of the column value. This capability is often used to include original values in the WHERE clause of an UPDATE statement to check for optimistic concurrency violations.

NOTE: If an error occurs while updating a row, a description of the error is placed in the RowError property of the DataRow object, and an exception is thrown. To continue the update operation without generating an exception, intercept the RowUpdated event and set the value of the UpdateStatus enumeration to Continue. Then, call DataRow.ClearErrors to clear the error state from the row.

Example

The following example uses the derived class, OleDbDataAdapter, to Update the data source. This example assumes that you have created an OleDbDataAdapter and a DataSet.

public DataSet UpdateDemo ( DataSet myDataSet, string connString, 
      string strQuery, string myTableName ) {

   OleDbConnection myConn = new OleDbConnection ( connString );
   OleDbDataAdapter myAdapter = new OleDbDataAdapter ( );
   myAdapter.SelectCommand = new OleDbCommand ( strQuery, myConn );
   OleDbCommandBuilder custCB = new OleDbCommandBuilder ( myAdapter );

   myConn.Open ( );
   DataSet myDataSet = new DataSet ( );
   myAdapter.Fill ( myDataSet );

   // ... code to modify data in DataSet here ...

   // without the OleDbCommandBuilder this line would fail
   myAdapter.Update ( myDataSet,"Categories" );

   myConn.Close ( );
   return myDataSet;
}
  C# VB

See Also

DbDataAdapter Members   DbDataAdapter.Update Overload List Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph