Share via


SqlCeDataAdapter.DeleteCommand 屬性

取得或設定 SQL 陳述式從資料集刪除資料錄。

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
Public Property DeleteCommand As SqlCeCommand
    Get
    Set
'用途
Dim instance As SqlCeDataAdapter
Dim value As SqlCeCommand

value = instance.DeleteCommand

instance.DeleteCommand = value
public SqlCeCommand DeleteCommand { get; set; }
public:
property SqlCeCommand^ DeleteCommand {
    SqlCeCommand^ get ();
    void set (SqlCeCommand^ value);
}
member DeleteCommand : SqlCeCommand with get, set
function get DeleteCommand () : SqlCeCommand
function set DeleteCommand (value : SqlCeCommand)

屬性值

型別:System.Data.SqlServerCe.SqlCeCommand
用於 Update 期間的 SqlCeCommand,可刪除資料來源中與 DataSet 中的已刪除資料列相對應的資料錄。

備註

在呼叫 Update 期間如果此屬性尚未設定,且 DataSet 中存在主索引鍵資訊,則如果設定 SelectCommand 屬性並使用 SqlCeCommandBuilder,則可以自動產生 DeleteCommand。然後,任何未設定的其他命令會由 SqlCeCommandBuilder 產生。這個產生邏輯需要 DataSet 中存在索引鍵資料行資訊。

當 DeleteCommand 指派給先前建立的 SqlCeCommand 時,並不會複製 SqlCeCommand。DeleteCommand 會維護先前所建立之 SqlCeCommand 物件的參考。

範例

下列範例會建立 SqlCeDataAdapter,並設定其部分屬性。

Dim cmd As SqlCeCommand = Nothing
Dim adp As SqlCeDataAdapter = Nothing

Try
    adp = New SqlCeDataAdapter()
    Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf")

    ' Create the SelectCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "SELECT * FROM Orders WHERE [Ship Country] = @country AND [Ship City] = @city"
    cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15)
    cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15)

    cmd.Parameters("@country").Value = "UK"
    cmd.Parameters("@city").Value = "London"

    adp.SelectCommand = cmd

    ' Create the DeleteCommand
    '
    cmd = conn.CreateCommand()
    cmd.CommandText = "DELETE FROM Orders WHERE [Order ID] = @orderID"

    Dim p As SqlCeParameter = cmd.Parameters.Add("@orderID", SqlDbType.NChar, 5, "Order ID")
    p.SourceVersion = DataRowVersion.Original

    adp.DeleteCommand = cmd

    ' Populate the dataset with the results from the SELECT statement
    '
    Dim ds As New DataSet()
    adp.Fill(ds)

    ' Modify the dataset
    '
    MessageBox.Show("Number of rows: " & ds.Tables(0).Rows.Count)

    ' Delete some rows
    '
    ds.Tables(0).Rows(3).Delete()
    ds.Tables(0).Rows(4).Delete()

    ' This will execute two DELETE statements 
    '
    adp.Update(ds.Tables(0))
Catch e As Exception
    MessageBox.Show(e.Message)
Finally
    If Not Nothing Is adp.SelectCommand Then
        adp.SelectCommand.Dispose()
    End If
    If Not Nothing Is adp.DeleteCommand Then
        adp.DeleteCommand.Dispose()
    End If
End Try
SqlCeCommand cmd = null;
SqlCeDataAdapter adp = null;

try
{
    adp = new SqlCeDataAdapter();
    SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");

    // Create the SelectCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT * FROM Orders WHERE [Ship Country] = @country AND [Ship City] = @city";
    cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15);
    cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15);

    cmd.Parameters["@country"].Value = "UK";
    cmd.Parameters["@city"].Value = "London";

    adp.SelectCommand = cmd;

    // Create the DeleteCommand
    //
    cmd = conn.CreateCommand();
    cmd.CommandText = "DELETE FROM Orders WHERE [Order ID] = @orderID";

    SqlCeParameter p = cmd.Parameters.Add("@orderID", SqlDbType.NChar, 5, "Order ID");
    p.SourceVersion = DataRowVersion.Original;

    adp.DeleteCommand = cmd;

    // Populate the dataset with the results from the SELECT statement
    //
    DataSet ds = new DataSet();
    adp.Fill(ds);

    // Modify the dataset
    //
    MessageBox.Show("Number of rows: " + ds.Tables[0].Rows.Count);

    // Delete some rows
    //
    ds.Tables[0].Rows[3].Delete();
    ds.Tables[0].Rows[4].Delete();

    // This will execute two DELETE statements 
    //
    adp.Update(ds.Tables[0]);
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
finally
{
    if (null != adp.SelectCommand) adp.SelectCommand.Dispose();
    if (null != adp.DeleteCommand) adp.DeleteCommand.Dispose();
}

請參閱

參考

SqlCeDataAdapter 類別

System.Data.SqlServerCe 命名空間

InsertCommand

SelectCommand

UpdateCommand