Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Propriedade SqlDataSource.SelectCommand

 

Obtém ou define a cadeia de caracteres SQL que o controle SqlDataSource usa para recuperar dados do banco de dados subjacente.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (em System.Web.dll)

public string SelectCommand { get; set; }

Valor da Propriedade

Type: System.String

Uma cadeia de caracteres SQL ou o nome de um procedimento armazenado que o SqlDataSource usa para recuperar dados.

The P:System.Web.UI.WebControls.SqlDataSource.SelectCommand property represents an SQL query or the name of a stored procedure, and is used by the M:System.Web.UI.WebControls.SqlDataSource.Select(System.Web.UI.DataSourceSelectArguments) method to retrieve data from a SQL Server database. If you use an asterisk (*) in the Select command to select all columns, and if you use automatic code generation to perform update or delete operations, make sure that no columns have spaces in their names.

Because different database products use different varieties of SQL, the syntax of the SQL string depends on the current ADO.NET provider being used, which is identified by the P:System.Web.UI.WebControls.SqlDataSource.ProviderName property. If the SQL string is a parameterized query or command, the placeholder of the parameter also depends on the ADO.NET provider being used. For example, if the provider is the N:System.Data.SqlClient, which is the default provider for the T:System.Web.UI.WebControls.SqlDataSource class, the placeholder of the parameter is '@parameterName'. However, if the provider is set to the N:System.Data.Odbc or N:System.Data.OleDb, the placeholder of the parameter is '?'. For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

The P:System.Web.UI.WebControls.SqlDataSource.SelectCommand property can be an SQL string or the name of a stored procedure, if the data source supports stored procedures.

The P:System.Web.UI.WebControls.SqlDataSource.SelectCommand property delegates to the P:System.Web.UI.WebControls.SqlDataSourceView.SelectCommand property of the T:System.Web.UI.WebControls.SqlDataSourceView object that is associated with the T:System.Web.UI.WebControls.SqlDataSource control.

System_CAPS_security Segurança Observação

For security purposes, the P:System.Web.UI.WebControls.SqlDataSource.SelectCommand property is not stored is view state. Because it is possible to decode the contents of view state on the client, storing sensitive information about the database structure in view state could result in an information disclosure vulnerability.

System_CAPS_security Segurança Observação

Values are inserted into parameters without validation, which is a potential security threat. Use the E:System.Web.UI.WebControls.SqlDataSource.Filtering event to validate parameter values before executing the query. For more information, see Script Exploits Overview.

This section contains two code examples. The first code example demonstrates how to set the P:System.Web.UI.WebControls.SqlDataSource.SelectCommand text to a basic SQL query to retrieve data from an ODBC-compliant database and display it in a T:System.Web.UI.WebControls.GridView control. The second code example demonstrates how to set the P:System.Web.UI.WebControls.SqlDataSource.SelectCommand text to the name of a stored procedure and the P:System.Web.UI.WebControls.SqlDataSource.SelectCommandType property to the F:System.Web.UI.WebControls.SqlDataSourceCommandType.StoredProcedure value to retrieve data from a Microsoft SQL Server database and display it in a T:System.Web.UI.WebControls.DropDownList control.

In both examples, there is no need to explicitly call the M:System.Web.UI.WebControls.SqlDataSource.Select(System.Web.UI.DataSourceSelectArguments) method because the data-bound controls that are attached to the data source control through the P:System.Web.UI.WebControls.DataBoundControl.DataSourceID property will automatically call the M:System.Web.UI.WebControls.SqlDataSource.Select(System.Web.UI.DataSourceSelectArguments) method during the E:System.Web.UI.Control.PreRender phase.

The following code example demonstrates how to set the P:System.Web.UI.WebControls.SqlDataSource.SelectCommand text to a basic SQL query to retrieve data from an ODBC-compliant database and display it in a T:System.Web.UI.WebControls.GridView control.

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

The following code example demonstrates how to set the P:System.Web.UI.WebControls.SqlDataSource.SelectCommand text to the name of a stored procedure and the P:System.Web.UI.WebControls.SqlDataSource.SelectCommandType property to the F:System.Web.UI.WebControls.SqlDataSourceCommandType.StoredProcedure value to retrieve data from a SQL Server database and display it in a T:System.Web.UI.WebControls.DropDownList control. The P:System.Web.UI.WebControls.SqlDataSource.SelectCommand property can be an SQL query or the name of a stored procedure, if the data source supports stored procedures.

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

.NET Framework
Disponível desde 2.0
Retornar ao início
Mostrar: