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);
}
}
我猜想是由于在递归调用时,有些变量如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);
}
}
解决方案 »
- 关于交叉表
- 关于C#读excel的例子,看不懂,请教高手
- 改变按钮颜色不成功
- 纠结的cookies读取问题。帮忙看看!
- 在DataGridView中用DataAdapter和CommmandBuilder怎么实现不了对数据库的修改
- 发送邮件问题
- 简单问题,在线等,绘制的图形显示在什么控件上?
- 在C#中怎样使用HOOK函数
- 如何将"可以识别的汉字"转换为"不可识别的汉字",并可以再转换回"可以识别的汉字"? 用C#
- 关于WORD彩蛋的问题: 在 1DF94 偏移处,你将会找到 4D 54 68 64
- 小弟想找一下c#里面怎么操作俄excel表格的资料
- 关于componentone studio.net的使用
OleDbCommand对象
{
MessageBox.Show(reader["Name"].ToString());
rootID=int.Parse(reader["NodeID"].ToString());
reader.Close();/////////////////////这里都关闭了,你接下来while怎么继续read???
NewQueryNode(myCommand,userID,rootID);
}
{
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
为什么要重新实例化一个OleDbCommand对象?
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();
}
{
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();
}
我非常奇怪,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();
}