c#,当只有 1 reader 时,我一直在捕获多个 MySQL reader 异常
c#, I keep catching a multiple MySQL reader exception when there is only 1 reader
我在 C# 中有一个 Windows 表单应用程序,它有一个按钮 运行 代码如下:
private void aCertainButton_Click(object sender, EventArgs e)
{
if (folderFinder.ShowDialog() == DialogResult.OK)
{
if (openConnection())
{
foreach (Thingy stuff in aCertainCollection)
{
string sqlCommandString = "SELECT COUNT(*) FROM thatTable WHERE someField = " + stuff.property + ";";
try
{
MySqlCommand count = new MySqlCommand(sqlCommandString, connection);
int rowCount = 0;
object o = count.ExecuteScalar();
if (o != null)
{
rowCount = int.Parse(o.ToString());
}
if (rowCount == 1)
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM thatTable WHERE someField = " + stuff.property + ";", connection);
MySqlDataReader reader = cmd.ExecuteReader();
//Processing stuff from the reader here
reader.Close();
reader.Dispose();
}
}
catch (MySqlException mex)
{
Console.WriteLine(mex.Message);
}
}
closeConnection();
}
}
}
任何时候我 运行 应用程序都会捕获以下异常:"There is already an open DataReader associated with this Connection which must be closed first."
但不应该有。我在这段代码中使用了 1 reader 并且没有其他 运行ning 异步。如您所见,我关闭 reader 并在循环中的每次迭代后打开一个新的。我在应用程序的初始化中使用了与同一连接相关联的 reader,但是当它的数据处理完成后,我关闭它并处理掉它。
如果有人能帮我解开这个谜,我将不胜感激,谢谢。
在您的代码中,如果某些东西在名为 "processing stuff" 的部分生成异常,那么您将让 reader 保持打开状态。
如果您需要确保某些东西在使用后即使在异常情况下也能被处置,那么请使用 using
条款:
if (rowCount == 1)
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM thatTable WHERE someField = " + stuff.property + ";", connection);
using(MySqlDataReader reader = cmd.ExecuteReader())
{
//Processing stuff from the reader here
}
}
我在 C# 中有一个 Windows 表单应用程序,它有一个按钮 运行 代码如下:
private void aCertainButton_Click(object sender, EventArgs e)
{
if (folderFinder.ShowDialog() == DialogResult.OK)
{
if (openConnection())
{
foreach (Thingy stuff in aCertainCollection)
{
string sqlCommandString = "SELECT COUNT(*) FROM thatTable WHERE someField = " + stuff.property + ";";
try
{
MySqlCommand count = new MySqlCommand(sqlCommandString, connection);
int rowCount = 0;
object o = count.ExecuteScalar();
if (o != null)
{
rowCount = int.Parse(o.ToString());
}
if (rowCount == 1)
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM thatTable WHERE someField = " + stuff.property + ";", connection);
MySqlDataReader reader = cmd.ExecuteReader();
//Processing stuff from the reader here
reader.Close();
reader.Dispose();
}
}
catch (MySqlException mex)
{
Console.WriteLine(mex.Message);
}
}
closeConnection();
}
}
}
任何时候我 运行 应用程序都会捕获以下异常:"There is already an open DataReader associated with this Connection which must be closed first."
但不应该有。我在这段代码中使用了 1 reader 并且没有其他 运行ning 异步。如您所见,我关闭 reader 并在循环中的每次迭代后打开一个新的。我在应用程序的初始化中使用了与同一连接相关联的 reader,但是当它的数据处理完成后,我关闭它并处理掉它。
如果有人能帮我解开这个谜,我将不胜感激,谢谢。
在您的代码中,如果某些东西在名为 "processing stuff" 的部分生成异常,那么您将让 reader 保持打开状态。
如果您需要确保某些东西在使用后即使在异常情况下也能被处置,那么请使用 using
条款:
if (rowCount == 1)
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM thatTable WHERE someField = " + stuff.property + ";", connection);
using(MySqlDataReader reader = cmd.ExecuteReader())
{
//Processing stuff from the reader here
}
}