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 並根據查詢的結果設定值。 下列範例顯示 Web 控制項的宣告式程式碼。

<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 控制項的資料繫結控制項所呼叫。

如果 屬性設定為 DataSet 值, DataSourceMode 此方法 Select 會傳回 DataView 物件。 如果 屬性設定為 DataReader 值, DataSourceMode 此方法 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 使用 SelectCommand 文字和任何相關聯的 SelectParameters 值來建 DbCommand 置 物件,然後針對基礎資料庫執行 DbCommand

重要

值會插入參數中,而不需驗證,這是潛在的安全性威脅。 在執行查詢之前, Filtering 請使用 事件來驗證參數值。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

適用於

另請參閱