DataReader 处理多个结果集--NextResult的用法

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd>

仔细查看您的数据库代码,看是否存在多次进入数据库的请求路径。每个这样的往返都会降低应用程序可以提供的每秒请求数量。通过在一个数据库请求中返回多个结果集,可以节省与数据库进行通信所需的总时间长度。同时因为减少了数据库服务器管理请求的工作,还会使得系统伸缩性更强。
简单示例如下:

一、返回多个数据集的存储过程
CREATE PROC Proc ---Multiple Resultsets
AS
SELECT * FROM Users
SELECT * FROM Users WHERE State = 'CA'
GO

二、取多个数据集的代码
   String ConnString = "User ID=sa;password=sa;Initial Catalog=pubs;Data Source=myServer";
   SqlConnection Connection = new SqlConnection(myConnString);
   SqlCommand Command = new SqlCommand();
   SqlDataReader reader ;

   Command.CommandType = CommandType.StoredProcedure;
   Command.Connection = Connection;
   Command.CommandText = "Proc";
   int RecordCount=0;
   try
   {
    Connection.Open();
    reader = command.ExecuteReader();
    int RecordCount=0;

    // read the data from that resultset
    while (reader.Read())
    {
     RecordCount = RecordCount + 1;
    }
    Response.Write("Total number of Users:" + RecordCount.ToString());

    // read the next resultset
    reader.NextResult();
    RecordCount = 0;

    // read the data from that second resultset
    while (reader.Read())
    {
     RecordCount = RecordCount + 1;
    }
    Response.Write("Total number of Users from California:" + RecordCount.ToString());
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.ToString());
   }
   finally
   {
    Connection.Close();
   }

本文转自 netcorner 博客园博客,原文链接: http://www.cnblogs.com/netcorner/archive/2008/05/06/2912156.html ,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值