已有打开的与此命令相关联的 DataReader,必须首先将它关闭

原因:同时打开建立两个DataReader,而采用同一个SqlConnection(或OleDbConnection );Command(或OleDbCommand )会造成这种结果。

解决方法: 两个DataReader用不同的SqlConnection,Command;

注意: DataReader依托于SqlConnection,如果SqlConnection关闭了,DataReader也就自动消亡了~


例:

public void swap()
    {
        OleDbConnection conn = new OleDbConnection(GD.MyConnStr());
        conn.Open();
        OleDbCommand comm = new OleDbCommand("select rankId from rankRole where adminId ="

+ ViewState["id"], conn);
        OleDbDataReader reader = comm.ExecuteReader();
        while (reader.Read())
        {
            OleDbConnection conn1 = new OleDbConnection(GD.MyConnStr());
            conn1.Open();
            OleDbCommand comm1 = new OleDbCommand("select id,rankName from rank where id =

" + reader.GetValue(0).ToString(), conn);
            OleDbDataReader reader1 = comm1.ExecuteReader();
            while (reader1.Read())
            {               
                ListItem item1 = new ListItem(reader1.GetValue(1).ToString(),

reader1.GetValue(0).ToString());
                ListBox2.Items.Add(item1);
                ListBox1.Items.Remove(item1);
            }
            reader1.Close();
            conn1.Close();
        }
        reader.Close();
        conn.Close();
    } 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值