SqlDataSource.DeleteCommand 属性

定义

获取或设置 SqlDataSource 控件从基础数据库删除数据所用的 SQL 字符串。

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

属性值

SqlDataSource 删除数据所用的 SQL 字符串。

示例

下面的代码示例演示如何设置 DeleteCommand 文本以从 Northwind 数据库 Orders 表中删除订单。 数据从 Orders 表中检索并显示在 控件中 GridViewGridView当 属性设置为 true时, AutoGenerateDeleteButton 会自动呈现“删除”按钮。 此外,单击“ 删除 ”按钮时, 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.OdbcSystem.Data.OleDb,则参数的占位符为 '?'。 有关参数化 SQL 查询和命令的详细信息,请参阅 在 SqlDataSource 控件中使用参数

如果数据库支持存储过程,则 DeleteCommand 属性可以是 SQL 字符串或存储过程的名称。

属性DeleteCommand委托给DeleteCommandSqlDataSource 控件关联的 对象的 属性SqlDataSourceView

重要

出于安全目的,属性 DeleteCommand 不存储在视图状态中。 由于可以在客户端上解码视图状态的内容,因此在视图状态中存储有关数据库结构的敏感信息可能会导致信息泄露漏洞。

适用于

另请参阅