Share via


SqlCeCommandBuilder.GetDeleteCommand 方法

當應用程式呼叫 SqlCeDataAdapter 上的 Update 時取得自動產生的 SqlCeCommand 物件,需要此物件才能在資料庫上執行刪除。

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

語法

'宣告
Public Function GetDeleteCommand As SqlCeCommand
'用途
Dim instance As SqlCeCommandBuilder
Dim returnValue As SqlCeCommand

returnValue = instance.GetDeleteCommand()
public SqlCeCommand GetDeleteCommand()
public:
SqlCeCommand^ GetDeleteCommand()
member GetDeleteCommand : unit -> SqlCeCommand 
public function GetDeleteCommand() : SqlCeCommand

傳回值

型別:System.Data.SqlServerCe.SqlCeCommand
自動產生的 SqlCeCommand 物件,執行刪除時需要此物件。

備註

應用程式可以針對參考或疑難排解用途而使用 GetDeleteCommand 方法,因為它會傳回要執行的 SqlCeCommand 物件。

您也可以將 GetDeleteCommand 用來當做修改命令的基礎。例如,您可以呼叫 GetDeleteCommand、修改其中一個屬性,然後在 SqlCeDataAdapter 上明確地設定該命令。

在第一次產生 SQL 陳述式之後,如果應用程式使用任何方式來變更陳述式,則它必須明確地呼叫 RefreshSchema()。否則,GetDeleteCommand 仍會使用前一個陳述式的資訊,而該項資訊可能並不正確。當應用程式呼叫 UpdateGetDeleteCommand 時,首先會產生 SQL 陳述式。

範例

下列範例顯示如何呼叫 SqlCeCommandBuilderGetDeleteCommand 方法。

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

    Dim cmd As SqlCeCommand = conn.CreateCommand()
    cmd.CommandText = "SELECT * FROM employees"

    Dim adp As New SqlCeDataAdapter(cmd)

    Dim cb As New SqlCeCommandBuilder()
    cb.DataAdapter = adp

    MessageBox.Show(cb.GetUpdateCommand().CommandText)
    MessageBox.Show(cb.GetInsertCommand().CommandText)
    MessageBox.Show(cb.GetDeleteCommand().CommandText)

    Dim ds As New DataSet("test")
    adp.Fill(ds)

    ' Modify the contents of the DataSet
    '
    ds.Tables(0).Rows(0)("First Name") = "Joe"

    adp.Update(ds)

Catch e1 As Exception
    Console.WriteLine(e1.ToString())
End Try
try
{
    SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT * FROM employees";

    SqlCeDataAdapter adp = new SqlCeDataAdapter(cmd);

    SqlCeCommandBuilder cb = new SqlCeCommandBuilder();
    cb.DataAdapter = adp;

    MessageBox.Show(cb.GetUpdateCommand().CommandText);
    MessageBox.Show(cb.GetInsertCommand().CommandText);
    MessageBox.Show(cb.GetDeleteCommand().CommandText);

    DataSet ds = new DataSet("test");
    adp.Fill(ds);

    // Modify the contents of the DataSet
    //
    ds.Tables[0].Rows[0]["First Name"] = "Joe";

    adp.Update(ds);

}
catch (Exception e1)
{
    Console.WriteLine(e1.ToString());
}

請參閱

參考

SqlCeCommandBuilder 類別

GetDeleteCommand 多載

System.Data.SqlServerCe 命名空間