SqlDataSource.DataSourceMode 屬性

定義

取得或設定 SqlDataSource 控制項用於擷取資料的資料擷取模式。Gets or sets the data retrieval mode that the SqlDataSource control uses to fetch data.

public:
 property System::Web::UI::WebControls::SqlDataSourceMode DataSourceMode { System::Web::UI::WebControls::SqlDataSourceMode get(); void set(System::Web::UI::WebControls::SqlDataSourceMode value); };
public System.Web.UI.WebControls.SqlDataSourceMode DataSourceMode { get; set; }
member this.DataSourceMode : System.Web.UI.WebControls.SqlDataSourceMode with get, set
Public Property DataSourceMode As SqlDataSourceMode

屬性值

SqlDataSourceMode

其中一個 SqlDataSourceMode 值。One of the SqlDataSourceMode values. 預設 DataSet 值為。The default is the DataSet value.

例外狀況

DataSourceMode 屬性不是在 SqlDataSourceMode 中定義的其中一個值。The DataSourceMode property is not one of the values defined in the SqlDataSourceMode.

範例

下列程式碼範例示範如何將控制項的 DataSourceMode 屬性設定 SqlDataSourceDataReader 具有 ListBox 不需要排序、分頁和篩選的控制項之案例的值。The following code example demonstrates how to set the DataSourceMode property of the SqlDataSource control to the DataReader value for a scenario with a ListBox control that requires no sorting, paging, nor filtering.

<%@ Page language="C#" %>

<!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:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </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>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>

備註

資料抓取模式會識別控制項如何 SqlDataSource 從基礎資料庫抓取資料。The data retrieval mode identifies how a SqlDataSource control retrieves data from the underlying database.

DataSourceMode 屬性設定為 DataSet 值時,資料會載入至物件, DataSet 並儲存在伺服器的記憶體中。When the DataSourceMode property is set to the DataSet value, data is loaded into a DataSet object and stored in memory on the server. 這會啟用使用者介面控制項(例如 GridView ,提供排序、篩選和分頁功能)的情況。This enables scenarios where user interface controls, such as GridView, offer sorting, filtering, and paging capabilities.

DataSourceMode 屬性設定為值時,就會 DataReaderIDataReader 物件(順向、唯讀的資料指標)抓取資料。When the DataSourceMode property is set to the DataReader value, data is retrieved by a IDataReader object, which is a forward-only, read-only cursor. 物件的特定類型 IDataReader 取決於所使用的 NET data provider SqlDataSource (由屬性識別) ProviderNameThe specific type of the IDataReader object depends on the NET data provider that the SqlDataSource uses, which is identified by the ProviderName property. 根據預設, SqlDataSource 控制項會使用提供者進行 Microsoft SQL Server、 System.Data.SqlClient 和資料讀取器是 SqlDataReader 物件。By default, the SqlDataSource control uses the provider for Microsoft SQL Server, the System.Data.SqlClient, and the data reader is a SqlDataReader object.

如果您變更 DataSourceMode 屬性,就 DataSourceChanged 會引發事件,而導致系結至的所有控制項重新系結 SqlDataSourceIf you change the DataSourceMode property, the DataSourceChanged event is raised, causing any controls that are bound to the SqlDataSource to rebind.

屬性的值 DataSourceMode 會儲存在 view 狀態中。The value of the DataSourceMode property is stored in view state.

適用於

另請參閱