日期:2014-05-17  浏览次数:20697 次

winform开线程加载数据时报错
构造函数开了一个线程,能够正常获取到数据。
但是构造函数里面接下来的几行代码访问数据时就报错了。
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;
            }));
        }

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