仰天一笑(Ansonxuyu),专业从事软件定制开发、Web软件开发,网站建设,网络推广,APP开发,微博应用开发,微信应用开发,电子商务开发,物联网开发等技术。
互联网8年风雨,愿在此交朋识友,交流心得,分享技术知识(策划/研发/运营/推广/合作)!QQ:943530498


仰天一笑

昨日不悔,今日勿失,明日莫忧! —徐羽

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

一、返回多个数据集的存储过程
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();
   }

 

posted on 2006-07-07 10:07  仰天一笑  阅读(2367)  评论(0编辑  收藏  举报