SqlDataSource.SelectCommand 属性

定义

获取或设置 SqlDataSource 控件从基础数据库检索数据所用的 SQL 字符串。

public:
 property System::String ^ SelectCommand { System::String ^ get(); void set(System::String ^ value); };
public string SelectCommand { get; set; }
member this.SelectCommand : string with get, set
Public Property SelectCommand As String

属性值

一个 SQL 字符串或存储过程的名称,SqlDataSource 使用它检索数据。

示例

本部分包含两个代码示例。 第一个代码示例演示如何将 SelectCommand 文本设置为基本 SQL 查询,以从符合 ODBC 的数据库检索数据并将其显示在控件中 GridView 。 第二个代码示例演示如何将SelectCommand文本设置为存储过程的名称,SelectCommandType将 属性设置为StoredProcedure值,以便从 Microsoft SQL Server 数据库中检索数据并将其显示在 控件中DropDownList

在这两个示例中,都无需显式调用 Select 方法,因为通过 DataSourceID 属性附加到数据源控件的数据绑定控件将在阶段自动PreRender调用 Select 方法。

下面的代码示例演示如何将 SelectCommand 文本设置为基本 SQL 查询,以便从符合 ODBC 的数据库检索数据并将其显示在控件中 GridView

<!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>
    <!-- This example uses a Northwind database that is hosted by an ODBC-compliant
         database. To run this sample, create an ODBC DSN to any database that hosts
         the Northwind database, including Microsoft SQL Server or Microsoft Access,
         change the name of the DSN in the ConnectionString, and view the page.
    -->

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

      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          ProviderName="System.Data.Odbc"
          DataSourceMode="DataSet"
          ConnectionString="dsn=myodbc3dsn;"
          SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:SqlDataSource>

      <asp:GridView
          id="GridView1"
          runat="server"
          AllowSorting="True"
          DataSourceID="SqlDataSource1">
      </asp:GridView>

    </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">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <!-- This example uses a Northwind database that is hosted by an ODBC-compliant
         database. To run this sample, create an ODBC DSN to any database that hosts
         the Northwind database, including Microsoft SQL Server or Microsoft Access,
         change the name of the DSN in the ConnectionString, and view the page.
    -->
    <form id="form1" runat="server">

      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          ProviderName="System.Data.Odbc"
          DataSourceMode="DataSet"
          ConnectionString="dsn=myodbc3dsn;"
          SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
      </asp:SqlDataSource>

      <asp:GridView
          id="GridView1"
          runat="server"
          AllowSorting="True"
          DataSourceID="SqlDataSource1">
      </asp:GridView>

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

下面的代码示例演示如何将SelectCommand文本设置为存储过程的名称,SelectCommandType将 属性设置为 StoredProcedure 值,以便从 SQL Server 数据库中检索数据并将其显示在 控件中DropDownList。 如果数据源支持存储过程,则 SelectCommand 属性可以是 SQL 查询或存储过程的名称。

<!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">

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

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommandType="StoredProcedure"                
                SelectCommand="sp_lastnames">
            </asp:SqlDataSource>

            <!--
                The sp_lastnames stored procedure is
                CREATE PROCEDURE sp_lastnames AS
                   SELECT LastName FROM Employees
                GO
            -->

        </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">

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

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommandType = "StoredProcedure"
                SelectCommand="sp_lastnames">
            </asp:SqlDataSource>

            <!--
                The sp_lastnames stored procedure is
                CREATE PROCEDURE sp_lastnames AS
                   SELECT LastName FROM Employees
                GO
            -->

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

注解

SelectCommand属性表示 SQL 查询或存储过程的名称,并由 方法用于Select从SQL Server数据库中检索数据。 如果在 Select 命令中使用星号 (*) 来选择所有列,并且使用自动生成代码来执行更新或删除操作,请确保列的名称中没有空格。

由于不同的数据库产品使用不同类型的 SQL,因此 SQL 字符串的语法取决于当前正在使用的 ADO.NET 提供程序,该提供程序由 ProviderName 属性标识。 如果 SQL 字符串是一个参数化查询或命令,则参数的占位符还取决于所使用的 ADO.NET 提供程序。 例如,如果提供程序是 System.Data.SqlClient,这是 类的默认提供程序 SqlDataSource ,则 参数的占位符为 '@parameterName'。 但是,如果提供程序设置为 System.Data.OdbcSystem.Data.OleDb,则参数的占位符为 '?'。 有关参数化 SQL 查询和命令的详细信息,请参阅 对 SqlDataSource 控件使用参数

如果数据源支持存储过程,则 SelectCommand 属性可以是 SQL 字符串或存储过程的名称。

属性SelectCommand委托给SelectCommandSqlDataSource 控件关联的 对象的 属性SqlDataSourceView

重要

出于安全目的, SelectCommand 不存储 属性为视图状态。 由于可以在客户端上解码视图状态的内容,因此在视图状态中存储有关数据库结构的敏感信息可能会导致信息泄露漏洞。

重要

值在没有验证的情况下插入到参数中,这是一种潜在的安全威胁。 在执行查询之前, Filtering 使用 事件验证参数值。 有关详细信息,请参阅脚本侵入概述

适用于

另请参阅