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

속성 값

SqlDataSource 에서 데이터를 검색하는 데 사용되는 SQL 문자열이나 저장 프로시저 이름입니다.

예제

이 섹션에는 두 코드 예제가 있습니다. 첫 번째 코드 예제에서는 설정 하는 방법에 설명 합니다 SelectCommand 텍스트에 표시 하는 ODBC 호환 데이터베이스에서 데이터를 검색 하는 기본 SQL 쿼리를를 GridView 제어 합니다. 두 번째 코드 예제에는 설정 하는 방법을 보여 줍니다.는 SelectCommand 저장된 프로시저의 이름으로 텍스트 및 SelectCommandType 속성을를 StoredProcedure Microsoft SQL Server 데이터베이스에서 데이터를 검색에서 표시 하는 값을 DropDownList 컨트롤입니다.

두 예제에는 명시적으로 호출할 필요가 없습니다를 Select 메서드를 통해 데이터 원본에 연결 된 데이터 바인딩된 컨트롤을 제어 하기 때문에 DataSourceID 속성이 자동으로 호출를 Select 합니다 중메서드PreRender 단계.

다음 코드 예제에서는 설정 하는 방법에 설명 합니다 SelectCommand 텍스트에 표시 하는 ODBC 호환 데이터베이스에서 데이터를 검색 하는 기본 SQL 쿼리를를 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.Odbc 또는 System.Data.OleDb, 매개 변수의 자리 표시자는 '?'합니다. 매개 변수가 있는 SQL 쿼리 및 명령에 대 한 자세한 내용은 참조 하세요. SqlDataSource 컨트롤을 사용 하 여 매개 변수를 사용 하 여입니다.

SelectCommand 속성 수는 SQL 문자열이 나 저장된 프로시저의 이름을 데이터 원본에서 저장된 프로시저를 지 원하는 경우.

SelectCommand 에 위임 하는 속성을 SelectCommand 의 속성을 SqlDataSourceView 개체와 연결 된는 SqlDataSource 컨트롤.

중요

보안상의 이유로 SelectCommand 속성 저장 되지 않습니다 보기 상태입니다. 클라이언트에서 보기 상태의 내용이 디코딩할 수 이기 때문에 데이터베이스 구조에 대 한 중요 한 정보를 보기 저장 상태 발생할 수 정보 공개 취약성이 있습니다.

중요

값 매개 변수 유효성 검사는 잠재적인 보안 위협을 없이 삽입 됩니다. 사용 된 Filtering 이벤트 쿼리를 실행 하기 전에 매개 변수 값의 유효성 검사를 합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.

적용 대상

추가 정보