已有打开的与此命令相关联的 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();
    } 

你可能感兴趣的:(ASP.NET技术)