SqlDataSourceView.SelectCommand 속성

정의

SqlDataSourceView 개체에서 내부 데이터베이스의 데이터를 검색하는 데 사용하는 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

속성 값

String

SqlDataSourceView에서 데이터를 검색하는 데 사용하는 SQL 문자열입니다.

예제

이 섹션에는 두 코드 예제가 있습니다. 첫 번째 코드 예제에서는 설정 하는 방법에 설명 합니다 SelectCommand Microsoft SQL Server 데이터베이스에서 데이터를 검색에서 표시 하는 기본 SQL 쿼리 텍스트를 DropDownList 컨트롤. 두 번째 코드 예제에서는 설정 하는 방법에 설명 합니다 SelectCommand 텍스트에 표시 하는 SQL Server 데이터베이스에서 데이터를 검색 하는 저장된 프로시저의 이름으로는 DropDownList합니다.

다음 코드 예제에는 설정 하는 방법을 보여 줍니다.는 SelectCommand SQL Server 데이터베이스에서 데이터를 검색에서 표시 하는 기본 SQL 쿼리 텍스트를 DropDownList 제어 합니다. 합니다 Button 하 고 TextBox 컨트롤에서 선택한 사용자에 대 한 주소를 업데이트 하기 위한 간단한 인터페이스로 제공 됩니다는 DropDownList합니다.

<%@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 On_Click(Object source, EventArgs e) {
    try {
        SqlDataSource1.Update();
    }
    catch (Exception except) {
        // Handle the Exception.
    }

    Label2.Text="The record was updated successfully!";
 }
</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 EmployeeID, LastName, Address FROM Employees"
          UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID">
          <UpdateParameters>
              <asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
              <asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
          </UpdateParameters>
      </asp:SqlDataSource>

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

      <br />
      <asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
        AssociatedControlID="TextBox1" />
      <asp:TextBox id="TextBox1" runat="server" />
      <asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />

      <br /><asp:Label id="Label2" runat="server" Text="" />

    </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_Click(ByVal source As Object, ByVal e As EventArgs)
    Try
        SqlDataSource1.Update()
    Catch except As Exception
        ' Handle the Exception.
    End Try

    Label2.Text="The record was updated successfully!"

 End Sub 'On_Click
</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 EmployeeID, LastName, Address FROM Employees"
          UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID">
          <UpdateParameters>
              <asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
              <asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
          </UpdateParameters>
      </asp:SqlDataSource>

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

      <br />
      <asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
        AssociatedControlID="TextBox1" />
      <asp:TextBox id="TextBox1" runat="server" />
      <asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />

      <br /><asp:Label id="Label2" runat="server" Text="" />
    </form>
  </body>
</html>

다음 코드 예제에서는 설정 하는 방법에 설명 합니다 SelectCommand 텍스트에 표시 하는 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>

설명

데이터베이스 제품마다 다양한 SQL을 사용하기 때문에 SQL 문자열의 구문은 현재 사용 중인 ADO.NET 공급자(ProviderName 속성으로 식별 가능)에 따라 달라집니다. SQL 문자열이 매개 변수가 있는 쿼리나 명령인 경우 매개 변수의 자리 표시자도 사용 중인 ADO.NET 공급자에 따라 달라집니다. 예를 들어 공급자가 합니다 System.Data.SqlClient에 대 한 기본 공급자 인를 SqlDataSource 클래스는 매개 변수의 자리 표시자는 '@parameterName'. 그러나 공급자로 설정 된 경우는 System.Data.Odbc 또는 System.Data.OleDb, 매개 변수의 자리 표시자는 '?'합니다. 매개 변수가 있는 SQL 쿼리 및 명령에 대 한 자세한 내용은 참조 하세요. SqlDataSource 컨트롤을 사용 하 여 매개 변수를 사용 하 여입니다.

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

중요

보다 안전한 SQL 문 보다 저장된 프로시저를 사용 하는 것은 SelectCommand 속성입니다.

값을 SelectCommand 속성은 뷰 상태에 저장 됩니다.

적용 대상

추가 정보