SqlDataSource.Select(DataSourceSelectArguments) SqlDataSource.Select(DataSourceSelectArguments) SqlDataSource.Select(DataSourceSelectArguments) SqlDataSource.Select(DataSourceSelectArguments) Method

定义

通过使用 SelectCommand SQL 字符串以及 SelectParameters 集合中的任何参数,从基础数据库中检索数据。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

参数

arguments
DataSourceSelectArguments DataSourceSelectArguments DataSourceSelectArguments DataSourceSelectArguments

用于请求对数据执行基本数据检索操作以外操作的 DataSourceSelectArguments 对象。A DataSourceSelectArguments object that is used to request operations on the data beyond basic data retrieval.

返回

一个数据行的 IEnumerable 列表。An IEnumerable list of data rows.

异常

SqlDataSource 对象无法与基础数据源建立连接。The SqlDataSource object cannot establish a connection with the underlying data source.

示例

下面的示例演示如何以编程方式调用Select方法, 并根据查询结果设置值。The following examples show how to programmatically call the Select method and set values based on the result of the query. 下面的示例演示 Web 控件的声明性代码。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] &gt; 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] &gt; 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" />

下面的示例演示如何以编程方式调用Select方法。The following example shows how to programmatically call the Select method. SqlDataSource控件返回整数。The SqlDataSource control returns an integer. 整数的值用于设置Label控件的文本, 并用于确定是否HyperLink显示控件。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

注解

在页生命周期的PreRender阶段, 会自动调用方法。SelectThe Select method is automatically called during the PreRender phase of the page life cycle. 它由已通过其SqlDataSource DataSourceID属性附加到控件的数据绑定控件调用。It is called by data-bound controls that have been attached to a SqlDataSource control through their DataSourceID property.

如果Select DataView 将属性设置DataSet为值, 则方法返回对象。 DataSourceModeThe Select method returns a DataView object if the DataSourceMode property is set to the DataSet value. 如果Select IDataReader 将属性设置DataReader为值, 则方法返回对象。 DataSourceModeThe Select method returns a IDataReader object if the DataSourceMode property is set to the DataReader value. 读取完数据后, 关闭对象。IDataReaderClose the IDataReader object when you have finished reading the data.

在执行OnSelecting操作之前, 将调用方法来引发Selecting事件。 SelectBefore the Select operation is performed, the OnSelecting method is called to raise the Selecting event. 可以处理此事件以检查参数的值并在Select操作之前执行任何处理。You can handle this event to examine the values of the parameters and to perform any processing before the Select operation.

操作完成后OnSelected , 调用方法来引发Selected事件。 SelectAfter 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.

如果将SqlDataSourceMode.DataSet SqlDataSource属性设置为并且启用了缓存, 则对象将在Select操作期间从缓存中检索数据并将数据保存到缓存中。 DataSourceModeIf 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. 根据CacheDurationCacheExpirationPolicy属性的组合指定的缓存行为, 创建、放弃或刷新缓存。The cache is created, discarded, or refreshed based on the caching behavior that is specified by the combination of the CacheDuration and CacheExpirationPolicy properties.

重要

使用 Microsoft Windows 身份验证下的客户端模拟时, 将在第一个用户访问数据时缓存数据。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.

如果将SqlDataSourceMode.DataSet FilterExpression属性设置为并指定了属性, 则使用任何提供FilterParameters的属性来计算筛选器表达式, 并将生成的筛选器应用到DataSourceMode Select操作。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.

方法委托SqlDataSourceView Select给与SqlDataSource控件相关联的对象的方法。 SelectThe Select method delegates to the Select method of the SqlDataSourceView object that is associated with the SqlDataSource control. 若要执行数据检索操作, 请SqlDataSourceView使用SelectCommand文本DbCommandDbCommand任何关联SelectParameters的值生成一个对象, 然后对基础数据库执行。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.

重要

无需验证即可将值插入到参数中, 这是一个潜在的安全威胁。Values are inserted into parameters without validation, which is a potential security threat. Filtering使用事件在执行查询之前验证参数值。Use the Filtering event to validate parameter values before executing the query. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

适用于

另请参阅