NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

DBDataSetCommand.Update (DataSet, String)

Calls the respective insert, update, or delete commands on the connection, for each inserted, updated, or deleted rows in the data set for a given source table.

[Visual Basic]
Overloads Overridable Public Function Update( _
   ByVal dataSet As DataSet, _
   ByVal srcTable As String _
) As Integer
[C#]
public virtual int Update(
   DataSet dataSet,
   string srcTable
);
[C++]
public: virtual int Update(
   DataSet* dataSet,
   String* srcTable
);
[JScript]
public function Update(
   dataSet : DataSet,
   srcTable : String
) : int;

Parameters

dataSet
The DataSet to update.
srcTable
The source table used for the table mappings.

Exceptions

Exception Type Condition
UpdateRequiresDataSet The DataSet is invalid.
UpdateRequiresSourceTable The source table is invalid.

Remarks

This method pulls rows from the table listed in the first mapping before running an update. The adapter will clone the insert, update, or delete command appropriate for the row. Then the OnRowUpdating event is raised, allowing the user to inspect the DataSet row, modify the cloned DBCommand, or cancel the default processing. The cloned command is then executed against the data source. If the cloned command is configured incorrectly, an error will be raised. The adapter then will refresh the row according to UpdateRowSource property of the DBCommand. Any additional rows returned will be 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 DBCommand. When the OnRowUpdated event returns, the adapter releases the cloned copy of the command and any changes made to it between the events are lost. After a row updates successfully, the changes to that row will have been accepted. If the connection was closed prior to Update, the adapter will close the DBConnection.

Notes to Inheritors: When overriding Update in a derived class, be sure to call the base class's Update method.

Example [Visual Basic]

The following example creates and initializes a DataSet and an ADODataSetCommand. The example then uses the Update method to update the DataSet and the data source.

[Visual Basic]

Sub CreateAndUpdateDataSet()
     Const strConnection = "Provider=SQLADO.1;User ID=sa;PASSWORD=;" _
         + "Initial Catalog=Northwind;Data Source=vbsql7;"
     Const strSelectCommand = "SELECT intID, vchFirstName, vchLastName, " _
+ "chState FROM Customers WHERE chState = 'CA'"
     Const strUpdateCommand = "UPDATE tblCustomer SET vchFirstName = ?, " _
+ "vchLastName = ?, chState = ? WHERE intID = ?"
     Dim dataSet        As DataSet
     Dim adoConnection  As ADOConnection
     Dim adoDataSetComm As ADODataSetCommand
     Dim adoParam       As ADOParameter
     ' Initialize the DataSet
     Set dataSet = New DataSet
     ' Initialize the connection
     Set adoConnection = New ADOConnection(strConnection)
     ' Initialize the new ADODataSetCommand object
     Set adoDataSetComm = New ADODataSetCommand
     ' Set the table that the ADODataSetCommand is to map to
     adoDataSetComm.TableMappings.Add "Customers", "Customer"
     ' Set the select command
     Set adoDataSetComm.SelectCommand.CommandText = strSelectCommand
     ' Set the active connection for our ADODataSetCommand
     Set adoDataSetComm.SelectCommand.ActiveConnection = adoConnection
     ' Populate the DataSet
     adoDataSetComm.FillDataSet dataSet, "Customers"
     ' Set the command text to the text of the query
     adoDataSetComm.UpdateCommand.CommandText = strUpdateCommand
     ' Set the command's connection to the current connection
     Set adoDataSetComm.UpdateCommand.ActiveConnection = adoConnection
     ' Set any parameters for this command
     Set adoParam = adoDataSetComm.UpdateCommand.parameters.Add( _
 "vchFirstName", ADODBType.VarChar, 50)
     adoParam.SourceColumn = "vchFirstName"
     Set adoParam = adoDataSetComm.UpdateCommand.parameters.Add( _
  "vchLastName", ADODBType.VarChar, 50)
     adoParam.SourceColumn = "vchLastName"
     Set adoParam = adoDataSetComm.UpdateCommand.parameters.Add( _
  "chState", ADODBType.Char, 2)
     adoParam.SourceColumn = "chState"
     Set adoParam = adoDataSetComm.UpdateCommand.parameters.Add( _
  "intID", ADODBType.Integer)
     adoParam.SourceColumn = "intID"
         
     ' this uses the UPDATE statement above
     adoConnection.BeginTransaction
     Try
         ' Update the data source with changes made in dataSet
         adoDataSetComm.Update dataSet, "Customers"
         adoConnection.CommitTransaction
     Catch e As Exception
         Dim displayBox As New MessageBox
         displayBox.Show "Error while updating data source - rolling back transaction..."
         adoConnection.RollbackTransaction
     End Try
     adoConnection.close
 End Sub

See Also

DBDataSetCommand Class | DBDataSetCommand Members | System.Data.Internal Namespace | DBDataSetCommand.Update Overload List | DBCommand | DBConnection