DataSourceMode 属性的用途

来源:互联网 发布:js tostring 参数 编辑:程序博客网 时间:2024/04/28 18:02

DataSourceMode是数据检索模式标识 SqlDataSource 控件从基础数据库中检索数据的方式。(MSDN)

DataSourceMode属性中有2个标识:DataSet和DataReader。默认是DataSet。DataSet我们都一般很熟悉,能够使用户界面控件(例如 GridView)可以提供排序、筛选和分页,还有Cache等的功能。当 DataSourceMode 属性设置为 DataReader 值时,数据由 IDataReader 对象来检索,该对象为只进且只读的游标。(MSDN)

DataReader比起DataSet的速度要快的多。经常用于含有不需要排序、分页和筛选功能的LIstBox,RadioButtonList,BulletedList,Repeater等控件。这样可以大大减少数据库的负荷量。

 以下是MSDN上的一个例子,参考:

<%@ Page language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html  >  <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>
原创粉丝点击