SqlDataSource.Delete 方法

定義

使用 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

傳回

Int32

表示從基礎資料庫刪除之資料列數的值。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 屬性,並 Delete 在使用資料繫結控制項時呼叫方法,例如 DropDownList (與其他控制項(例如和)不同, GridView DetailsView 這會自動填入參數,並 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 控制項的方法 SqlDataSourceIn 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提供方法以程式設計方式存取 Delete 方法。The Delete method is provided for programmatic access to the Delete method. 如果 SqlDataSource 控制項與資料繫結控制項相關聯,資料繫結控制項就會自動呼叫 Delete 方法。If the SqlDataSource control is associated with a data-bound control, the data-bound control automatically calls the Delete method.

Delete方法會委派給 Delete SqlDataSourceView 與控制項相關聯之物件的方法 SqlDataSourceThe Delete method delegates to the Delete method of the SqlDataSourceView object that is associated with the SqlDataSource control. 若要執行此作業,會 SqlDataSourceView DbCommand 使用 DeleteCommand 文字和任何相關聯的值來建立物件 DeleteParameters ,然後 DbCommand 針對基礎資料庫執行。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.

適用於

另請參閱