共用方式為


SqlDataSource.DeleteCommand 屬性

定義

取得或設定 SQL 字串,SqlDataSource 控制項會用來刪除基礎資料庫的資料。

public:
 property System::String ^ DeleteCommand { System::String ^ get(); void set(System::String ^ value); };
public string DeleteCommand { get; set; }
member this.DeleteCommand : string with get, set
Public Property DeleteCommand As String

屬性值

SQL 字串,SqlDataSource 會用來刪除資料。

範例

下列程式代碼範例示範如何設定 DeleteCommand 文字,以從 Northwind 資料庫 Orders 數據表中刪除訂單。 數據是從 Orders 數據表擷取,並顯示在控件中 GridView 。 當 屬性設定為 trueAutoGenerateDeleteButton,會自動GridView轉譯 [刪除] 按鈕。 此外,按兩下 [ 刪除] 按鈕時, GridView 控制件會自動填入 DeleteParameters 集合並呼叫 Delete 方法。 最後,由於此程式代碼範例會刪除數據,因此會新增事件處理程式,以在執行作業之前 Delete 嘗試將資料庫備份至磁碟。

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
private void OnRecordDeleting(Object source, SqlDataSourceCommandEventArgs e) {    
    // Cancel the delete operation if the checkbox is not checked.
    if (!CheckBox1.Checked) {
        e.Cancel = true;
        Label1.Text = "The command was cancelled because the CheckBox was not checked.";
    }
 }

private void OnRecordDeleted(object source, SqlDataSourceStatusEventArgs e) {
    Label1.Text = e.AffectedRows + " row(s) were deleted";
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:SqlDataSource
            id="SqlDataSource1"
            runat="server"
            DataSourceMode="DataSet"
            ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
            SelectCommand="SELECT * FROM Orders"            
            DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
            OnDeleting="OnRecordDeleting"
            OnDeleted="OnRecordDeleted">
        </asp:SqlDataSource>
        <br />
       <asp:CheckBox 
         id="CheckBox1" 
         runat="server"
         autopostback="true"
         text="Check To Delete Data" />
        <br />
        <br />

        <asp:GridView
            id="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataKeyNames="OrderID"
            AutoGenerateDeleteButton="True"
            AllowPaging="True"
            PageSize="20"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField HeaderText="Order ID" DataField="OrderID" />
                <asp:BoundField HeaderText="Customer" DataField="CustomerID" />
                <asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
                <asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />
            </Columns>
        </asp:GridView>

        <asp:Label
            id="Label1"
            runat="server">
        </asp:Label>

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
 Sub On_Record_Deleting(ByVal source As Object, ByVal e As SqlDataSourceCommandEventArgs)
     ' Cancel the delete operation if the checkbox is not checked.
     If Not CheckBox1.Checked 
            e.Cancel = True
            Label1.Text = "The command was cancelled because the CheckBox was not checked."
     End If

End Sub 'On_Record_Deleting

Sub On_Record_Deleted(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
    Label1.Text = e.AffectedRows & " row(s) were deleted"

End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

        <asp:SqlDataSource
            id="SqlDataSource1"
            runat="server"
            DataSourceMode="DataSet"
            ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
            SelectCommand="SELECT * FROM Orders"
            DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
            OnDeleting="On_Record_Deleting"
            OnDeleted="On_Record_Deleted">
        </asp:SqlDataSource>
        <br />

       <asp:CheckBox 
         id="CheckBox1" 
         runat="server"
         autopostback="true"
         text="Check To Delete Data" />
        <br />
        <br />

        <asp:GridView
            id="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataKeyNames="OrderID"
            AutoGenerateDeleteButton="True"
            AllowPaging="True"
            PageSize="20"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField HeaderText="Order ID" DataField="OrderID" />
                <asp:BoundField HeaderText="Customer" DataField="CustomerID" />
                <asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
                <asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />
            </Columns>
        </asp:GridView>

        <asp:Label
            id="Label1"
            runat="server">
        </asp:Label>

    </form>
  </body>
</html>

備註

DeleteCommand表示 SQL 查詢或預存程式的名稱,並由方法使用Delete

由於不同的資料庫產品使用不同的 SQL,SQL 字串的語法須視目前所使用的 ADO.NET 提供者 (可由 ProviderName 屬性識別) 而定。 如果 SQL 字串是參數化查詢或命令,參數的語法也會取決於所使用的 ADO.NET 提供者。 例如,如果提供者是 System.Data.SqlClient,這是 類別的預設提供者 SqlDataSource ,則 參數的語法為 '@parameterName'。 不過,如果提供者設定 System.Data.Odbc 為 或 System.Data.OleDb,則 參數的佔位元為 '?'。 如需參數化 SQL 查詢和命令的詳細資訊,請參閱 搭配 SqlDataSource 控制項使用參數

如果資料庫支援預存程式,屬性 DeleteCommand 可以是 SQL 字串或預存程式的名稱。

屬性DeleteCommand會委派給DeleteCommandSqlDataSource 控件相關聯之 SqlDataSourceView 對象的屬性。

重要

基於安全性目的, DeleteCommand 屬性不會儲存在檢視狀態中。 因為可以譯碼用戶端上檢視狀態的內容,所以將資料庫結構的敏感性資訊儲存在檢視狀態可能會導致資訊洩漏弱點。

適用於

另請參閱