DataAdapter.Update(DataSet) DataAdapter.Update(DataSet) DataAdapter.Update(DataSet) Method


从名为“Table”的 DataSet 为指定的 DataTable 中每个已插入、已更新或已删除的行调用相应的 INSERT、UPDATE 或 DELETE 语句。Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet from a DataTable named "Table."

 abstract int Update(System::Data::DataSet ^ dataSet);
public abstract int Update (System.Data.DataSet dataSet);
abstract member Update : System.Data.DataSet -> int


DataSet DataSet DataSet

用于更新数据源的 DataSetThe DataSet used to update the data source.


DataSet 中成功更新的行数。The number of rows successfully updated from the DataSet.



尝试执行 INSERT、UPDATE 或 DELETE 语句,但没有记录受到影响。An attempt to execute an INSERT, UPDATE, or DELETE statement resulted in zero records affected.


下面的示例使用派生类OleDbDataAdapterUpdate数据源。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))
        OleDbDataAdapter adapter =
            new OleDbDataAdapter();
        adapter.SelectCommand =
            new OleDbCommand(queryString, connection);
        OleDbCommandBuilder builder =
            new OleDbCommandBuilder(adapter);


        // Code to modify data in the DataSet here.

        // Without the OleDbCommandBuilder, this line would fail.
        adapter.UpdateCommand = builder.GetUpdateCommand();
    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)
        Dim adapter As New OleDbDataAdapter()

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

        Dim builder As OleDbCommandBuilder = _
            New OleDbCommandBuilder(adapter)


        ' Code to modify the data in the DataSet here. 

        ' Without the OleDbCommandBuilder this line would fail.
    End Using
    Return dataSet
End Function


更新按行执行。The update is performed on a by-row basis. 对于每个已插入的、已修改的和Update已删除的行,该方法将确定对其执行的更改类型(Insert、Update 或 Delete)。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). 根据更改的类型,将Insert Update,执行或Delete命令模板,以将修改后的行传播到数据源。Depending on the type of change, the Insert, Update, or Delete command template executes to propagate the modified row to the data source. 当应用程序调用Update方法时,将DataAdapter检查RowState属性,并根据在中配置的DataSet索引的顺序,对每一行执行所需的INSERT、UPDATE或DELETE语句。.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. 例如, Update可能会执行 DELETE 语句,后跟 INSERT 语句,然后执行另一 delete 语句,因为DataTable中的行的顺序。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. 如果必须控制语句类型GetChanges的顺序(例如,在 UPDATE 之前插入),则应用程序可以调用方法。An application can call the GetChanges method in situations where you must control the sequence of statement types (for example, INSERT before UPDATE). 有关详细信息,请参阅用 Dataadapter 更新数据源For more information, see Updating Data Sources with DataAdapters.

如果尚未指定 INSERT、UPDATE 或 DELETE 语句,则该方法将Update生成一个异常。If INSERT, UPDATE, or DELETE statements have not been specified, the Update method generates an exception. 但是,如果您设置了SqlCommandBuilder .NET Framework OleDbCommandBuilder数据提供程序的SelectCommand属性,则可以创建或对象以自动为单表更新生成 SQL 语句。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. 然后, CommandBuilder将生成未设置的任何其他 SQL 语句。Then, any additional SQL statements that you do not set are generated by the CommandBuilder. 此代逻辑要求密钥列信息存在于中DataSetThis generation logic requires key column information to be present in the DataSet. 有关详细信息,请参阅通过 Commandbuilder 生成命令For more information see Generating Commands with CommandBuilders.

Update执行更新之前,方法将从第一个映射中列出的表中检索行。The Update method retrieves rows from the table listed in the first mapping before performing an update. 然后Update ,使用UpdatedRowSource属性的值刷新该行。The Update then refreshes the row using the value of the UpdatedRowSource property. 将忽略返回的任何其他行。Any additional rows returned are ignored.

在将任何数据加载回DataSet后,将OnRowUpdated引发事件,从而允许用户检查已协调DataSet的行和该命令返回的任何输出参数。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.

使用Update时,执行顺序如下所示:When using Update, the order of execution is as follows:

  1. 中的值DataRow将被移动到参数值中。The values in the DataRow are moved to the parameter values.

  2. 引发 OnRowUpdating 事件。The OnRowUpdating event is raised.

  3. 命令执行。The command executes.

  4. 如果命令设置为FirstReturnedRecord,则将第一个返回的结果放置DataRow在中。If the command is set to FirstReturnedRecord, then the first returned result is placed in the DataRow.

  5. 如果有输出参数,则将它们放在中DataRowIf there are output parameters, they are placed in the DataRow.

  6. 引发 OnRowUpdated 事件。The OnRowUpdated event is raised.

  7. 调用 DataRow.AcceptChangesDataRow.AcceptChanges is called. 这会为更新后DataTable.RowChanging DataRow引发DataTable.RowChanged和事件。This will raise both the DataTable.RowChanging and DataTable.RowChanged events for the updated DataRow.

与关联的DataAdapter每个命令通常都具有与之关联的参数集合。Each command associated with the DataAdapter usually has a parameters collection associated with it. 通过 .net 数据提供程序的SourceColumn Parameter类的和属性SourceVersion ,将参数映射到当前行。Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a .NET data provider's Parameter class. SourceColumn引用用于获取当前行的DataAdapter参数值的列。DataTableSourceColumn refers to a DataTable column that the DataAdapter references to obtain parameter values for the current row.

SourceColumn在应用了任何表映射之前引用未映射的列名称。SourceColumn refers to the unmapped column name before any table mappings have been applied. 如果SourceColumn引用不存在的列,则执行的操作取决于下列MissingMappingAction值之一。If SourceColumn refers to a nonexistent column, the action taken depends on one of the following MissingMappingAction values.

枚举值Enumeration value 执行的操作Action taken
MissingMappingAction.Passthrough DataSet如果不存在映射,请使用中的源列名称和表名称。Use the source column names and table names in the DataSet if no mapping is present.
MissingMappingAction.Ignore SystemException生成。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 SystemException生成。A SystemException is generated.

属性还可用于将输出或输入/输出参数的值映射回DataSetSourceColumnThe 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.

.Net SourceVersion数据提供程序的Parameter类的属性确定是使用列值的原始版本、当前版本还是建议的版本。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. 此功能通常用于在 UPDATE 语句的 WHERE 子句中包含原始值,以检查是否存在开放式并发冲突。This capability is often used to include original values in the WHERE clause of an UPDATE statement to check for optimistic concurrency violations.

调用方法或AcceptChanges方法将提交或DataTable中的DataSet所有更改。 AcceptChangesCalling the AcceptChanges method or AcceptChanges method will commit all changes in the DataSet or DataTable. 如果在调用Update方法之前调用这些方法中的任何一个,则调用Update方法时不会提交任何更改,除非从或AcceptChanges调用以来AcceptChanges进行了进一步的更改。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.


如果在更新行时出现错误,则会引发异常并中止执行更新。If an error occurs while updating a row, an exception is thrown and execution of the update is discontinued. 若要在遇到错误时继续执行更新操作而不生成异常,请ContinueUpdateOnError在调用true Update之前将属性设置为。To continue the update operation without generating exceptions when an error is encountered, set the ContinueUpdateOnError property to true before calling Update. 您还可以在 DataAdapter RowUpdated事件内每行响应错误。You may also respond to errors on a per-row basis within the RowUpdated event of a DataAdapter. 若要继续执行更新RowUpdated操作而不在事件中生成异常,请Status将的RowUpdatedEventArgs属性设置Continue为。To continue the update operation without generating an exception within the RowUpdated event, set the Status property of the RowUpdatedEventArgs to Continue.

Update对于传播到上的数据源的每个列,应将参数添加InsertCommand到、 UpdateCommandDeleteCommandFor every column that you propagate to the data source on Update, a parameter should be added to the InsertCommand, UpdateCommand, or DeleteCommand. 应将参数的属性设置为列的名称。SourceColumnThe 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.