合并查询 returns -1 而不是合并的行数

Merge query returns -1 instead of number of rows merged

我们有一个针对 Oracle 数据库运行并使用数据集、数据表和表适配器的旧应用程序。它使用现已弃用的 System.Data.OracleClient,我们希望用 DevArt 中的 dll 替换此 dll。

我使用 Oracle 迁移向导从 System.Data.OracleClient 迁移到 Devart.Data.Oracle。迁移后,所有查询继续正常工作,除了一种类型:MERGE 查询。他们仍然很好地合并记录(预期的记录是 inserted/updated)。但是,以前返回合并的记录数,但现在总是 returns "-1" 而不是。

知道问题出在哪里吗?最好不必手动更新所有合并查询(有很多合并查询),但如果更新所有查询是解决此问题的唯一方法,那也是可以接受的。

处理数据库连接的代码由 DataSet 文件生成:

Devart.Data.Oracle.OracleCommand command = "MERGE INTO [...]";
System.Data.ConnectionState previousConnectionState = command.Connection.State;
if ((command.Connection.State & System.Data.ConnectionState.Open) != System.Data.ConnectionState.Open)
{
    command.Connection.Open();
    int returnValue;
    try
    {
        returnValue = command.ExecuteNonQuery();
    }
    finally
    {
        if (previousConnectionState != System.Data.ConnectionState.Closed)
        {
            command.Connection.Close();
        }
    }
}
return returnValue;

我找到了一篇关于从 oracle 客户端迁移到 devart 的文章。他基本上说"this is how devart works"。您可以在下面找到详细信息:

Devart can return the number of affected rows only for INSERT, UPDATE and DELETE DML commands, in all the other cases it returns -1. We used these return values sometimes in our api to indicate whether the operation was successful or not. With these 3 DML commands it is ok. We have several usages of oracle’s MERGE command too, OracleClient can return the correct number of affected rows, devart returns -1. We changed our API where MERGE is used to not count on the return value.

要阅读全文,请单击 here

在ODP.NET中ExecuteNonQuery对于Oracle语句merge也returns-1。 我知道的唯一解决方法是在 PLSQL 块中调用它并使用 SQL%ROWCOUNT,如下所示:

  string sql =
      "begin " +
      "  merge into test t1 " +
      "    using (select col1, col2 from test) t2 on (t2.col1=t1.col1) " +
      "    when matched then update set col2=col2+1; " +
      "  :num := sql%rowcount; "+
      "end;";
  OracleCommand cmd = new OracleCommand(sql, myConnection);
  var p = cmd.Parameters.Add(":num", OracleDbType.Int32, ParameterDirection.Output);
  cmd.ExecuteNonQuery();
  Console.WriteLine("Rows affected: " + p.Value);

--------------------------
Sample output for my data: 
Rows affected: 2

我希望你能 get/find 为 Devart 提供更好的答案。