SqlDataSource.Delete SqlDataSource.Delete SqlDataSource.Delete SqlDataSource.Delete Method

定义

使用 DeleteCommand SQL 字符串和 DeleteParameters 集合中的所有参数执行删除操作。Performs a delete operation using the DeleteCommand SQL string and any parameters that are in the DeleteParameters collection.

public:
 int Delete();
public int Delete ();
member this.Delete : unit -> int
Public Function Delete () As Integer

返回

一个值,该值表示从基础数据库中删除的行数。A value that represents the number of rows deleted from the underlying database.

异常

SqlDataSource 无法与基础数据源建立连接。The SqlDataSource cannot establish a connection with the underlying data source.

示例

下面的代码示例演示如何设置DeleteCommand文本以删除 Northwind 数据库中的订单。The following code example demonstrates how to set the DeleteCommand text to delete an order from the Northwind database. 最初, 从 Orders 表中检索数据, 并在DropDownList控件中显示数据。Initially, data is retrieved from the Orders table and displayed in a DropDownList control. DeleteParameters使用数据绑定GridView Delete DetailsView控件 (如和) 时, 必须显式声明属性并调用方法 (与其他控件 (如和) 不同, 后者会自动填充DropDownList参数, 并对Delete数据源控件调用方法)。You must explicitly declare the DeleteParameters property and call the Delete method when using data-bound controls, such as the DropDownList (unlike other controls, such as GridView and DetailsView, which automatically populate the parameters and call the Delete method on a data source control). 在此示例中, OnClick将事件委托给专用OnDelete事件处理程序, 该处理程序显式Delete SqlDataSource调用控件的方法。In this example, the OnClick event is delegated to the private OnDelete event handler, which explicitly calls the Delete method of the SqlDataSource control.

<%@Page  Language="C#" %>
<!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 OnDelete(Object sender, EventArgs e) {
    SqlDataSource1.Delete();
}
</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"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommand="SELECT OrderID FROM Orders"
                DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;">
                <DeleteParameters>
                    <asp:ControlParameter Name="OrderID" ControlId="DropDownList1" PropertyName="SelectedValue" />
                </DeleteParameters>
            </asp:SqlDataSource>

            <asp:DropDownList
                id="DropDownList1"
                runat="server"
                DataTextField="OrderID"
                DataValueField="OrderID"
                DataSourceID="SqlDataSource1">
            </asp:DropDownList>

            <asp:Button
                id="Button1"
                runat="server"
                Text="Delete Order"
                OnClick="OnDelete">
            </asp:Button>

        </form>
    </body>
</html>
<%@Page  Language="VB" %>
<!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_Delete(ByVal sender As Object, ByVal e As EventArgs)
    SqlDataSource1.Delete()
 End Sub 'On_Delete
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>ASP.NET Example</title>
</head>

    <body>
        <form id="form1" runat="server">

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommand="SELECT OrderID FROM Orders"
                DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;">
                <DeleteParameters>
                    <asp:ControlParameter Name="OrderID" ControlId="DropDownList1" PropertyName="SelectedValue" />
                </DeleteParameters>
            </asp:SqlDataSource>

            <asp:DropDownList
                id="DropDownList1"
                runat="server"
                DataTextField="OrderID"
                DataValueField="OrderID"
                DataSourceID="SqlDataSource1">
            </asp:DropDownList>

            <asp:Button
                id="Button1"
                runat="server"
                Text="Delete Order"
                OnClick="On_Delete">
            </asp:Button>

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

注解

在执行删除操作之前, OnDeleting将调用方法来Deleting引发事件。Before the delete operation is performed, the OnDeleting method is called to raise the Deleting event. 您可以处理此事件以检查参数的值, 并在执行删除操作之前执行任何预处理。You can handle this event to examine the values of the parameters and to perform any preprocessing before a delete operation.

操作完成后, OnDeleted调用方法来Deleted引发事件。After the operation completes, the OnDeleted method is called to raise the Deleted event. 可以处理此事件以检查任何返回值和错误代码, 并执行任何后续处理。You can handle this event to examine any return values and error codes and to perform any post-processing.

提供方法以编程方式访问Delete方法。 DeleteThe Delete method is provided for programmatic access to the Delete method. 如果控件与数据绑定控件关联, 数据绑定控件将自动调用 Delete 方法。 SqlDataSourceIf the SqlDataSource control is associated with a data-bound control, the data-bound control automatically calls the Delete method.

方法委托SqlDataSourceView Delete给与SqlDataSource控件相关联的对象的方法。 DeleteThe Delete method delegates to the Delete method of the SqlDataSourceView object that is associated with the SqlDataSource control. 若要执行该操作, SqlDataSourceView请使用DbCommand DeleteCommand文本和DbCommand任何关联DeleteParameters的值生成一个对象, 然后对基础数据库执行。To perform the operation, the SqlDataSourceView builds a DbCommand object using the DeleteCommand text and any associated DeleteParameters values, and then executes the DbCommand against the underlying database.

适用于

另请参阅