Oracle.DataAccess的返回值执行非查询(存储过程)

时间:2011-08-05 11:38:13

标签: oracle stored-procedures odp.net

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

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

要清楚,我应该从成功执行存储过程中获得什么返回值,或者存储过程由于某种原因而无法执行...

我确信它会给我带来某种错误,但是有一个不会执行的实例,它会给我一个返回值吗?

1 个答案:

答案 0 :(得分:7)

对于存储过程,它返回-1,无论sp执行的操作(非常容易测试)

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