ORA-01403:未找到数据ORA-06512:尝试使用ODP.NET运行存储过程时在第1行

时间:2015-11-05 15:57:46

标签: c# oracle stored-procedures odp.net

我正在构建一个接口,该接口将通过我的.NET Winforms应用程序从Oracle数据库运行存储过程。我能够成功连接到Oracle数据库,并且可以成功地使用ODP.NET API来检索所有存储过程的列表以及我选择的任何存储过程的所有参数。

当我尝试运行存储过程时存在问题。当我尝试执行并返回Oracle错误告诉我没有记录时它会中断,但是当我从sql developer运行存储过程时,它会返回我正在寻找的内容。这是存储过程:

    create or replace procedure sp_get_emp_info 
    (
       empid in INTEGER,
       m_UserName  out VARCHAR2,
       m_Email out VARCHAR2,
       m_IsActive out INTEGER
    ) as 
    begin
     SELECT 
       UserName,
       Email,
       IsActive
     INTO
       m_UserName,
       m_Email,
       m_IsActive
     FROM
       MICHAELTABLE
     WHERE ID = empid;
    end sp_get_emp_info;

这是我写的一个测试类,试图在C#中读取它:

    using Oracle.ManagedDataAccess.Client;
    using System;

    namespace OracleTester
    {
    class Program
    {
        static void Main(string[] args)
        {                      
            OracleConnection conn = new OracleConnection();
            conn.ConnectionString = "User Id=apps;Password=apps;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = Orchid64)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = MVP)));";
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.DarkYellow;

            //string procs = "SELECT object_name FROM dba_objects WHERE object_type = 'PROCEDURE' AND owner = 'APPS'";
            //OracleCommand cmd = new OracleCommand(procs, conn);
            //cmd.CommandType = System.Data.CommandType.Text;
            //cmd.Connection.Open();
            //OracleDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            //int i = 1;
            //while (reader.Read())
            //{
            //    Console.WriteLine(i + ". " +reader.GetString(0));
            //    i++;
            //}

            OracleCommand comm = new OracleCommand();
            comm.CommandText = "SP_GET_EMP_INFO";
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            comm.Connection = conn;

            OracleParameter user = new OracleParameter("m_UserName", OracleDbType.Varchar2, 25);
            user.Direction = System.Data.ParameterDirection.Output;

            OracleParameter email = new OracleParameter("m_Email", OracleDbType.Varchar2, 50);
            email.Direction = System.Data.ParameterDirection.Output;

            OracleParameter active = new OracleParameter("m_IsActive", OracleDbType.Double, 38);
            active.Direction = System.Data.ParameterDirection.Output;

            comm.Parameters.Add(user);
            comm.Parameters.Add(email);
            comm.Parameters.Add(active);

            OracleParameter empID = new OracleParameter("empid", OracleDbType.Double, 38);
            empID.Direction = System.Data.ParameterDirection.Input;
            empID.SourceColumn = "empid";
            empID.Value = 1;
            comm.Parameters.Add(empID);



            comm.Connection.Open();
            OracleDataReader rdr = comm.ExecuteReader();
            while (rdr.Read())
            {
                Console.WriteLine(rdr["m_UserName"].ToString());
            }

            Console.WriteLine("Server Version: " + conn.ServerVersion);
            conn.Dispose();
            Console.ReadLine();
        }
    }
}

当我尝试运行它时,这是我收到的例外:

Oracle.ManagedDataAccess.Client.OracleException was unhandled
  DataSource=""
  ErrorCode=-2147467259
  HResult=-2147467259
  IsRecoverable=false
  Message=ORA-01403: no data found
ORA-06512: at "APPS.SP_GET_EMP_INFO", line 9
ORA-06512: at line 1
  Number=1403
  Procedure=""
  Source=Oracle Data Provider for .NET, Managed Driver
  StackTrace:
       at OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl, Int32& cursorId, Boolean bThrowArrayBindRelatedErrors, OracleException& exceptionForArrayBindDML, Boolean& hasMoreRowsInDB, Boolean bFirstIterationDone)
       at OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteReader(String commandText, OracleParameterCollection paramColl, CommandType commandType, OracleConnectionImpl connectionImpl, OracleDataReaderImpl& rdrImpl, Int32 longFetchSize, Int64 clientInitialLOBFS, OracleDependencyImpl orclDependencyImpl, Int64[] scnForExecution, Int64[]& scnFromExecution, OracleParameterCollection& bindByPositionParamColl, Boolean& bBindParamPresent, Int64& internalInitialLOBFS, OracleException& exceptionForArrayBindDML, Boolean isDescribeOnly, Boolean isFromEF)
       at Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
       at Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteReader()
       at OracleTester.Program.Main(String[] args) in C:\TestProjects\OracleTester\OracleTester\Program.cs:line 54
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

非常感谢任何帮助!!

1 个答案:

答案 0 :(得分:2)

From the documentation

  

默认情况下, OracleCommand.CommandType = Text 命令的ODP.NET参数按位置绑定。 OracleCommand 类提供 BindByName 属性 - 将此属性设置为true以按名称而不是按位置绑定参数。

您正在创建命名参数,但它们实际上是按位置添加的,这意味着您有效地为m_IsActive提供了值(尽管它是一个OUT参数)并且保留empid null ,就程序而言。

您可以将comm.BindByName=true设置为@b_levitt在评论中指出。或者,如果您重新排序参数赋值以匹配形式参数顺序,它将选择正确的值:

OracleParameter empID = new OracleParameter("empid", OracleDbType.Double, 38);
empID.Direction = System.Data.ParameterDirection.Input;
empID.SourceColumn = "empid";
empID.Value = 1;
comm.Parameters.Add(empID);

comm.Parameters.Add(user);
comm.Parameters.Add(email);
comm.Parameters.Add(active);

comm.Connection.Open();
OracleDataReader rdr = comm.ExecuteReader();

当CommandType属性设置为StoredProcedure时,documentation似乎不清楚返回值将包含什么,如果您正在调用函数,它可能会有用;但是通过过程调用没有关联的结果集,所以这可能是空的。在它上面循环找不到任何东西:

while (rdr.Read())
{
    Console.WriteLine(rdr["m_UserName"].ToString());
}

相反,您可以直接引用OUT参数的值:

Console.WriteLine(user.value);