DataReader对象作为一个asp.net中读取记录的一个比较好的控件,它的最大的优点就是速度快,使用频繁,而且在网站访问量很大的情况下,避免了因dataset对象过多的占用内存空间,造成服务器负担过重的情况.从而大大提高性能! 当然,它也有不好的地方,那就是datareader对象只能是根据read()方法判断,是一个只读的,仅向前的数据流.此外可以通过command对象的executereader来创建该对象.
下面是关于该控件的基本使用代码,希望对大家有用 <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %> <%@ import namespace="system.data"%> <%@ import namespace="system.data.sqlclient"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script language="vb" runat="server"> sub page_load(sender as object,e as eventargs) dim cnn as sqlconnection dim cmd as sqlcommand dim html as string dim datar as sqldatareader '建立连接对象 cnn=new sqlconnection("server=LYWS;uid=sa;pwd=sa;database=book") cmd=new sqlcommand("select * from bookinfo",cnn) cmd.connection.open() '将结果集赋给datareader对象 datar=cmd.executereader() response.Write("<center><h2>图书信息表<h2><center>") response.Write("<center><table border=1 style='width:18cm'>") response.Write("<tr>") response.Write("<th>图书代码</th>") response.Write("<th>图书名称</th>") response.Write("<th>出版社</th>") response.Write("<th>定价</th>") response.Write("<th>版别</th>") response.Write("<th>作者</th>") response.Write("</tr>")
try '调用datareader对象的reader方法,通过while循环,遍利结果集 while (datar.read()) response.Write("<tr>") response.Write("<td align='center'>" + datar("图书代码").tostring() + "</td>") response.Write("<td>" + datar("图书名称").tostring() + "</td>") response.Write("<td>" + datar("出版社").tostring() + "</td>") response.Write("<td align='right'>" + datar("定价").tostring() + "</td>") response.Write("<td>" + datar("版别").tostring() + "</td>") response.Write("<td>" + datar("作者").tostring() + "</td>") response.Write("</tr>") end while response.Write("</table></center>") finally datar.close() cnn.close() end try end sub </script> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>datareader对象测试!</title> </head> <body bgcolor="#9999ff"> <br> <br> <hr width="70%" align="center"> WEB+数据库设计系列 ->落叶无声 67060096
</body> </html>

|