【问题标题】:Return Value of Oracle.DataAccess execute non-query (stored proc)Oracle.DataAccess 的返回值执行非查询(存储过程)
【发布时间】:2011-10-20 19:15:31
【问题描述】:

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

这就是 microsofts 文档关于该函数的返回值的说明...这是否意味着如果我调用存储过程,它将返回 -1?

需要明确的是,如果存储过程成功执行或者存储过程由于某种原因无法执行,我应该期望收到什么返回值...

我确信它会给我带来某种错误,但是是否存在它不会执行并且会给我一个返回值的实例?

【问题讨论】:

  • 您的问题是“存储过程将返回 -1”?你为什么不测试呢?您测试了 CREATE TABLE 和 DROP TABLE 语句、插入、更新...
  • 嗯,它说对于所有其他语句,返回值为-1?我假设包括存储过程......我还无法确认。我希望这里有人有使用 ODP.Net 调用存储过程的经验。

标签: oracle stored-procedures odp.net


【解决方案1】:

不管sp执行什么操作,它都会为存储过程返回-1(很容易测试)

create procedure test1 as 
begin
    null ; --do nothing
end test1 ;
/

create table testtable(a number);
/

create procedure test2 as
begin
    insert into testtable(a) select level from dual connect by level < 5;
end test2  ;
/

create procedure test3 as
begin
    update testtable set a = a-1;
end test3;
/

create procedure test4 as
begin
    delete testtable;
end test4;
/

        static int executeProc(string procName,OracleConnection connection ){
                OracleCommand cmd= new OracleCommand(procName, connection);
                cmd.CommandType = CommandType.StoredProcedure;
                return cmd.ExecuteNonQuery();                   
        }
        static void Main(string[] args)
        {
            Console.WriteLine("what does ExecuteNonQuery return?");
            // Connect
            string connectStr = getConnection();
            OracleConnection connection = new OracleConnection(connectStr);
            connection.Open();
            try{
            Console.WriteLine("test1 =>" + executeProc("test1",connection));
            Console.WriteLine("test2 =>" + executeProc("test2",connection));
            Console.WriteLine("test3 =>" + executeProc("test3",connection));
            Console.WriteLine("test4 =>" + executeProc("test4",connection));
            }
            catch (Exception e){
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("Done");
        }

what does ExecuteNonQuery return?
test1 =>-1
test2 =>-1
test3 =>-1
test4 =>-1

/*
drop table testtable;
drop procedure test1;
drop procedure test2;
drop procedure test3;
drop procedure test4;
*/

参考: http://download.oracle.com/docs/cd/E11882_01/win.112/e18754/OracleCommandClass.htm#i998363 http://forums.oracle.com/forums/thread.jspa?threadID=636182

【讨论】:

  • 测试代码和参考...先生,这是一个高质量的答案。谢谢。
  • 如果你执行一个由 BEGIN/END 包围的语句块,它也会返回 -1。
猜你喜欢
  • 2011-08-03
  • 1970-01-01
  • 2012-12-26
  • 2012-12-24
  • 2019-02-16
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多