尝试在同一连接中运行多个命令时,C#Winforms Npgsql 3.0.5“正在进行的操作”错误

前端之家收集整理的这篇文章主要介绍了尝试在同一连接中运行多个命令时,C#Winforms Npgsql 3.0.5“正在进行的操作”错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在C# Winforms中运行以下代码段.这段代码适用于pgsql 2.2.6适配器.为了与pgsql3.0.5适配器一起正常工作,可以进行哪些更正?谢谢.
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",conn))
                    cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                SM.Debug("Error on update users IDX for place and zone with value 0 :" + ex.ToString());
            }

所以,在第二个命令语句中,它给出了以下错误

A first chance exception of type ‘System.InvalidOperationException’
occurred in Npgsql.dll

Additional information: An operation is already in progress.

编辑附加信息:

解决方法

您需要处理在第一个ExecuteReader调用中获得的NpgsqlDataReader,使用using语句将其包装起来,就像使用NpgsqlCommand一样.
原文链接:https://www.f2er.com/c/113923.html

猜你在找的C&C++相关文章