Επεξεργασία

Κοινή χρήση μέσω


DataAdapter.Update(DataSet) Method

Definition

Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet from a DataTable named "Table."

public:
 virtual int Update(System::Data::DataSet ^ dataSet);
public:
 abstract int Update(System::Data::DataSet ^ dataSet);
public virtual int Update (System.Data.DataSet dataSet);
public abstract int Update (System.Data.DataSet dataSet);
abstract member Update : System.Data.DataSet -> int
override this.Update : System.Data.DataSet -> int
abstract member Update : System.Data.DataSet -> int
Public Overridable Function Update (dataSet As DataSet) As Integer
Public MustOverride Function Update (dataSet As DataSet) As Integer

Parameters

dataSet
DataSet

The DataSet used to update the data source.

Returns

The number of rows successfully updated from the DataSet.

Implements

Exceptions

The source table is invalid.

An attempt to execute an INSERT, UPDATE, or DELETE statement resulted in zero records affected.

Examples

The following example uses the derived class, OleDbDataAdapter, to Update the data source.

static private DataSet CreateCommandAndUpdate(
    string connectionString,
    string queryString)
{
    DataSet dataSet = new DataSet();

    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        connection.Open();
        OleDbDataAdapter adapter =
            new OleDbDataAdapter();
        adapter.SelectCommand =
            new OleDbCommand(queryString, connection);
        OleDbCommandBuilder builder =
            new OleDbCommandBuilder(adapter);

        adapter.Fill(dataSet);

        // Code to modify data in the DataSet here.

        // Without the OleDbCommandBuilder, this line would fail.
        adapter.UpdateCommand = builder.GetUpdateCommand();
        adapter.Update(dataSet);
    }
    return dataSet;
}
Public Function CreateCommandAndUpdate( _
    ByVal connectionString As String, _
    ByVal queryString As String) As DataSet

    Dim dataSet As New DataSet()

    Using connection As New OleDbConnection(connectionString)
        connection.Open()
        Dim adapter As New OleDbDataAdapter()

        adapter.SelectCommand = New OleDbCommand( _
            queryString, connection)

        Dim builder As OleDbCommandBuilder = _
            New OleDbCommandBuilder(adapter)

        adapter.Fill(dataSet)

        ' Code to modify the data in the DataSet here. 

        ' Without the OleDbCommandBuilder this line would fail.
        builder.GetUpdateCommand()
        adapter.Update(dataSet)
    End Using
    Return dataSet
End Function

Remarks

The update is performed on a by-row basis. For every inserted, modified, and deleted row, the Update method determines the type of change that has been performed on it (Insert, Update or Delete). Depending on the type of change, the Insert, Update, or Delete command template executes to propagate the modified row to the data source. When an application calls the Update method, the DataAdapter examines the RowState property, and executes the required INSERT, UPDATE, or DELETE statements iteratively for each row, 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.

It should be noted that these statements are not performed as a batch process; each row is updated individually. An application can call the GetChanges method in situations where you must control the sequence of statement types (for example, INSERT before UPDATE). For more information, see Updating Data Sources with DataAdapters.

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 Framework 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 Generating Commands with CommandBuilders.

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. DataRow.AcceptChanges is called. This will raise both the DataTable.RowChanging and DataTable.RowChanged events for the updated DataRow.

Each command associated with the DataAdapter 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 DataAdapter 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 nonexistent column, the action taken depends on one of the following MissingMappingAction values.

Enumeration value Action taken
MissingMappingAction.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 nonexistent 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.

Calling the AcceptChanges method or AcceptChanges method will commit all changes in the DataSet or DataTable. If either of these methods are called before the Update method is called, no changes will be committed when the Update method is called, unless further changes have been made since AcceptChanges or AcceptChanges was called.

Note

If an error occurs while updating a row, an exception is thrown and execution of the update is discontinued. To continue the update operation without generating exceptions when an error is encountered, set the ContinueUpdateOnError property to true before calling Update. You may also respond to errors on a per-row basis within the RowUpdated event of a DataAdapter. To continue the update operation without generating an exception within the RowUpdated event, set the Status property of the RowUpdatedEventArgs to Continue.

For every column that you propagate to the data source on Update, a parameter should be added to the InsertCommand, UpdateCommand, or DeleteCommand. The SourceColumn property of the parameter should be set to the name of the column. This setting indicates that the value of the parameter is not set manually, but is taken from the particular column in the currently processed row.

Applies to

See also