构造函数开了一个线程,能够正常获取到数据。
但是构造函数里面接下来的几行代码访问数据时就报错了。
System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。//开线程加载存货档案表
            Thread mythread = new Thread(new ThreadStart(myFunction));
            mythread.IsBackground = true;
            mythread.Start();
            //this.inventoryc = myoper.GetInventory();
            //this.comboBox6.DataSource = this.inventoryc;            this.comboBox7.DisplayMember = "judge";
            this.comboBox7.ValueMember = "id";
            this.comboBox7.DataSource = myoper.GetJudge();//这个方法也是去数据库读取数据装入DataTable作为comboBox7的数据源。这个方法会报错:System.InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。private void myFunction()
        {
            MFinalCheckOper myoper = new MFinalCheckOper(MySystemInfo);
            this.inventoryc =myoper.GetInventory();
          
            this.BeginInvoke(new MethodInvoker(delegate()
            {
                this.comboBox6.DataSource = this.inventoryc;
            }));
        }

解决方案 »

  1.   

    这个错误是因为你打开DataReader没有关闭的原因吧应该
    每次读完记得close一下 自己仔细检查下应该就ok了
      

  2.   

    SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
                sb.DataSource = ".";
                sb.InitialCatalog = "DATABASE";
                sb.UserID = "sa";
                sb.Password = "123456";
                sb.MultipleActiveResultSets = true;      //通过这个属性设置一个连接可以打开多个DataReader
                string _conStr = sb.ConnectionString;
    这样就可以了,谢谢你