SqlDataSourceView.SelectParameters 属性

定义

获取参数集合,该集合包含由 SelectCommand 属性使用的参数。

public:
 property System::Web::UI::WebControls::ParameterCollection ^ SelectParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
public System.Web.UI.WebControls.ParameterCollection SelectParameters { get; }
member this.SelectParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property SelectParameters As ParameterCollection

属性值

ParameterCollection

ParameterCollection,它包含 SelectCommand 属性所使用的参数。

示例

下面的代码示例演示如何通过将属性设置为SelectCommandSQL查询,从 Microsoft SQL Server 中的 Northwind 数据库中检索数据。 SQL查询参数化,属性中的SelectCommand占位符与ControlParameter添加到集合的对象SelectParameters匹配。 这样,绑定到控件ControlParameterDropDownList控件充当控件中显示的ListBox内容的筛选器。

备注

由于参数在 WHERE 子句中使用, SelectParameters 因此此代码示例中的属性的功能等效于使用 FilterExpressionFilterParameters 属性。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <p><asp:dropdownlist
          id="DropDownList1"
          runat="server"
          autopostback="True">
          <asp:listitem selected="True">Sales Representative</asp:listitem>
          <asp:listitem>Sales Manager</asp:listitem>
          <asp:listitem>Vice President, Sales</asp:listitem>
      </asp:dropdownlist></p>

      <asp:sqldatasource
          id="SqlDataSource1"
          runat="server"
          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
          <selectparameters>
              <asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
          </selectparameters>
      </asp:sqldatasource>

      <p><asp:listbox
          id="ListBox1"
          runat="server"
          datasourceid="SqlDataSource1"
          datatextfield="LastName">
      </asp:listbox></p>

    </form>
  </body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <p><asp:dropdownlist
          id="DropDownList1"
          runat="server"
          autopostback="True">
          <asp:listitem selected="True">Sales Representative</asp:listitem>
          <asp:listitem>Sales Manager</asp:listitem>
          <asp:listitem>Vice President, Sales</asp:listitem>
      </asp:dropdownlist></p>

      <asp:sqldatasource
          id="SqlDataSource1"
          runat="server"
          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
          <selectparameters>
              <asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
          </selectparameters>
      </asp:sqldatasource>

      <p><asp:listbox
          id="ListBox1"
          runat="server"
          datasourceid="SqlDataSource1"
          datatextfield="LastName">
      </asp:listbox></p>

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

注解

SelectCommand如果属性包含参数化SQL查询,则SelectParameters集合包含与SQL字符串中的参数占位符对应的任何Parameter对象。

根据 ADO.NET 提供程序,集合中的 SelectParameters 参数顺序可能很重要。 这些System.Data.OleDb参数和System.Data.Odbc提供程序根据参数化SQL查询中显示的顺序关联集合中的参数。 该System.Data.SqlClient提供程序是控件的默认 ADO.NET 提供程序SqlDataSource,通过将参数的名称与SQL查询中的占位符别名匹配来关联集合中的参数。 有关参数化SQL查询和命令的详细信息,请参阅将 Parameters 与 SqlDataSource 控件配合使用

适用于

另请参阅