DbDataAdapter.Update DbDataAdapter.Update DbDataAdapter.Update DbDataAdapter.Update Method

定義

針對 DataSet 中的每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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.

多載

Update(DataSet, String) Update(DataSet, String) Update(DataSet, String)

針對具有指定 DataTable 名稱之 DataSet 中的每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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) Update(DataRow[], DataTableMapping) Update(DataRow[], DataTableMapping) Update(DataRow[], DataTableMapping)

針對 DataSet 物件的指定陣列中每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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 DataSet objects.

Update(DataTable) Update(DataTable) Update(DataTable)

針對指定之 DataTable 中的每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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) Update(DataSet) Update(DataSet)

針對指定之 DataSet 中的每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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[]) Update(DataRow[]) Update(DataRow[]) Update(DataRow[])

針對 DataSet 的指定陣列中每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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) Update(DataSet, String) Update(DataSet, String)

針對具有指定 DataTable 名稱之 DataSet 中的每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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

參數

dataSet
DataSet DataSet DataSet DataSet

用來更新資料來源的 DataSetThe DataSet to use to update the data source.

srcTable
String String String String

用於資料表對應的來源資料表名稱。The name of the source table to use for table mapping.

傳回

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.

範例

下列範例會使用衍生的類別OleDbDataAdapter來更新資料來源。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 OleDbCommandBuilder = New OleDbCommandBuilder(adapter)

        connection.Open()

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

        ' Code to modify data in DataSet here 

        adapter.Update(customers, tableName)

        Return customers
    End Using
End Function

備註

當應用程式呼叫Update方法時DbDataAdapter ,會檢查RowState屬性,並根據在中DataSet設定的索引順序,針對每個資料列反復執行必要的INSERT、UPDATE或DELETE子句。.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. 例如, 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. 不過,如果您設定 .NET Framework Data Provider SqlCommandBuilderOleDbCommandBuilder 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. 此層代邏輯需要在中DataSet出現索引鍵資料行資訊。This generation logic requires key column information to be present in the DataSet. 如需詳細資訊,請參閱使用 Commandbuilder 產生命令For more information see Generating Commands with CommandBuilders.

方法支援的DataSet案例,其中包含的DataTable多個物件的名稱只有大小寫不同。 UpdateThe Update method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 當多個具有相同名稱但大小寫不同的資料表存在於中DataSetUpdate ,會執行區分大小寫的比較來尋找對應的資料表,並在沒有完全相符的情況下產生例外狀況。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. 下列C#程式碼將說明此行為。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.  

如果Update呼叫DataTable DataTable ,且只包含其名稱只有大小寫不同的一個,則會更新。DataSetIf 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. 下列C#程式碼將說明此行為。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.  

Update執行更新之前,方法會從第一個對應所列的資料表中抓取資料列。The Update method retrieves rows from the table listed in the first mapping before performing an update. 接著會使用UpdatedRowSource屬性的值重新整理資料列。 UpdateThe 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. 如果有輸出參數,則會將它們放在DataRow中。If there are output parameters, they are placed in the DataRow.

  6. 便會引發 OnRowUpdated 事件。The OnRowUpdated event is raised.

  7. 呼叫 AcceptChangesAcceptChanges is called.

與相關聯的DbDataAdapter每個命令,通常都有一個與其相關聯的 parameters 集合。Each command associated with the DbDataAdapter usually has a parameters collection associated with it. 參數會透過SourceColumn .NET Framework Data Provider Parameter類別的和SourceVersion屬性,對應至目前的資料列。Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a .NET Framework data provider's Parameter class. SourceColumnDbDataAdapter參考用DataTable來取得目前資料列之參數值的資料行。SourceColumn refers to a DataTable column that the DbDataAdapter 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 Framework Data Provider 類別的SourceVersion屬性會決定是要使用資料行值的原始、目前或建議版本。ParameterThe 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. 這項功能通常用來在 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.

注意

如果在更新資料列時發生錯誤,則會擲回例外狀況,並中止執行更新。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將的Continue屬性RowUpdatedEventArgs設定為。To continue the update operation without generating an exception within the RowUpdated event, set the Status property of the RowUpdatedEventArgs to Continue.

另請參閱

Update(DataRow[], DataTableMapping) Update(DataRow[], DataTableMapping) Update(DataRow[], DataTableMapping) Update(DataRow[], DataTableMapping)

針對 DataSet 物件的指定陣列中每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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 DataSet 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

參數

dataRows
DataRow[]

用來更新資料來源的 DataRow 物件陣列。An array of DataRow objects used to update the data source.

tableMapping
DataTableMapping DataTableMapping DataTableMapping DataTableMapping

要使用的 TableMappings 集合。The TableMappings collection to use.

傳回

DataSet 中成功更新的資料列數目。The number of rows successfully updated from the DataSet.

例外狀況

要更新的 DataRow 不存在。No DataRow exists to update.

-或--or- 要更新的 DataTable 不存在。No DataTable exists to update.

-或--or- 要用來做為來源的 DataSet 不存在。No DataSet exists to use as a source.

嘗試執行 INSERT、UPDATE 或 DELETE 陳述式會造成沒有資料錄受影響。An attempt to execute an INSERT, UPDATE, or DELETE statement resulted in zero records affected.

備註

當應用程式呼叫Update方法時DbDataAdapter ,會檢查RowState屬性,並根據在中DataSet設定的索引順序,針對每個資料列反復執行必要的INSERT、UPDATE或DELETE子句。.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. 例如, 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必須控制語句類型序列的情況下呼叫方法(例如,在更新之前插入)。An application can call the GetChanges method in situations where you must control the sequence of statement types (for example, INSERTs before UPDATEs). 如需詳細資訊,請參閱使用 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. 不過,如果您設定 .NET Framework Data Provider SqlCommandBuilderOleDbCommandBuilder 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. 此層代邏輯需要在中DataSet出現索引鍵資料行資訊。This 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. 接著會使用UpdatedRowSource屬性的值重新整理資料列。 UpdateThe 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. 如果有輸出參數,則會將它們放在DataRow中。If there are output parameters, they are placed in the DataRow.

  6. 便會引發 OnRowUpdated 事件。The OnRowUpdated event is raised.

  7. 呼叫 AcceptChangesAcceptChanges is called.

與相關聯的DbDataAdapter每個命令,通常都有一個與其相關聯的 parameters 集合。Each command associated with the DbDataAdapter usually has a parameters collection associated with it. 參數會透過SourceColumn .NET Framework Data Provider Parameter類別的和SourceVersion屬性,對應至目前的資料列。Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a .NET Framework data provider's Parameter class. SourceColumnDbDataAdapter參考用DataTable來取得目前資料列之參數值的資料行。SourceColumn refers to a DataTable column that the DbDataAdapter 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 Framework Data Provider 類別的SourceVersion屬性會決定是要使用資料行值的原始、目前或建議版本。ParameterThe 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. 這項功能通常用來在 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.

注意

如果在更新資料列時發生錯誤,則會擲回例外狀況,並中止執行更新。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將的Continue屬性RowUpdatedEventArgs設定為。To continue the update operation without generating an exception within the RowUpdated event, set the Status property of the RowUpdatedEventArgs to Continue.

另請參閱

Update(DataTable) Update(DataTable) Update(DataTable)

針對指定之 DataTable 中的每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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

參數

dataTable
DataTable DataTable DataTable DataTable

用來更新資料來源的 DataTableThe DataTable used to update the data source.

傳回

DataTable 中成功更新的資料列數目。The number of rows successfully updated from the DataTable.

例外狀況

要更新的 DataRow 不存在。No DataRow exists to update.

-或--or- 要更新的 DataTable 不存在。No DataTable exists to update.

-或--or- 要用來做為來源的 DataSet 不存在。No DataSet exists to use as a source.

嘗試執行 INSERT、UPDATE 或 DELETE 陳述式會造成沒有資料錄受影響。An attempt to execute an INSERT, UPDATE, or DELETE statement resulted in zero records affected.

範例

下列範例會使用衍生的類別OleDbDataAdapter來更新資料來源。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 OleDbCommandBuilder = New OleDbCommandBuilder(adapter)

        connection.Open()

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

        ' Code to modify data in DataTable here 

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

備註

當應用程式呼叫Update方法時DbDataAdapter ,會檢查RowState屬性,並根據在中DataSet設定的索引順序,針對每個資料列反復執行必要的INSERT、UPDATE或DELETE子句。.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. 例如, 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必須控制語句類型序列的情況下呼叫方法(例如,在更新之前插入)。An application can call the GetChanges method in situations where you must control the sequence of statement types (for example, INSERTs before UPDATEs). 如需詳細資訊,請參閱使用 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. 不過,如果您設定 .NET Framework Data Provider SqlCommandBuilderOleDbCommandBuilder 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. 此層代邏輯需要在中DataSet出現索引鍵資料行資訊。This 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. 接著會使用UpdatedRowSource屬性的值重新整理資料列。 UpdateThe 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. 如果有輸出參數,則會將它們放在DataRow中。If there are output parameters, they are placed in the DataRow.

  6. 便會引發 OnRowUpdated 事件。The OnRowUpdated event is raised.

  7. 呼叫 AcceptChangesAcceptChanges is called.

與相關聯的DbDataAdapter每個命令,通常都有一個與其相關聯的 parameters 集合。Each command associated with the DbDataAdapter usually has a parameters collection associated with it. 參數會透過SourceColumn .NET Framework Data Provider Parameter類別的和SourceVersion屬性,對應至目前的資料列。Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a .NET Framework data provider's Parameter class. SourceColumnDbDataAdapter參考用DataTable來取得目前資料列之參數值的資料行。SourceColumn refers to a DataTable column that the DbDataAdapter 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 Framework Data Provider 類別的SourceVersion屬性會決定是要使用資料行值的原始、目前或建議版本。ParameterThe 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. 這項功能通常用來在 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.

注意

如果在更新資料列時發生錯誤,則會擲回例外狀況,並中止執行更新。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將的Continue屬性RowUpdatedEventArgs設定為。To continue the update operation without generating an exception within the RowUpdated event, set the Status property of the RowUpdatedEventArgs to Continue.

另請參閱

Update(DataSet) Update(DataSet) Update(DataSet)

針對指定之 DataSet 中的每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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

參數

dataSet
DataSet 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.

範例

下列範例會使用衍生的類別OleDbDataAdapter來更新資料來源。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 OleDbCommandBuilder = New OleDbCommandBuilder(adapter)

        connection.Open()

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

        ' Code to modify data in DataSet here 

        adapter.Update(customers)

        Return customers
    End Using
End Function

備註

當應用程式呼叫Update方法時DbDataAdapter ,會檢查RowState屬性,並根據在中DataSet設定的索引順序,針對每個資料列反復執行必要的INSERT、UPDATE或DELETE子句。.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. 例如, 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必須控制語句類型序列的情況下呼叫方法(例如,在更新之前插入)。An application can call the GetChanges method in situations where you must control the sequence of statement types (for example, INSERTs before UPDATEs). 如需詳細資訊,請參閱使用 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. 不過,如果您設定 .NET Framework Data Provider SqlCommandBuilderOleDbCommandBuilder 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. 此層代邏輯需要在中DataSet出現索引鍵資料行資訊。This 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. 接著會使用UpdatedRowSource屬性的值重新整理資料列。 UpdateThe 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. 如果有輸出參數,則會將它們放在DataRow中。If there are output parameters, they are placed in the DataRow.

  6. 便會引發 OnRowUpdated 事件。The OnRowUpdated event is raised.

  7. 呼叫 AcceptChangesAcceptChanges is called.

與相關聯的DbDataAdapter每個命令,通常都有一個與其相關聯的 parameters 集合。Each command associated with the DbDataAdapter usually has a parameters collection associated with it. 參數會透過SourceColumn .NET Framework Data Provider Parameter類別的和SourceVersion屬性,對應至目前的資料列。Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a .NET Framework data provider's Parameter class. SourceColumnDbDataAdapter參考用DataTable來取得目前資料列之參數值的資料行。SourceColumn refers to a DataTable column that the DbDataAdapter 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 Framework Data Provider 類別的SourceVersion屬性會決定是要使用資料行值的原始、目前或建議版本。ParameterThe 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. 這項功能通常用來在 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.

注意

如果在更新資料列時發生錯誤,則會擲回例外狀況,並中止執行更新。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將的Continue屬性RowUpdatedEventArgs設定為。To continue the update operation without generating an exception within the RowUpdated event, set the Status property of the RowUpdatedEventArgs to Continue.

另請參閱

Update(DataRow[]) Update(DataRow[]) Update(DataRow[]) Update(DataRow[])

針對 DataSet 的指定陣列中每個插入、更新或刪除的資料列,執行個別的 INSERT、UPDATE 或 DELETE 陳述式,以更新資料庫中的值。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

參數

dataRows
DataRow[]

用來更新資料來源的 DataRow 物件陣列。An array of DataRow objects used to update the data source.

傳回

DataSet 中成功更新的資料列數目。The number of rows successfully updated from the DataSet.

例外狀況

要更新的 DataRow 不存在。No DataRow exists to update.

-或--or- 要更新的 DataTable 不存在。No DataTable exists to update.

-或--or- 要用來做為來源的 DataSet 不存在。No DataSet exists to use as a source.

嘗試執行 INSERT、UPDATE 或 DELETE 陳述式會造成沒有資料錄受影響。An attempt to execute an INSERT, UPDATE, or DELETE statement resulted in zero records affected.

範例

下列範例會使用衍生的類別OleDbDataAdapter來更新資料來源。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 OleDbCommandBuilder = New OleDbCommandBuilder(adapter)

        connection.Open()

        Dim customers As DataSet = 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

備註

當應用程式呼叫Update方法時DbDataAdapter ,會檢查RowState屬性,並根據在中DataSet設定的索引順序,針對每個資料列反復執行必要的INSERT、UPDATE或DELETE子句。.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. 例如, 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必須控制語句類型序列的情況下呼叫方法(例如,在更新之前插入)。An application can call the GetChanges method in situations where you must control the sequence of statement types (for example, INSERTs before UPDATEs). 如需詳細資訊,請參閱使用 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. 不過,如果您設定 .NET Framework Data Provider SqlCommandBuilderOleDbCommandBuilder 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. 此層代邏輯需要在中DataSet出現索引鍵資料行資訊。This 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. 接著會使用UpdatedRowSource屬性的值重新整理資料列。 UpdateThe 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, the first returned result is placed in the DataRow.

  5. 如果有輸出參數,則會將它們放在DataRow中。If there are output parameters, they are placed in the DataRow.

  6. 便會引發 OnRowUpdated 事件。The OnRowUpdated event is raised.

  7. 呼叫 AcceptChangesAcceptChanges is called.

與相關聯的DbDataAdapter每個命令,通常都有一個與其相關聯的 parameters 集合。Each command associated with the DbDataAdapter usually has a parameters collection associated with it. 參數會透過SourceColumn .NET Framework Data Provider Parameter類別的和SourceVersion屬性,對應至目前的資料列。Parameters are mapped to the current row through the SourceColumn and SourceVersion properties of a .NET Framework data provider's Parameter class. SourceColumnDbDataAdapter參考用DataTable來取得目前資料列之參數值的資料行。SourceColumn refers to a DataTable column that the DbDataAdapter 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 Framework Data Provider 類別的SourceVersion屬性會決定是要使用資料行值的原始、目前或建議版本。ParameterThe 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. 這項功能通常用來在 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.

注意

如果在更新資料列時發生錯誤,則會擲回例外狀況,並中止執行更新。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將的Continue屬性RowUpdatedEventArgs設定為。To continue the update operation without generating an exception within the RowUpdated event, set the Status property of the RowUpdatedEventArgs to Continue.

另請參閱

適用於