C#的WinForms Npgsql 3.0.5"正在进行"的操作;试图运行相同的连接中的多个命令时出错 [英] C# Winforms Npgsql 3.0.5 "An operation already in progress" error when trying to run multiple commands inside same connection
问题描述
我试图运行在C#中的WinForms下面的代码片段。这段代码与pgsql的2.2.6适配器运作良好。什么修正可以以做工精细用pgsql3.0.5适配器进行?谢谢
I'm trying to run the following snippet in C# Winforms. This piece of code is working well with pgsql 2.2.6 adapter. What correction can be made in order to work fine with pgsql3.0.5 adapter? Thanks.
NpgsqlConnection conn = new NpgsqlConnection(MainForm2.MyConString);
{
conn.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand("SELECT rfid,name,sc_id from passenger ORDER by name", conn))
{
NpgsqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var obj = new PassengerClass
{
RFID = dr.GetString(0),
Name = dr.GetString(1),
sc_id = dr.GetInt32(2)
};
s = dr.GetString(0);
try { ret.Add(s, obj); }
catch (Exception ex) { SM.Debug("Fail to add RFID Name in hash RFID:" + s + ex.ToString()); }
}
}
MainForm2.PassHash = ret;
try
{
using (NpgsqlCommand cmd = new NpgsqlCommand(string.Format("UPDATE place set useridx ={0} where useridx=0", MainForm2.userIDX), conn))
cmd.ExecuteNonQuery();
using (NpgsqlCommand cmd = new NpgsqlCommand(string.Format("UPDATE zonename set useridx ={0} where useridx=0", MainForm2.userIDX), conn))
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
SM.Debug("Error on update users IDX for place and zone with value 0 :" + ex.ToString());
}
因此,在第二个命令语句它给了我以下错误:
So, at the second command statement it gives me the following error:
型的第一个机会异常'System.InvalidOperationException'
发生在Npgsql.dll
A first chance exception of type 'System.InvalidOperationException' occurred in Npgsql.dll
更多信息:操作已在进行中。
Additional information: An operation is already in progress.
推荐答案
您需要处置其在第一个的ExecuteReader调用得到NpgsqlDataReader,用using语句包住它,就像你与你的NpgsqlCommand做的。
You need to dispose of the NpgsqlDataReader which you get in the first ExecuteReader call, wrap it with a using statement just like you do with your NpgsqlCommand.
这篇关于C#的WinForms Npgsql 3.0.5"正在进行"的操作;试图运行相同的连接中的多个命令时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!