Bewerken

Share via


DbDataAdapter.Update Method

Definition

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet.

Overloads

Update(DataSet, String)

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet with the specified DataTable name.

Update(DataRow[], DataTableMapping)

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array of DataRow objects.

Update(DataTable)

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataTable.

Update(DataSet)

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet.

Update(DataRow[])

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array in the DataSet.

Update(DataSet, String)

Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet with the specified DataTable name.

public:
 int Update(System::Data::DataSet ^ dataSet, System::String ^ srcTable);
public int Update (System.Data.DataSet dataSet, string srcTable);
override this.Update : System.Data.DataSet * string -> int
Public Function Update (dataSet As DataSet, srcTable As String) As Integer

Parameters

dataSet
DataSet

The DataSet to use to update the data source.

srcTable
String

The name of the source table to use for table mapping.

Returns

The number of rows successfully updated from the DataSet.

Exceptions

The DataSet is invalid.

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.

public DataSet CreateCmdsAndUpdate(string connectionString,
    string queryString, string tableName)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(queryString, connection);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

        connection.Open();

        DataSet customers = new DataSet();
        adapter.Fill(customers);

        //code to modify data in dataset here

        adapter.Update(customers, tableName);

        return customers;
    }
}
Public Function CreateCmdsAndUpdate(ByVal connectionString As String, _
    ByVal queryString As String, _
    ByVal tableName As String) As DataSet

    Using connection As New OleDbConnection(connectionString)
        Dim adapter As New OleDbDataAdapter()
        adapter.SelectCommand = New OleDbCommand(queryString, connection)
        Dim builder As New OleDbCommandBuilder(adapter)

        connection.Open()

        Dim customers As New DataSet()
        adapter.Fill(customers)

        ' Code to modify data in DataSet here 

        adapter.Update(customers, tableName)

        Return customers
    End Using
End Function

Remarks

When an application calls the Update method, the DbDataAdapter 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 supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. When multiple tables with the same name, but different case, exist in a DataSet, 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 ds = new DataSet();  
 ds.Tables.Add("aaa");  
 ds.Tables.Add("AAA");  
 adapter.Update(ds, "aaa"); // Updates "aaa", which already exists in the DataSet.  
 adapter.Update(ds, "AAA"); // Updates "AAA", which already exists in the DataSet.  
    adapter.Update(ds, "Aaa"); // Results in an exception.  

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 Framework 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 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 Framework 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, 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.

See also

Applies to

Update(DataRow[], DataTableMapping)

Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array of DataRow objects.

protected:
 virtual int Update(cli::array <System::Data::DataRow ^> ^ dataRows, System::Data::Common::DataTableMapping ^ tableMapping);
protected virtual int Update (System.Data.DataRow[] dataRows, System.Data.Common.DataTableMapping tableMapping);
override this.Update : System.Data.DataRow[] * System.Data.Common.DataTableMapping -> int
Protected Overridable Function Update (dataRows As DataRow(), tableMapping As DataTableMapping) As Integer

Parameters

dataRows
DataRow[]

An array of DataRow objects used to update the data source.

tableMapping
DataTableMapping

The TableMappings collection to use.

Returns

The number of rows successfully updated from the array of DataRow objects.

Exceptions

The DataSet is invalid.

The source table is invalid.

No DataRow exists to update.

-or-

No DataTable exists to update.

-or-

No DataSet exists to use as a source.

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 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, INSERTs before UPDATEs). 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. 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 Framework 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 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 Framework 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, 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.

See also

Applies to

Update(DataTable)

Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataTable.

public:
 int Update(System::Data::DataTable ^ dataTable);
public int Update (System.Data.DataTable dataTable);
override this.Update : System.Data.DataTable -> int
Public Function Update (dataTable As DataTable) As Integer

Parameters

dataTable
DataTable

The DataTable used to update the data source.

Returns

The number of rows successfully updated from the DataTable.

Exceptions

The DataSet is invalid.

The source table is invalid.

No DataRow exists to update.

-or-

No DataTable exists to update.

-or-

No DataSet exists to use as a source.

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.

public DataTable CreateCmdsAndUpdate(string connectionString,
    string queryString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(queryString, connection);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

        connection.Open();

        DataTable customers = new DataTable();
        adapter.Fill(customers);

        // code to modify data in DataTable here

        adapter.Update(customers);

        return customers;
    }
}
Public Function CreateCmdsAndUpdate(ByVal connectionString As String, _
    ByVal queryString As String) As DataTable

    Using connection As New OleDbConnection(connectionString)
        Dim adapter As New OleDbDataAdapter()
        adapter.SelectCommand = New OleDbCommand(queryString, connection)
        Dim builder As New OleDbCommandBuilder(adapter)

        connection.Open()

        Dim customers As New DataTable()
        adapter.Fill(customers)

        ' Code to modify data in DataTable here 

        adapter.Update(customers)
        Return customers
    End Using
End Function

Remarks

When an application calls the Update method, the DbDataAdapter 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, INSERTs before UPDATEs). 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. 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 Framework 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 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 Framework 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, 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.

See also

Applies to

Update(DataSet)

Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet.

public:
 override int Update(System::Data::DataSet ^ dataSet);
public override int Update (System.Data.DataSet dataSet);
override this.Update : System.Data.DataSet -> int
Public Overrides 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.

public DataSet CreateCmdsAndUpdate(string connectionString,
    string queryString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(queryString, connection);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

        connection.Open();

        DataSet customers = new DataSet();
        adapter.Fill(customers);

        //code to modify data in dataset here

        adapter.Update(customers);

        return customers;
    }
}
Public Function CreateCmdsAndUpdate(ByVal connectionString As String, _
    ByVal queryString As String) As DataSet

    Using connection As New OleDbConnection(connectionString)
        Dim adapter As New OleDbDataAdapter()
        adapter.SelectCommand = New OleDbCommand(queryString, connection)
        Dim builder As New OleDbCommandBuilder(adapter)

        connection.Open()

        Dim customers As New DataSet()
        adapter.Fill(customers)

        ' Code to modify data in DataSet here 

        adapter.Update(customers)

        Return customers
    End Using
End Function

Remarks

When an application calls the Update method, the DbDataAdapter 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, INSERTs before UPDATEs). 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. 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 Framework 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 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 Framework 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, 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.

See also

Applies to

Update(DataRow[])

Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs
Source:
DbDataAdapter.cs

Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array in the DataSet.

public:
 int Update(cli::array <System::Data::DataRow ^> ^ dataRows);
public int Update (System.Data.DataRow[] dataRows);
override this.Update : System.Data.DataRow[] -> int
Public Function Update (dataRows As DataRow()) As Integer

Parameters

dataRows
DataRow[]

An array of DataRow objects used to update the data source.

Returns

The number of rows successfully updated from the DataSet.

Exceptions

The DataSet is invalid.

The source table is invalid.

No DataRow exists to update.

-or-

No DataTable exists to update.

-or-

No DataSet exists to use as a source.

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.

public DataSet CreateCmdsAndUpdate(string connectionString,
    string queryString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(queryString, connection);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

        connection.Open();

        DataSet customers = new DataSet();
        adapter.Fill(customers);

        //code to modify data in dataset here

        //Insert new records from DataSet
        DataRow[] rows = customers.Tables[0].Select(
            null, null, DataViewRowState.Added);
        adapter.Update(rows);

        return customers;
    }
}
Public Function CreateCmdsAndUpdate(ByVal connectionString As String, _
    ByVal queryString As String) As DataSet

    Using connection As New OleDbConnection(connectionString)
        Dim adapter As New OleDbDataAdapter()
        adapter.SelectCommand = New OleDbCommand(queryString, connection)
        Dim builder As New OleDbCommandBuilder(adapter)

        connection.Open()

        Dim customers As New DataSet()
        adapter.Fill(customers)

        ' Code to modify data in DataSet here 

        ' Insert new records from DataSet
        Dim rows() As DataRow = customers.Tables(0).Select( _
            Nothing, Nothing, DataViewRowState.Added)
        adapter.Update(rows)

        Return customers
    End Using
End Function

Remarks

When an application calls the Update method, the DbDataAdapter 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, INSERTs before UPDATEs). 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, 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 Framework 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 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 Framework 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, 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.

See also

Applies to