【问题标题】:Why I am getting error: An operation is already in progress为什么我收到错误:操作已在进行中
【发布时间】:2019-07-03 12:41:41
【问题描述】:

我收到下一个错误:

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


它的样子:


更多图片在这里:

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

【问题讨论】:

标签: c# postgresql


【解决方案1】:

您应该在使用下一个 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);
...

【讨论】:

    【解决方案2】:

    确保您没有同时混合同步和异步调用。

    【讨论】:

      【解决方案3】:

      尝试将“Preload Reader=true”添加到您的连接字符串中。

      根据Update Asp.net app to Npgsql 3 and removing Preload Reader,它可能会对您有所帮助,因为它将允许数据库驱动程序释放连接。

      【讨论】:

      • 你使用的是哪个版本的 Npgsql 包?
      • 我正在使用 Npgsql 3.0.5
      • 这是我得到的错误NotSupportedException: The PreloadReader parameter is no longer supported. Please see http://www.npgsql.org/doc/3.0/migration.html
      猜你喜欢
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 2021-05-04
      • 2019-11-23
      • 1970-01-01
      相关资源
      最近更新 更多