Winform中查询中的连接打开和关闭执行需要什么? [英] what is need of connection open and close of execution in query in winform?

查看:77
本文介绍了Winform中查询中的连接打开和关闭执行需要什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

在我的表单中,有很多组合框.我想将不同的表数据加载到组合框中.我正在努力做到这一点.但是代码进行的时间很长.由于连接的打开和关闭编码.

当我在没有紧密连接的情况下运行两个命令并打开它时抛出

>

hi friends,

In my form, lot of combo box is there. I want to load different table datas to combo box. I am trying to do that. but code is going on very long. Because of the connection open and close codings.

when i run two command in without close connection and open it throw

>

There is already an open DataReader associated with this Connection which must be closed first.<br />


例外.

如何在我的程序中删除此打开和关闭


Exception.

how to remove this open and close in my program

string MyConString = ConfigurationManager.ConnectionStrings["College_Management_System.Properties.Settings.cmsConnectionString"].ConnectionString;
          MySqlConnection connection = new MySqlConnection(MyConString);
          MySqlCommand command = connection.CreateCommand();
          MySqlDataReader Reader;
          command.CommandText = "select name from course_master";
          connection.Open();
          Reader = command.ExecuteReader();
          while (Reader.Read())
          {
              cmbo_course.Items.Add(Reader[0].ToString());
          }

          command.CommandText = "select name from country_master";
          Reader = command.ExecuteReader();
          while (Reader.Read())
          {
              cmbo_perCountry.Items.Add(Reader[0].ToString());
              cmbo_country.Items.Add(Reader[0].ToString());
          }
          connection.Close();





thanks in advance..

推荐答案

在您的ConnectionString中添加另一个名为MultipleActiveResultSets = true;
的属性.
通过MARS,您可以实现异步通信.
添加此属性,则您将不会遇到问题.
In your ConnectionString add another attribute called as MultipleActiveResultSets=true;

through MARS you can implement asynchronous communication.
add this attribute then you will not face the problem.


好吧,这是因为DataReaders是连接模式,只能转发内容.只要阅读器处于打开状态,连接就处于活动状态并正在工作.因此,如果您尝试使用其他阅读器,则会出现错误.

看看这些:
建议采用变通方法 [类似的讨论 [
Well, it''s because DataReaders are connected-mode, forward only thing. As long as a reader is open, connection is active and working on it. Thus if you try to use another reader then, you get the error.

Have a look at these:
MSDN blog describing the same in much more detail[^]
Workaround suggested with implication[^]
Similar discussion[^]


首先要解决此问题,您必须关闭已由rdr.close()打开的阅读器.

之后
while(Reader.read())
{
}

Reader.close();

然后使用第二个阅读器

否则

遵循Mewara先生建议的步骤
to solve this first you have to close the reader that is already opened by rdr.close()

that is after

while(Reader.read())
{
}

Reader.close();

then use second reader

or else

follow the steps suggested by Mr.S Mewara


这篇关于Winform中查询中的连接打开和关闭执行需要什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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