ExecuteNonQuery()从Oracle Procedure返回-1

时间:2017-10-23 08:36:56

标签: c# sql oracle

我的.net C# console app

中有以下部分代码
var SGCodProdInsert = new OracleCommand
                        {
                            Connection = con,
                            CommandText = ProductFirstInsert,                                
                            CommandTimeout = 15,
                            Transaction = dbContextTransaction
                        };
                        var t = SGCodProdInsert.ExecuteNonQuery();

我传递的Oracle命令(ProductFirstInsert)是这样的:

BEGIN
INSERT INTO Products_TEST (ProductName,ProductCode) VALUES ('TEST','TEST');
INSERT INTO Products_TEST (ProductName,ProductCode) VALUES ('TEST1','TEST1');
INSERT INTO Products_TEST (ProductName,ProductCode) VALUES ('TEST2','TEST2');

 INSERT INTO Products_TEST (ProductName,ProductCode) VALUES ('TESTn','TESTn');
 END;

结果t我得-1,我明白为什么......所以我的问题如下: 如何从单个整数中获取从oracle过程在表中插入了多少条记录?

2 个答案:

答案 0 :(得分:1)

最简单的方法是在命令之前和之后执行count(*)命令:

var SGCodProdInsert = new OracleCommand
{
    Connection = con,
    CommandText = ProductFirstInsert,
    CommandTimeout = 15,
    Transaction = dbContextTransaction
};
var CountTable = new OracleCommand
{
    Connection = con,
    CommandText = "select count(*) from Products_TEST",
    CommandTimeout = 15,
    Transaction = dbContextTransaction
};
decimal beforeInsert = Convert.ToDecimal(CountTable.ExecuteScalar());
SGCodProdInsert.ExecuteNonQuery();
decimal afterInsert = Convert.ToDecimal(CountTable.ExecuteScalar());

Console.WriteLine($"Inserted rows: {afterInsert - beforeInsert}");

否则你必须创建一个临时的Oracle函数并阅读SQL%ROWCOUNT

答案 1 :(得分:0)

对于UPDATE,INSERT和DELETE语句,返回值是该命令影响的行数。对于所有其他类型的语句,返回值为-1。

微软自己的医生在说。

参考:https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executenonquery?view=dotnet-plat-ext-3.1