zoukankan      html  css  js  c++  java
  • [原]DataReader 处理多个结果集—NextResult的用法

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

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

  • 相关阅读:
    吉特仓储管系统(开源WMS)--分享两月如何做到10W+的项目
    吉特仓库管理系统(开源)-如何在网页端启动WinForm 程序
    RequireJS中的require如何返回模块
    RequireJS shim 用法说明
    从ISTIO熔断说起-轻舟网关熔断
    数据库与数据仓库的区别是什么
    API是什么
    要想业务中台建得快,最好用Service Mesh来带
    中台建设之路-中台建设怎么做?建设中台需要具备什么?
    为什么要建设中台
  • 原文地址:https://www.cnblogs.com/skylaugh/p/444918.html
Copyright © 2011-2022 走看看