• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

asp.net中datareader对象的简单使用

发布: 2007-6-30 23:38 | 作者: admin | 来源: | 查看: 11次 | 进入软件测试论坛讨论

领测软件测试网 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>



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网