日期:2014-05-18  浏览次数:20949 次

SQL事件处理 指示“已有打开的与此命令相关联的 DataReader,必须首先将它关闭”。
下面是源代码,各位给看看要怎么改 我如果直接在中间加个加个conn.Close(); 也会提示出错。整个事务的功能是先将数据读出,修改后写回数据库。谢谢

C# code
conn.Open();//采用事件处理时行数据提交
                    SqlCommand cmd = new SqlCommand();
                    SqlTransaction sqltran = conn.BeginTransaction();
                    cmd.Connection = conn;
                    cmd.Transaction = sqltran;

                          cmd.CommandText = "SELECT [" + outdept + "],[" + outdept1 + "],[" + indept + "] FROM [Order] where [ClientOrder]='" +lvw_list.Items[1].SubItems[2].Text + "'";
                        SqlDataReader getnumber = cmd.ExecuteReader();
                        getnumber.Read();
                        outi = Convert.ToInt32(getnumber[0].ToString()) -Convert.ToInt32( lvw_list.Items[0].SubItems[4].Text);
                        outo = Convert.ToInt32(getnumber[1].ToString()) +Convert.ToInt32( lvw_list.Items[0].SubItems[4].Text);
                        ini = Convert.ToInt32(getnumber[2].ToString()) + Convert.ToInt32(lvw_list.Items[0].SubItems[4].Text);
                         cmd.CommandText = "UPDATE [Order] SET [" + outdept + "]='" + outi + "',[" + outdept1 + "]='" + outo + "',[" + indept + "]='" + ini + "' WHERE  [ClientOrder]='" + lvw_list.Items[1].SubItems[2].Text + "' ";
                        cmd.ExecuteNonQuery();
                        sqltran.Commit();
                        lvw_list.Items.RemoveAt(1);
                        conn.Close();


------解决方案--------------------
getnumber.Close();
cmd.CommandText = "UPDATE [Order] SET