共用方式為


SqlDataAdapter.DeleteCommand 屬性

定義

取得或設定要從資料集刪除資料錄的 Transact-SQL 陳述式或預存程序。

public:
 property Microsoft::Data::SqlClient::SqlCommand ^ DeleteCommand { Microsoft::Data::SqlClient::SqlCommand ^ get(); void set(Microsoft::Data::SqlClient::SqlCommand ^ value); };
public Microsoft.Data.SqlClient.SqlCommand DeleteCommand { get; set; }
member this.DeleteCommand : Microsoft.Data.SqlClient.SqlCommand with get, set
Public Property DeleteCommand As SqlCommand

屬性值

SqlCommand 期間使用的 Update(DataSet),用來刪除資料庫中的資料錄,其對應到 DataSet 中已刪除的資料列。

範例

下列範例會建立 並 SqlDataAdapter 設定 SelectCommandInsertCommandUpdateCommandDeleteCommand 屬性。 它假設您已經建立 SqlConnection 物件。

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
    }
    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;
    }
}

備註

在 期間 Update ,如果未設定此屬性且主鍵資訊存在於 中 DataSet ,則如果您設定 SelectCommand 屬性並使用 SqlCommandBuilderDeleteCommand 則會自動產生 。 然後,您未設定的任何其他命令都會由 SqlCommandBuilder 產生。 此產生邏輯需要中 DataSet 存在索引鍵資料行資訊。 如需詳細資訊,請參閱使用 CommandBuilder 產生命令

當 指派給先前建立 SqlCommand 的 時 DeleteCommandSqlCommand 不會複製 。 會 DeleteCommand 維護先前建立 SqlCommand 之 物件的參考。

針對您傳播至 上 Update 資料來源的每個資料行,應該將 參數加入 InsertCommandUpdateCommandDeleteCommand 。 參數 SourceColumn 的 屬性應該設定為數據行的名稱。 這表示參數的值不是手動設定,而是取自目前處理資料列中的特定資料行。

適用於