SqlDataSource.Select(DataSourceSelectArguments) 方法

定義

利用 SelectCommand SQL 字串及集合中 SelectParameters 的任何參數,從底層資料庫擷取資料。

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 用於請求資料操作的物件,超出基本資料檢索範圍。

傳回

一份 IEnumerable 資料列清單。

例外狀況

物件 SqlDataSource 無法與底層資料來源建立連結。

範例

以下範例展示了如何程式化呼叫方法 Select ,並根據查詢結果設定值。 以下範例展示了網頁控制的宣告式程式碼。

<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" />

以下範例展示了如何程式化呼叫此 Select 方法。 控制回 SqlDataSource 傳整數。 整數的值用來設定控制項的 Label 文字,並決定是否顯示控制項 HyperLink

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

備註

Select 方法會在頁面生命週期的階段自動呼叫 PreRender 。 它是由透過屬性DataSourceID附加到SqlDataSource控制項的資料綁定控制項所呼叫的。

DataSourceMode該屬性設定為 值DataSet,方法會Select回傳物件DataView。 若DataSourceMode該屬性設定為 值DataReader,方法會Select回傳物件IDataReader。 讀取完資料後關閉物件 IDataReader

在操作執行前 Select ,會呼叫該 OnSelecting 方法來引發 Selecting 事件。 你可以處理此事件來檢查參數值,並在操作前 Select 執行任何處理。

操作完成後 Select ,會呼叫該 OnSelected 方法來啟動事件 Selected 。 你可以處理此事件來檢查任何回傳值和錯誤代碼,並執行後製處理。

DataSourceMode 屬性設定為 且 SqlDataSourceMode.DataSet 啟用快取, SqlDataSource 物件會在操作過程中 Select 從快取中擷取資料並儲存到快取。 快取的建立、丟棄或刷新,是根據與CacheExpirationPolicy屬性組合CacheDuration所指定的快取行為。

這很重要

當你在 Microsoft Windows 認證下使用用戶端模擬時,資料會在第一個使用者存取資料時被快取。 若有其他使用者請求相同資料,資料會從快取中取得。 資料不會透過再次呼叫資料庫來驗證使用者對資料的存取來取得。 如果你預期會有超過一個使用者存取資料,且你希望每次資料擷取都能由資料庫的安全設定驗證,請不要使用快取。

DataSourceMode 屬性設定為 且 SqlDataSourceMode.DataSet 已指定屬性 FilterExpression ,則會以所提供 FilterParameters 屬性評估篩選式,並在操作過程中 Select 將該篩選器套用到資料清單中。

Select方法將委派給SelectSqlDataSource控制項相關的物件的方法SqlDataSourceView。 執行資料檢索操作時,會SqlDataSourceView利用SelectCommandDbCommand文字及相關SelectParameters值建立物件,然後對底層資料庫執行。DbCommand

這很重要

參數中插入數值時未經驗證,這可能構成安全威脅。 在執行查詢前,請利用事件 Filtering 驗證參數值。 欲了解更多資訊,請參閱 腳本漏洞概述

適用於

另請參閱