为什么我会遇到错误:“操作已经在进行中”?

5
我遇到了以下错误:

System.InvalidOperationException: 操作已在进行中。

我不理解它的原因。如果我注释掉开始和结束的块,一切都正常工作。如果不注释,我就会遇到错误。有什么想法吗?
     public void PGConnect()
    {
        List<UserData> uds = new List<UserData>();
        UserData ud;

        List<string> dblist = GetListDBsList();
        if (dblist.Contains(config.PGdbName))
        {
            Console.WriteLine("Data Base exists: {0}", config.PGdbName);
        }

        else
        {
            Console.WriteLine("Data Base DO NOT exists: {0}", config.PGdbName);
            return;
        }

        NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=" + config.PGLogin + ";" +
           "Password=" + config.PGPass + ";Database=" + config.PGdbName + ";");

        //select datname from pg_database;

        try
        {
            conn.Open();

            Console.WriteLine("PG Connected");
        }

        catch(SocketException e)
        {
            Console.WriteLine(e.Message);

        }

        //start
        string tablesListRequestSQL = @"SELECT table_name FROM information_schema.tables WHERE table_schema='public'";
        NpgsqlCommand commandGetDBTables = new NpgsqlCommand(tablesListRequestSQL, conn);
        NpgsqlDataReader drGetDBTables = commandGetDBTables.ExecuteReader();
        //tr.Commit();
        while (drGetDBTables.Read())
        {
            existsInDBTables.Add(drGetDBTables[0].ToString());
        }

        foreach (string table in requireTablesList) // 
        {
            if (!existsInDBTables.Contains(table)) // if element from requireTablesList do not found -> DB have not simmilar Tables!
            {
                notFoundedTables.Add(table);
            }

        }

        if (notFoundedTables.Count != 0) // if not empty
        {
            Console.WriteLine("Next tables are marked as reqired for Sync, but can't be found in DataBase: ");

            foreach (var table in notFoundedTables)
            {
                Console.WriteLine(table);
            }
        }
        // end 


        Console.ReadKey();


         NpgsqlCommand command = new NpgsqlCommand("SELECT city, state FROM cities", conn);


        try
        {

            NpgsqlDataReader dr = command.ExecuteReader(); // VS show error here
            while (dr.Read())
            {
               // UserData ud = new UserData();
                ud.id = Int32.Parse(dr[0].ToString());
                ud.guid = (dr[1].ToString());
                ud.name = (dr[2].ToString());
                ud.userblob = (byte[])dr[3];
                uds.Add(ud);
                //File.WriteAllBytes("outputimg.jpg", ud.userblob);
                //Console.ReadKey();

            }

        }

        finally
        {
            conn.Close();
        }

操作环境:Windows 7,VS2015,PG9.5


界面截图如下: my picture looks like this


更多截图请参见以下链接:

http://img.ctrlv.in/img/16/04/20/5717882d0b968.png

http://img.ctrlv.in/img/16/04/20/57179041abaa4.png

http://img.ctrlv.in/img/16/04/20/571790825648f.png


这是在树莓派上吗?https://github.com/npgsql/npgsql/issues/826 - Rob
3个回答

8

在使用下一个DbDataReader之前,您应该关闭先前的对象。不关闭会导致此异常。

...    
if (notFoundedTables.Count != 0) // if not empty
{
    ...
}
// end 

/** IMPORTANT **/
/** Closing Data Reader **/
drGetDBTables.Close();

Console.ReadKey();

NpgsqlCommand command = new NpgsqlCommand("SELECT city, state FROM cities", conn);
...

1

请确保您不同时混合使用同步和异步调用。


0

你正在使用哪个版本的Npgsql包? - VitaliyK
我正在使用 Npgsql 3.0.5。 - Dmitry Bubnenkov
这是我收到的错误信息:NotSupportedException: 不再支持 PreloadReader 参数。请参见 http://www.npgsql.org/doc/3.0/migration.html - Prashant Lakhlani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接