Condividi tramite


SqlDataSource.SelectCommand Proprietà

Definizione

Ottiene o imposta la stringa SQL utilizzata dal SqlDataSource controllo per recuperare i dati dal database sottostante.

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

Valore della proprietà

Stringa SQL o nome di una stored procedure utilizzata SqlDataSource da per recuperare i dati.

Esempio

Questa sezione contiene due esempi di codice. Il primo esempio di codice illustra come impostare il SelectCommand testo su una query SQL di base per recuperare i dati da un database conforme a ODBC e visualizzarli in un GridView controllo . Il secondo esempio di codice illustra come impostare il SelectCommand testo sul nome di una stored procedure e la SelectCommandType proprietà sul StoredProcedure valore per recuperare i dati da un database di Microsoft SQL Server e visualizzarli in un DropDownList controllo .

In entrambi gli esempi non è necessario chiamare in modo esplicito il Select metodo perché i controlli associati a dati collegati al controllo origine dati tramite la DataSourceID proprietà chiameranno automaticamente il Select metodo durante la PreRender fase.

Nell'esempio di codice seguente viene illustrato come impostare il SelectCommand testo su una query SQL di base per recuperare i dati da un database conforme a ODBC e visualizzarli in un GridView controllo .

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

Nell'esempio di codice seguente viene illustrato come impostare il SelectCommand testo sul nome di una stored procedure e la SelectCommandType proprietà sul StoredProcedure valore per recuperare dati da un database di SQL Server e visualizzarli in un DropDownList controllo . La SelectCommand proprietà può essere una query SQL o il nome di una stored procedure, se l'origine dati supporta stored procedure.

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

Commenti

La SelectCommand proprietà rappresenta una query SQL o il nome di una stored procedure e viene utilizzata dal Select metodo per recuperare i dati da un database di SQL Server. Se si usa un asterisco (*) nel comando Seleziona per selezionare tutte le colonne e se si usa la generazione automatica del codice per eseguire operazioni di aggiornamento o eliminazione, assicurarsi che non siano presenti spazi nei nomi delle colonne.

Poiché i diversi prodotti di database usano diverse varietà di SQL, la sintassi della stringa SQL dipende dal provider di ADO.NET corrente usato, identificato dalla ProviderName proprietà . Se la stringa SQL è una query o un comando con parametri, il segnaposto del parametro dipende anche dal provider ADO.NET utilizzato. Ad esempio, se il provider è , System.Data.SqlClientche è il provider predefinito per la SqlDataSource classe , il segnaposto del parametro è '@parameterName'. Tuttavia, se il provider è impostato su System.Data.Odbc o System.Data.OleDb, il segnaposto del parametro è '?'. Per altre informazioni sulle query e i comandi SQL con parametri, vedere Uso di parametri con il controllo SqlDataSource.

La SelectCommand proprietà può essere una stringa SQL o il nome di una stored procedure, se l'origine dati supporta stored procedure.

La SelectCommand proprietà delega alla SelectCommand proprietà dell'oggetto SqlDataSourceView associato al SqlDataSource controllo .

Importante

Ai fini della sicurezza, la SelectCommand proprietà non viene archiviata è lo stato di visualizzazione. Poiché è possibile decodificare il contenuto dello stato di visualizzazione nel client, l'archiviazione di informazioni riservate sulla struttura del database nello stato di visualizzazione potrebbe causare una vulnerabilità di divulgazione di informazioni.

Importante

I valori vengono inseriti nei parametri senza convalida, ovvero una potenziale minaccia per la sicurezza. Usare l'evento per convalidare i Filtering valori dei parametri prima di eseguire la query. Per altre informazioni, vedere Cenni preliminari sugli exploit di script.

Si applica a

Vedi anche