SqlDataSource.DeleteCommand SqlDataSource.DeleteCommand SqlDataSource.DeleteCommand SqlDataSource.DeleteCommand Property

定义

获取或设置 SqlDataSource 控件从基础数据库删除数据所用的 SQL 字符串。Gets or sets the SQL string that the SqlDataSource control uses to delete data from the underlying database.

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 字符串。An SQL string that the SqlDataSource uses to delete data.

示例

下面的代码示例演示如何设置DeleteCommand文本以从 Northwind 数据库 Orders 表中删除订单。The following code example demonstrates how to set the DeleteCommand text to delete an order from the Northwind database Orders table. 将从 Orders 表中检索数据, 并在GridView控件中显示数据。Data is retrieved from the Orders table and displayed in a GridView control. GridView AutoGenerateDeleteButton属性设置 为true时, 自动呈现删除按钮。The GridView renders a Delete button automatically when the AutoGenerateDeleteButton property is set to true. 此外, 当单击 "删除" 按钮时, GridView控件将自动填充DeleteParameters该集合并调用Delete方法。Additionally, when the Delete button is clicked, the GridView control automatically populates the DeleteParameters collection and calls the Delete method. 最后, 由于此代码示例删除数据, 因此添加了一个事件处理程序, 以便在执行Delete操作之前尝试将数据库备份到磁盘。Finally, because this code example deletes data, an event handler is added to attempt to back up the database to disk before the Delete operation is performed.

<%@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>

注解

表示 SQL 查询或存储过程的名称, Delete由方法使用。 DeleteCommandThe DeleteCommand represents an SQL query or the name of a stored procedure, and is used by the Delete method.

由于不同的数据库产品使用不同类型的 SQL,因此 SQL 字符串的语法取决于当前正在使用的 ADO.NET 提供程序,该提供程序由 ProviderName 属性标识。Because different database products use different varieties of SQL, the syntax of the SQL string depends on the current ADO.NET provider being used, which is identified by the ProviderName property. 如果 SQL 字符串是参数化的查询或命令, 则该参数的语法还取决于所使用的 ADO.NET 提供程序。If the SQL string is a parameterized query or command, the syntax of the parameter also depends on the ADO.NET provider being used. 例如, 如果提供程序是System.Data.SqlClient, 它是SqlDataSource类的默认提供程序, 则参数的语法为'@parameterName'For example, if the provider is the System.Data.SqlClient, which is the default provider for the SqlDataSource class, the syntax of the parameter is '@parameterName'. 但是, 如果提供程序设置为System.Data.OdbcSystem.Data.OleDb, 则参数的占位符为'?'However, if the provider is set to the System.Data.Odbc or System.Data.OleDb, the placeholder of the parameter is '?'. 有关参数化 SQL 查询和命令的详细信息, 请参阅将参数与 SqlDataSource 控件一起使用For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

如果DeleteCommand数据库支持存储过程, 则该属性可以是 SQL 字符串或存储过程的名称。The DeleteCommand property can be an SQL string or the name of a stored procedure, if the database supports stored procedures.

属性委托SqlDataSourceView DeleteCommand给与SqlDataSource控件相关联的对象的属性。 DeleteCommandThe DeleteCommand property delegates to the DeleteCommand property of the SqlDataSourceView object that is associated with the SqlDataSource control.

重要

出于安全考虑, DeleteCommand属性不会存储在视图状态中。For security purposes, the DeleteCommand property is not stored in view state. 由于可以对客户端上的视图状态的内容进行解码, 因此在视图状态中存储有关数据库结构的敏感信息可能会导致信息泄露漏洞。Because it is possible to decode the contents of view state on the client, storing sensitive information about the database structure in view state could result in an information disclosure vulnerability.

适用于

另请参阅