使用Npgsql的多个游标?

时间:2018-10-22 20:46:18

标签: c# sql postgresql npgsql

我习惯于(使用libpq从Ada到)

  • 打开光标

  • 获取一些键值

  • 将这些键值用作其他语句的绑定参数。

但是我得到了Npgsql.NpgsqlOperationInProgressException 代替。

我曾经使用过sql-server,但是通过在连接字符串(多个活动记录集)中添加“ MARS”解决了该问题

我可以在这里做类似的事情吗?

这是我想要做的:

            conn.Open();

        // Define a query
        NpgsqlCommand cmdGetSelectionIds = new NpgsqlCommand("select distinct(R.SELECTIONID) from ARUNNERS R, AMARKETS M " +
                                              "where true " +
                                              "and M.MARKETID = R.MARKETID " +
                                              "and M.MARKETTYPE = 'WIN' " +
                                              "and R.STATUS <> 'REMOVED'", conn);


        NpgsqlCommand cmdNumWins = new NpgsqlCommand("select count('a') from ARUNNERS R, AMARKETS M " +
                                              "where true " +
                                              "and M.MARKETID = R.MARKETID " +
                                              "and R.SELECTIONID = @selid " +
                                              "and M.MARKETTYPE = 'WIN' " +
                                              "and R.STATUS = 'WINNER'", conn);

        NpgsqlCommand cmdNumPlcs = new NpgsqlCommand("select count('a') from ARUNNERS R, AMARKETS M " +
                                              "where true " +
                                              "and M.MARKETID = R.MARKETID " +
                                              "and R.SELECTIONID = @selid " +
                                              "and M.MARKETTYPE = 'PLACE' " +
                                              "and R.STATUS = 'WINNER'", conn);

        NpgsqlCommand cmdNumLosses = new NpgsqlCommand("select count('a') from ARUNNERS R, AMARKETS M " +
                                              "where true " +
                                              "and M.MARKETID = R.MARKETID " +
                                              "and R.SELECTIONID = @selid " +
                                              "and M.MARKETTYPE = 'WIN' " +
                                              "and R.STATUS = 'LOSER'", conn);

        NpgsqlDataReader drGetSelectionIds = cmdGetSelectionIds.ExecuteReader();

        while (drGetSelectionIds.Read())
        {
            selid = drGetSelectionIds.GetInt32(0);


            cmdNumWins.Parameters.AddWithValue("selid", selid);
            using (NpgsqlDataReader drNumWins = cmdNumWins.ExecuteReader())
            { 
                if (drNumWins.Read()) numWins = drNumWins.GetInt32(0);
            }

            using (NpgsqlDataReader drNumPlcs = cmdNumPlcs.ExecuteReader())
            {
                if (drNumPlcs.Read()) numPlcs = drNumPlcs.GetInt32(0);
            }

            using (NpgsqlDataReader drNumLosses = cmdNumLosses.ExecuteReader()) {
                if (drNumLosses.Read()) numPlcs = drNumLosses.GetInt32(0);
            }

            Console.Write("selid : {0} \t num W {1} \t num P {2} num L {3} \t points {4}\n", selid, numWins, numPlcs, numLosses, (3.0*numWins + numPlcs )/(numWins+numLosses));


        }
        // Close connection
        conn.Close();

是的,我可以将第一条语句读入列表并循环遍历, 但是我确实有一些像上面那样构建的旧代码。

/比约恩

2 个答案:

答案 0 :(得分:0)

问题是NpgSql只允许一次打开一个数据读取器。如果您需要同时针对同一连接打开多个数据读取器,那么我认为可能会有更好的方法来达到最终目标。

因此,免责声明...不要这样做。但是,如果您想做上面描述的事情,这将是实现它的一种方法:

cmdNumWins.Parameters.Add(new NpgsqlParameter("@selid", NpgsqlDbType.Integer));
cmdNumPlcs.Parameters.Add(new NpgsqlParameter("@selid", NpgsqlDbType.Integer));
cmdNumLosses.Parameters.Add(new NpgsqlParameter("@selid", NpgsqlDbType.Integer));

List<int> idList = new List<int>();

using (NpgsqlDataReader drGetSelectionIds = cmdGetSelectionIds.ExecuteReader())
{
    while (drGetSelectionIds.Read())
        idList.Add(drGetSelectionIds.GetInt32(0));

    drGetSelectionIds.Close();
}

foreach (int selid in idList)
{
    cmdNumWins.Parameters[0].Value = selid;
    cmdNumPlcs.Parameters[0].Value = selid;
    cmdNumLosses.Parameters[0].Value = selid;

    numWins = Convert.ToInt32(cmdNumWins.ExecuteScalar());
    numPlcs = Convert.ToInt32(cmdNumPlcs.ExecuteScalar());
    numLosses = Convert.ToInt32(cmdNumLosses.ExecuteScalar());

    Console.Write("selid : {0} \t num W {1} \t num P {2} num L {3} \t points {4}\n", selid, 
        numWins, numPlcs, numLosses, (3.0 * numWins + numPlcs) / (numWins + numLosses));
}

更好的方法是将所有操作作为一个查询来完成:

select
  r.selectionid,
  count (1) filter (where m.markettype = 'WIN' and r.status = 'WINNER') as win,
  count (1) fitler (where m.markettype = 'PLACE' and r.status = 'WINNER') as place,
  count (1) filter (where m.markettype = 'WIN' and r.status = 'LOSER') as lose
from
  arunners r
  join amarkets m on m.marketid = r.marketid
where
  r.status != 'REMOVED'
group by
  r.selectionid

我可能已经错过了一些关于您的逻辑的细微差别,但希望您能理解。这应该一口气快速有效地完成C#逻辑的工作。它将对数据库友好得多,对于大型数据集,它应该更快一些。

答案 1 :(得分:0)

如上所述,Npgsql实际上只允许在给定的连接上同时打开一个数据读取器(与MSSQL的MARS功能不同)。但是,您可以执行一些类似于by manually using server-side cursors yourself的操作。换句话说,您可以运行一个查询并从其游标中获取X行,然后根据第一个查询的输出执行第二个查询。您随时可以返回第一个查询并获取X个以上的行,等等。

换句话说,虽然Npgsql不允许同时使用多个阅读器,但PostgreSQL确实允许同时使用多个 query (或游标)时间。