数据库连接的类的方法是这样写的
public OleDbDataReader GetDataReader(string strSQL)
{
Connection.Open();
OleDbCommand command = new OleDbCommand(strSQL, Connection);
OleDbDataReader dataReader = command.ExecuteReader();
//Connection.Close(); //ABC注释
return dataReader;
}
窗体Load调用如下:
OleDbDataReader myRead = ut.GetDataReader("Select TypeID,TypeName from WordType");
while (myRead.Read())
{
tvType.Nodes.Add(myRead["TypeName"].ToString());
}
myRead.Close();
我在另一个按钮执行其它的数据操作的时候,总是提示Connection已打开。myRead.Close()执行的时候,不是Connection已关闭了吗?如果我把ABC注释那里取消注释,while (myRead.Read())循环的时候又会提示未连接。请问大家是怎么写的。谢谢。
public OleDbDataReader GetDataReader(string strSQL)
{
Connection.Open();
OleDbCommand command = new OleDbCommand(strSQL, Connection);
OleDbDataReader dataReader = command.ExecuteReader();
//Connection.Close(); //ABC注释
return dataReader;
}
窗体Load调用如下:
OleDbDataReader myRead = ut.GetDataReader("Select TypeID,TypeName from WordType");
while (myRead.Read())
{
tvType.Nodes.Add(myRead["TypeName"].ToString());
}
myRead.Close();
我在另一个按钮执行其它的数据操作的时候,总是提示Connection已打开。myRead.Close()执行的时候,不是Connection已关闭了吗?如果我把ABC注释那里取消注释,while (myRead.Read())循环的时候又会提示未连接。请问大家是怎么写的。谢谢。
OleDbDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);关闭阅读器时会自动关闭数据连接。
并行任务共享 Connection 变量。不是你写了myRead.Close()就关闭了,你要搞懂什么才叫做“关闭”,(相对于这个bug而言)你的关闭代码的位置决定了——根本就没有针对这个问题的发生机制而设计。
using(OleDbDataReader myRead = ut.GetDataReader(.....))
{
....
}
的问题!你的Connection根本就会被共享,争用,自然就会出现这个bug。