C# 在一个SqlConnection中打开两个SqlDataReader

本文介绍了如何在C#的ADO.Net中通过设置连接串的MultipleActiveResultSets属性为True,实现一个SqlConnection对象同时打开并操作两个不同的SqlDataReader实例,演示了SQL查询和数据读取的高效流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在C#的ADO.Net中可以通过设置连接串的MultipleActiveResultSets属性为True

来允许一个SqlConnection中打开两个SqlDataReader

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace 一个连接可以打开多个SqlDataReader
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
            sb.DataSource = "192.168.8.249";
            sb.InitialCatalog = "PA";
            sb.UserID = "sa";
            sb.Password = "kicpassword";
            sb.MultipleActiveResultSets = true;      //通过这个属性设置一个连接可以打开多个DataReader

            string _conStr = sb.ConnectionString;
            string _sql = "SELECT TOP 10 BatchID,BatchNo,BoxNo,importTime FROM dbo.M_Batch01 where isreturn=0 order by BatchID";

            using (SqlConnection _conn = new SqlConnection(_conStr))
            {
                using (SqlCommand _cmd = new SqlCommand(_sql, _conn))
                {
                    _conn.Open();
                    using (SqlDataReader reader1 = _cmd.ExecuteReader())
                    {
                        if (reader1.HasRows)
                        {
                            while (reader1.Read())
                            {
                                for (int i = 0; i < reader1.FieldCount; i++)
                                {
                                    Console.Write(reader1[i].ToString()+"\t");
                                }
                                Console.WriteLine();


                                //打开第二个Reader,必须要重新New一个SqlCommand
                                _sql = "SELECT TOP 10 * FROM dbo.D_ListBoxValues";
                                using (SqlCommand _cmd1 = new SqlCommand(_sql, _conn))
                                {
                                    using (SqlDataReader reader2 = _cmd1.ExecuteReader())
                                    {
                                        if (reader1.HasRows)
                                        {
                                            while (reader2.Read())
                                            {
                                                for (int j = 0; j < reader2.FieldCount; j++)
                                                {
                                                    Console.Write(reader2[j].ToString() + "\t");
                                                }
                                                Console.WriteLine();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Console.ReadLine();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值