SqlDataSource.SelectCommand 屬性

定義

取得或設定 SQL 字串,SqlDataSource 控制項會用來擷取基礎資料庫的資料。

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

屬性值

SqlDataSource 用來擷取資料的 SQL 字串或預存程序名稱。

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何將文字設定 SelectCommand 為基本 SQL 查詢,以從 ODBC 相容的資料庫擷取資料,並將其顯示在 控制項中 GridView 。 第二個程式碼範例示範如何將文字設定為預存程式的名稱,並將 SelectCommandType 屬性設定 SelectCommandStoredProcedure 值,以從 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>

下列程式碼範例示範如何將文字設定為預存程式的名稱,並將 SelectCommandType 屬性設定 SelectCommandStoredProcedure 值,以從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.Odbc 為 或 System.Data.OleDb ,則 參數的預留位置為 '?' 。 如需參數化 SQL 查詢和命令的詳細資訊,請參閱 搭配 SqlDataSource 控制項使用參數

如果資料來源支援預存程式,此屬性 SelectCommand 可以是 SQL 字串或預存程式的名稱。

屬性 SelectCommand 會委派給 SelectCommandSqlDataSource 控制項相關聯之 SqlDataSourceView 物件的 屬性。

重要

基於安全性考慮, SelectCommand 屬性不會儲存為檢視狀態。 因為可以解碼用戶端上檢視狀態的內容,所以將資料庫結構的敏感性資訊儲存在檢視狀態可能會導致資訊洩漏弱點。

重要

值會插入參數中,而不需驗證,這是潛在的安全性威脅。 在執行查詢之前, Filtering 請使用 事件來驗證參數值。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

適用於

另請參閱