• 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();
       }

  • 相关阅读:
    GitHub 教程【转】
    Github 教程
    Excel 多个单元格输入同样内容
    寻找问题远比解决问题重要!
    蒲公英: 一个提供App 存储、分发、Bug管理的网站
    Dacapao 实验集(9.12 版本) 能不能给个网址?【内存分析实验】
    软件测试思维导图[ZZ]
    程序员、技术领导、管理者各有烦恼,你占了几条?ZZ
    r test
    mooctest项目总结 【转载】
  • 原文地址:https://www.cnblogs.com/netcorner/p/2912156.html
Copyright © 2020-2023  润新知