为什么我收到错误:操作已在进行中

时间:2016-04-20 13:28:49

标签: c# postgresql

我收到下一个错误:

  

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。 PG 9.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

3 个答案:

答案 0 :(得分:5)

在使用下一个DbDataReader之前,您应关闭之前的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)

尝试在连接字符串中添加“Preload Reader = true”。

根据这个Update Asp.net app to Npgsql 3 and removing Preload Reader,它可以帮助你,因为它将允许db驱动程序释放连接。

答案 2 :(得分:0)

确保您不会同时将同步和异步呼叫混在一起。