C#中, 对数据库的操作,使用递归调用出错(出错信息为OleDbCommand是当前正忙的Open,Fectching。)下面的程序,NewQueryNode是一个递归调用,出错信息为OleDbCommand是当前正忙的Open,Fectching。
我猜想是由于在递归调用时,有些变量如reader没有执行reader.Close()靠造成的。
不过我仍然非常奇怪:按理,每次递归调用NewQueryNode,它使用新的内存空间,变量之间应互不干扰啊!!
public void NewFillTree(int userID,int parentID)  //临时放在此处用于测试目的

try
{
ConstClass.databaseConnect.Open(); //打开Access数据库连接
OleDbCommand myCommand=new OleDbCommand();
myCommand.Connection=ConstClass.databaseConnect; NewQueryNode(myCommand,userID,parentID); }
catch (Exception ex) 
{
MessageBox.Show(ex.Message);
}
finally 
{

ConstClass.databaseConnect.Close();
} } private void NewQueryNode(OleDbCommand myCommand,int userID,int rootID)  //临时放在此处用于测试目的
{
       
myCommand.CommandText="select * from categorytable where UserID="+userID
+" and ParentID="+rootID
+" and ElderBrotherID=0"; 
OleDbDataReader reader=myCommand.ExecuteReader();
while (reader.Read()) 
{
MessageBox.Show(reader["Name"].ToString());
rootID=int.Parse(reader["NodeID"].ToString()); 
         NewQueryNode(myCommand,userID,rootID); //递归调用
} reader.Close();            
}当我将程序作如下改动时,中间过程MessageBox.Show(reader["Name"].ToString());的执行正常,但在程序结束时显示错误“阅读器关闭时Read的尝试无效”
private void NewQueryNode(OleDbCommand myCommand,int userID,int rootID)  //临时放在此处用于测试目的
{
      
myCommand.CommandText="select * from categorytable where UserID="+userID
+" and ParentID="+rootID
+" and ElderBrotherID=0"; 
OleDbDataReader reader=myCommand.ExecuteReader();
while (reader.Read()) 
{
MessageBox.Show(reader["Name"].ToString());
rootID=int.Parse(reader["NodeID"].ToString());
                reader.Close();
     NewQueryNode(myCommand,userID,rootID);
}
            
}

解决方案 »

  1.   

    重新实例化一个
    OleDbCommand对象
      

  2.   

    上面说的对,把OleDbCommand 作为NewQueryNode()中的局部变量
      

  3.   

    oledbdatareader打开没有关上,如果关掉后,再重新使用oledbcommand就可以了!嗬嗬,就是如此!
      

  4.   

    这个事你写的代码:while (reader.Read()) 
    {
    MessageBox.Show(reader["Name"].ToString());
    rootID=int.Parse(reader["NodeID"].ToString());
    reader.Close();/////////////////////这里都关闭了,你接下来while怎么继续read???
    NewQueryNode(myCommand,userID,rootID);
    }
      

  5.   

    这个事你写的代码:while (reader.Read()) 
    {
    MessageBox.Show(reader["Name"].ToString());
    rootID=int.Parse(reader["NodeID"].ToString());
    reader.Close();/////////////////////这里都关闭了,你接下来while怎么继续read???
    NewQueryNode(myCommand,userID,rootID);////////这句执行完毕之后,你仍然要转到reader.read执行,你说会不会出错
    }关于这个,我继续说明一下:
    你可能想要迭代调用,可是你执行完 newquerynode之后,仍然要向下执行到reader.read
    此处关闭了,还怎么read
      

  6.   

    To zhzuo(秋枫) 
    为什么要重新实例化一个OleDbCommand对象?
      

  7.   


    To xxuu503(2005年 下面的写法也出错
      private void NewQueryNode(OleDbCommand myCommand,int userID,int rootID)  
       {
         myCommand.CommandText="select * from categorytable where UserID="+userID
           +" and ParentID="+rootID
           +" and ElderBrotherID=0"; 
         OleDbDataReader reader=myCommand.ExecuteReader();
         
         while (reader.Read()) 
         {
           MessageBox.Show(reader["Name"].ToString());
           rootID=int.Parse(reader["NodeID"].ToString()); 
                 NewQueryNode(myCommand,userID,rootID); //递归调用
         }     reader.Close();            
       }
      

  8.   

    本来就出错,我是copy你的代码说你的是错的,你有没有看我写的??
      

  9.   

    不好意思,我看错了OleDbDataReader reader=myCommand.ExecuteReader();这个说明了,一个reader对应一个command,当command改变了,reader指向的command.executereader改不改变?呵呵!private void NewQueryNode(int userID,int rootID)  
       {
    Oledbcommand myCommand=new Oledbcommand;//这样子作为局部变量     myCommand.CommandText="select * from categorytable where UserID="+userID
           +" and ParentID="+rootID
           +" and ElderBrotherID=0"; 
         OleDbDataReader reader=myCommand.ExecuteReader();
         
         while (reader.Read()) 
         {
           MessageBox.Show(reader["Name"].ToString());
           rootID=int.Parse(reader["NodeID"].ToString()); 
                 NewQueryNode(myCommand,userID,rootID); //递归调用
         }     reader.Close();            
       }
      

  10.   

    下面的递补归算法中, 在调用QueryNode(myCommand,userID,rootID);之前,必须加上reader.Close()语句,
    我非常奇怪,reader是一个局部变量,再次调用OleDbDataReader reader=myCommand.ExecuteReader()时,应该
    重新初始,不应该出现错误“ExecuteReader需要打开的并且可用的连接,该连接的当前状态是Open,Fetching”啊!!! private void QueryNode(OleDbCommand myCommand,int userID,int rootID)  //临时放在此处用于测试目的
    {
    myCommand.CommandText="select * from categorytable where UserID="+userID
                       +" and ParentID="+rootID
                       +" and ElderBrotherID=0"; 
    OleDbDataReader reader=myCommand.ExecuteReader();
               

    while (reader.Read()) 
    {
    MessageBox.Show(reader["Name"].ToString());
    rootID=int.Parse(reader["NodeID"].ToString());
    int youngerID=int.Parse(reader["YoungerBrotherID"].ToString());                        reader.Close();
    myCommand.CommandText="select * from categorytable where UserID="+userID
    +" and ParentID="+rootID
    +" and ElderBrotherID=0";
    reader=myCommand.ExecuteReader();
    if (reader.Read()) 
    {
    reader.Close(); //不加这一句,显示错误“ExecuteReader需要打开的并且可用的连接,该连接的当前状态是Open,Fetching”
    QueryNode(myCommand,userID,rootID);
    } reader.Close();
    myCommand.CommandText="select * from categorytable where UserID="+userID
    +" and NodeID="+youngerID; 
    reader=myCommand.ExecuteReader();
    }
    reader.Close();
                
    }
    public void FillTree(int userID,int parentID)  //临时放在此处用于测试目的

       try
    {
    ConstClass.databaseConnect.Open(); 
    OleDbCommand myCommand=new OleDbCommand();
    myCommand.Connection=ConstClass.databaseConnect;
    QueryNode(myCommand,userID,parentID);
    }
    catch (Exception ex) 
    {
    MessageBox.Show(ex.Message);
    }
    finally 
    {
    ConstClass.databaseConnect.Close();
    }
    }
    我将OleDbCommand myCommand变为局部变量,仍然出现同样的错误!!!!!!!!
    private void QueryNode(int userID,int rootID)  //临时放在此处用于测试目的
    {
            OleDbCommand myCommand=new OleDbCommand();
            myCommand.Connection=ConstClass.databaseConnect;    myCommand.CommandText="select * from categorytable where UserID="+userID
                       +" and ParentID="+rootID
                       +" and ElderBrotherID=0"; 
    OleDbDataReader reader=myCommand.ExecuteReader();

    while (reader.Read()) 
    {
    MessageBox.Show(reader["Name"].ToString());
    rootID=int.Parse(reader["NodeID"].ToString());
    int youngerID=int.Parse(reader["YoungerBrotherID"].ToString());               reader.Close();
    myCommand.CommandText="select * from categorytable where UserID="+userID
    +" and ParentID="+rootID
    +" and ElderBrotherID=0";
    reader=myCommand.ExecuteReader(); if (reader.Read()) 
    {
    reader.Close(); //不加这一句,显示错误“ExecuteReader需要打开的并且可用的连接,该连接的当前状态是Open,Fetching”
    QueryNode(userID,rootID);
    } reader.Close();
    myCommand.CommandText="select * from categorytable where UserID="+userID
    +" and NodeID="+youngerID; 
    reader=myCommand.ExecuteReader(); } reader.Close();
                
         }