微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

为了清楚起见,我应该从成功执行存储过程中获得什么返回值,或者存储过程由于某种原因未能执行…

我确信它会给我一个错误,但有一个实例,它不会执行,它会给我一个返回值?

解决方法

对于存储过程,它返回-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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐