Calls the respective insert, update, or delete commands on the connection for each inserted, updated, or deleted row in the data set for the given DataSet.
[Visual Basic] Overloads Overrides Public Function Update( _ ByVal dataSet As DataSet _ ) As Integer [C#] public override int Update( DataSet dataSet ); [C++] public: override int Update( DataSet* dataSet ); [JScript] public override function Update( dataSet : DataSet ) : int;
Exception Type | Condition |
---|---|
UpdateRequiresSourceTable | A source table could not be found. |
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 ADOCommand, 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 ADOCommand. 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 ADOCommand. 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 ADOConnection.
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 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
DBDataSetCommand Class | DBDataSetCommand Members | System.Data.Internal Namespace | DBDataSetCommand.Update Overload List | ADOCommand | ADOConnection