SqlDataSource.DeleteParameters SqlDataSource.DeleteParameters SqlDataSource.DeleteParameters SqlDataSource.DeleteParameters Property

定义

从与 DeleteCommand 控件相关联的 SqlDataSourceView 对象获取包含 SqlDataSource 属性所使用的参数的参数集合。Gets the parameters collection that contains the parameters that are used by the DeleteCommand property from the SqlDataSourceView object that is associated with the SqlDataSource control.

public:
 property System::Web::UI::WebControls::ParameterCollection ^ DeleteParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection DeleteParameters { get; }
member this.DeleteParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property DeleteParameters As ParameterCollection

属性值

ParameterCollection,它包含 DeleteCommand 属性所使用的参数。A ParameterCollection that contains the parameters used by the DeleteCommand property.

示例

下面的代码示例演示如何设置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使用数据DetailsView Delete GridView绑定控件 (如和) 时, 必须显式声明属性并调用方法 (与其他控件不同, 如和), 后者会自动填充参数和DropDownListDelete数据源控件进行调用)。You must explicitly declare DeleteParameters property and call the Delete method when using data-bound controls, such as DropDownList (unlike other controls, such as GridView and DetailsView, which automatically populate the parameters and call Delete 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>

注解

如果属性包含参数化 sql 查询, 则DeleteParameters集合包含与 SQL Parameter字符串中的参数占位符对应的任何对象。 DeleteCommandIf the DeleteCommand property contains a parameterized SQL query, the DeleteParameters collection contains any Parameter objects that correspond to the parameter placeholders in the SQL string.

备注

请确保绑定SqlDataSourceBoundField控件的数据绑定控件中的控件的名称与DeleteParameters集合中的任何参数名称都不匹配。Make sure that no BoundField controls in the data-bound control that you bind to the SqlDataSource control have names that match any parameter names in the DeleteParameters collection. 与绑定字段具有相同名称的参数将从 SQL 命令中排除, 并且可能会导致 "未提供参数" 错误。Parameters that have the same name as bound fields are excluded from the SQL command, and a "parameter was not supplied" error might result.

如果将CompareAllValues属性设置为值, 则会为数据的旧值和新值创建参数。 ConflictDetectionIf the ConflictDetection property is set to the CompareAllValues value, the parameters are created for both the old and new values of the data. 旧值的参数根据OldValuesParameterFormatString属性进行命名。The parameters for the old values are named according to the OldValuesParameterFormatString property.

根据 ADO.NET 提供程序, DeleteParameters集合中参数的顺序可能很重要。Depending on the ADO.NET provider, the order of the parameters in the DeleteParameters collection might be important. System.Data.OleDbSystem.Data.Odbc提供程序根据参数在参数化 SQL 查询中的显示顺序来关联集合中的参数。The System.Data.OleDb and System.Data.Odbc providers associate the parameters in the collection according to the order in which the parameters appear in the parameterized SQL query. 提供程序是SqlDataSource控件的默认 ADO.NET 提供程序, 它通过将参数名称与 SQL 查询中的占位符匹配来关联集合中的参数。 System.Data.SqlClientThe System.Data.SqlClient provider, which is the default ADO.NET provider for the SqlDataSource control, associates the parameters in the collection by matching the name of the parameter with the placeholder in the SQL query. 有关参数化 SQL 查询和命令的详细信息, 请参阅将参数与 SqlDataSource 控件一起使用For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

SqlDataSourceView属性检索与SqlDataSource控件相关联的对象所包含的属性。DeleteParameters DeleteParametersThe DeleteParameters property retrieves the DeleteParameters property that is contained by the SqlDataSourceView object that is associated with the SqlDataSource control.

重要

无需验证即可将值插入到参数中, 这是一个潜在的安全威胁。Values are inserted into parameters without validation, which is a potential security threat. Deleting使用事件在执行查询之前验证参数值。Use the Deleting event to validate parameter values before executing the query. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

适用于

另请参阅