Uredi

Deli z drugimi prek


SqlDataSource.Select(DataSourceSelectArguments) Method

Definition

Retrieves data from the underlying database by using the SelectCommand SQL string and any parameters that are in the SelectParameters collection.

public:
 System::Collections::IEnumerable ^ Select(System::Web::UI::DataSourceSelectArguments ^ arguments);
public System.Collections.IEnumerable Select (System.Web.UI.DataSourceSelectArguments arguments);
member this.Select : System.Web.UI.DataSourceSelectArguments -> System.Collections.IEnumerable
Public Function Select (arguments As DataSourceSelectArguments) As IEnumerable

Parameters

arguments
DataSourceSelectArguments

A DataSourceSelectArguments object that is used to request operations on the data beyond basic data retrieval.

Returns

An IEnumerable list of data rows.

Exceptions

The SqlDataSource object cannot establish a connection with the underlying data source.

Examples

The following examples show how to programmatically call the Select method and set values based on the result of the query. The following example shows the declarative code for the Web controls.

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT Count(*) FROM [Products] WHERE ([ReorderLevel] > 0)">
</asp:SqlDataSource>
<asp:Label 
    ID="Label1" 
    runat="server" 
    Text="">
</asp:Label>
<br />
<asp:Button 
    ID="Button1" 
    Text="Check Reorder Status" 
    runat="server" 
    onclick="Button1_Click" />
<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT Count(*) FROM [Products] WHERE ([ReorderLevel] > 0)">
</asp:SqlDataSource>
<asp:Label 
    ID="Label1" 
    runat="server" 
    Text="">
</asp:Label>
<br />
<asp:Button 
   ID="Button1" 
   Text="Check Reorder Status" 
   runat="server" 
   onclick="Button1_Click" />

The following example shows how to programmatically call the Select method. The SqlDataSource control returns an integer. The value of the integer is used to set the text of a Label control and to determine whether to display a HyperLink control.

protected void CheckReorderStatus()
{
    DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    int reorderedProducts = (int)dv.Table.Rows[0][0];
    if (reorderedProducts > 0)
    {
        Label1.Text = "Number of products on reorder: " + reorderedProducts;
    }
    else
    {
        Label1.Text = "No products on reorder.";
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    CheckReorderStatus();
}
Protected Sub CheckReorderStatus()
    Dim dv As DataView
    Dim reorderedProducts As Integer

    dv = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
    reorderedProducts = CType(dv.Table.Rows(0)(0), Integer)
    If (reorderedProducts > 0) Then
        Label1.Text = "Number of products on reorder: " & reorderedProducts
    Else
        Label1.Text = "No products on reorder."
    End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    CheckReorderStatus()
End Sub

Remarks

The Select method is automatically called during the PreRender phase of the page life cycle. It is called by data-bound controls that have been attached to a SqlDataSource control through their DataSourceID property.

The Select method returns a DataView object if the DataSourceMode property is set to the DataSet value. The Select method returns a IDataReader object if the DataSourceMode property is set to the DataReader value. Close the IDataReader object when you have finished reading the data.

Before the Select operation is performed, the OnSelecting method is called to raise the Selecting event. You can handle this event to examine the values of the parameters and to perform any processing before the Select operation.

After the Select operation completes, the OnSelected method is called to raise the Selected event. You can handle this event to examine any return values and error codes and to perform any post-processing.

If the DataSourceMode property is set to SqlDataSourceMode.DataSet and caching is enabled, the SqlDataSource object retrieves data from and saves data to the cache during the Select operation. The cache is created, discarded, or refreshed based on the caching behavior that is specified by the combination of the CacheDuration and CacheExpirationPolicy properties.

Important

When you are using client impersonation under Microsoft Windows authentication, the data is cached when the first user accesses the data. If another user requests the same data, the data is retrieved from the cache. The data is not retrieved by making another call to the database to verify the user's access to the data. If you expect more than one user to access the data, and you want each retrieval of data to be verified by the security configurations for the database, do not use caching.

If the DataSourceMode property is set to SqlDataSourceMode.DataSet and a FilterExpression property has been specified, the filter expression is evaluated with any supplied FilterParameters properties and the resulting filter is applied to the list of data during the Select operation.

The Select method delegates to the Select method of the SqlDataSourceView object that is associated with the SqlDataSource control. To perform a data retrieval operation, the SqlDataSourceView builds a DbCommand object by using the SelectCommand text and any associated SelectParameters values, and then executes the DbCommand against the underlying database.

Important

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

Applies to

See also