SqlDataSource.DataSourceMode SqlDataSource.DataSourceMode SqlDataSource.DataSourceMode SqlDataSource.DataSourceMode Property

定义

获取或设置 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 值之一。One of the SqlDataSourceMode values. 默认DataSet值为。The default is the DataSet value.

异常

示例

下面的代码DataSourceMode示例演示如何将SqlDataSource控件的属性设置为DataReader 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.

如果将DataSet属性设置为值, 则会将DataSet数据加载到对象中, 并将其存储在服务器上的内存中。 DataSourceModeWhen 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.

如果将DataReader属性设置为值, 则将通过IDataReader对象 (只进只读游标) 来检索数据。 DataSourceModeWhen 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对象的特定类型取决于SqlDataSource使用的 NET 数据提供程序, 该提供程序由ProviderName属性标识。The 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 , 则会引发事件, 从而导致绑定到的SqlDataSource任何控件重新绑定。If you change the DataSourceMode property, the DataSourceChanged event is raised, causing any controls that are bound to the SqlDataSource to rebind.

DataSourceMode属性的值存储在视图状态中。The value of the DataSourceMode property is stored in view state.

适用于

另请参阅