SqlDataReader问题[连接打开和关闭ISSUE] [英] Problem with SqlDataReader [Connection Open and Close ISSUE]

查看:83
本文介绍了SqlDataReader问题[连接打开和关闭ISSUE]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用SqlDataReader来读取数据库值。我一直这样做如下

        I am using a SqlDataReader to read Database Values. I have been doing this as follows

SqlConnection cn = new SqlConnection(" connectionString")

SqlConnection cn=new SqlConnection("connectionString") 

for(int i = 0; i< myList .Count; i ++)// myList是一个整数列表

for(int i=0; i<myList.Count;i++)       //myList is a list of integers  

{
foreach(ID中的ID ID)// ID是ID的列表

{
     foreach(int ID in IDs)        //IDs is a list of Ids

{

cn.Open();

     cn.Open();

//这里我使用的是SqlDataReader ro读取数据库值,我将这些值附加到这些值为字符串变量。

//Here i am using a SqlDataReader ro read database values  and i am appending these values to a string variable.

cn.Close()

     cn.Close()

myString = myString +""READER VALUE";

myString=myString +""READER VALUE";

}

}

以上代码工作正常没有错误或任何显示问题,但如果"myList"中的元素数量,则处理变得非常慢。和"ID"增加。

The above code work fine has no errors or any display problems but the processing is getting really slow if the number of elements in "myList" and "IDs" increase.

例如:假设myList中有100个整数

For example : say there are 100 integers in myList

并且"IDS"中有10个ID。 list

                  And 10 IDs in "IDS" list

然后我打开和关闭SqlConnection 100x10次,这使得这个过程非常慢。

then i am opening and closing the SqlConnection 100x10 times which is making this process really slow.

任何人都可以帮助我解决这个问题。

CAN ANY ONE HELP ME OUT IN THIS ASPECT.

推荐答案

嗨亲爱的阿里。
通常你必须打开你的连接一次。更改您的代码如下:


SqlConnection cn = new SqlConnection(" connectionString")

Hi Dear Ali.
Generally you must open your connection once. Change your code as following:


SqlConnection cn=new SqlConnection("connectionString") 

cn.Open();

cn.Open();


for(int i = 0; i< myList.Count; i ++)// myList是一个整数列表

for(int i=0; i<myList.Count;i++)       //myList is a list of integers  

{
foreach(ID中的ID ID)// ID是ID列表

{
     foreach(int ID in IDs)        //IDs is a list of Ids

{


//这里我使用的是SqlDataReader ro读取数据库值,我将这些值附加到字符串变量。

//Here i am using a SqlDataReader ro read database values  and i am appending these values to a string variable.

}

}


这篇关于SqlDataReader问题[连接打开和关闭ISSUE]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆