SqlDataAdapter.UpdateCommand 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定用於更新資料來源記錄的 Transact-SQL 語句或儲存程序。
public:
property System::Data::SqlClient::SqlCommand ^ UpdateCommand { System::Data::SqlClient::SqlCommand ^ get(); void set(System::Data::SqlClient::SqlCommand ^ value); };
[System.Data.DataSysDescription("DbDataAdapter_UpdateCommand")]
public System.Data.SqlClient.SqlCommand UpdateCommand { get; set; }
public System.Data.SqlClient.SqlCommand UpdateCommand { get; set; }
[<System.Data.DataSysDescription("DbDataAdapter_UpdateCommand")>]
member this.UpdateCommand : System.Data.SqlClient.SqlCommand with get, set
member this.UpdateCommand : System.Data.SqlClient.SqlCommand with get, set
Public Property UpdateCommand As SqlCommand
屬性值
SqlCommand A 用於Update(DataSet)更新資料庫中對應修改過的列DataSet的紀錄。
- 屬性
範例
以下範例會產生 , SqlDataAdapter 並設定 SelectCommand、 InsertCommand、 UpdateCommand 及 DeleteCommand 性質。 它假設你已經建立了 SqlConnection 物件。
public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);
adapter.SelectCommand = command;
// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
adapter.InsertCommand = command;
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand = command;
// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = command;
return adapter;
}
Public Function CreateCustomerAdapter( _
ByVal connection As SqlConnection) As SqlDataAdapter
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
' Create the SelectCommand.
Dim command As SqlCommand = New SqlCommand( _
"SELECT * FROM Customers " & _
"WHERE Country = @Country AND City = @City", connection)
' Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15)
command.Parameters.Add("@City", SqlDbType.NVarChar, 15)
adapter.SelectCommand = command
' Create the InsertCommand.
command = New SqlCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", connection)
' Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
adapter.InsertCommand = command
' Create the UpdateCommand.
command = New SqlCommand( _
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
"WHERE CustomerID = @oldCustomerID", connection)
' Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
Dim parameter As SqlParameter = command.Parameters.Add( _
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
adapter.UpdateCommand = command
' Create the DeleteCommand.
command = New SqlCommand( _
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection)
' Add the parameters for the DeleteCommand.
command.Parameters.Add( _
"@CustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
adapter.DeleteCommand = command
Return adapter
End Function
備註
在 Update期間,若未設定此屬性且主鍵資訊存在 DataSet於 , UpdateCommand 則可自動生成,並設定 SelectCommand 屬性並使用 SqlCommandBuilder。 接著,任何你未設定的額外指令會由 SqlCommandBuilder產生。 此生成邏輯要求關鍵欄位資訊必須存在於 DataSet中。 如需詳細資訊,請參閱使用 CommandBuilder 產生命令。
當 UpdateCommand 被指派給先前建立 SqlCommand的 時,則 SqlCommand 不會被複製。 它 UpdateCommand 會維持對先前建立 SqlCommand 物件的參考。
備註
如果執行此指令回傳列,更新後的列可能會合併,DataSet視你如何設定物件的 SqlCommandUpdatedRowSource 屬性而定。
對於你傳播到資料來源Update的每一欄,都應該在 、 UpdateCommand、 或 DeleteCommand上加入一個參數。InsertCommand
SourceColumn參數的屬性應該設為欄位名稱。 這表示參數的值不是手動設定的,而是從當前處理的列中特定欄位取出。