可以使用Enterprise Library从.Net调用Oracle FUNCTION吗?

时间:2009-01-04 22:39:38

标签: .net oracle enterprise-library

我有以下Oracle功能:

function get_job_no return number is
        V_job_no number;
      begin
         select appwork.tlm_corphier_job.nextval into V_job_no from dual;
         return V_job_no;
      end get_job_no;

请注意:
1)这是一个功能,而不是程序
2)返回NUMBER,而不是VARCHAR
3)我碰巧使用System.Data.OracleClient而不是Oracle.DataAccess.Client(由于我遇到的另一个问题),但是使用其中任何一个的解决方案都没问题。

我试图使用MS企业库从VB.Net中调用它,如下所示:

Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data.OracleClient
    Public Function GetNextJobNumber() As Object
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim myresult As Object = Nothing
        Using cmd As DbCommand = db.GetStoredProcCommand("CORPHIER_PKG.get_job_no")
            Dim retval As New Data.OracleClient.OracleParameter("retval", GetType(System.Data.OracleClient.OracleNumber))
            retval.Direction = ParameterDirection.ReturnValue
            cmd.Parameters.Add(retval)
            db.ExecuteNonQuery(cmd)
            myresult = retval.Value
        End Using
        Return myresult
    End Function

我收到以下错误:

System.InvalidCastException:无法将参数值从RuntimeType转换为Decimal。 ---> System.InvalidCastException:Object必须实现IConvertible。    at System.Convert.ChangeType(Object value,Type conversionType,IFormatProvider provider)    at System.Data.OracleClient.OracleParameter.CoerceValue(Object value,MetaType destinationType)    ---内部异常堆栈跟踪结束---    at System.Data.OracleClient.OracleParameter.CoerceValue(Object value,MetaType destinationType)    at System.Data.OracleClient.OracleParameter.SetCoercedValueInternal(Object value,MetaType metaType)    在System.Data.OracleClient.OracleParameterBinding.PrepareForBind(OracleConnection连接,Int32和偏移)    在System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle,CommandBehavior behavior,Boolean needRowid,OciRowidDescriptor& rowidDescriptor,ArrayList& resultParameterOrdinals)    在System.Data.OracleClient.OracleCommand.ExecuteNonQueryInternal(Boolean needRowid,OciRowidDescriptor& rowidDescriptor)    在System.Data.OracleClient.OracleCommand.ExecuteNonQuery()    在Microsoft.Practices.EnterpriseLibrary.Data.Database.DoExecuteNonQuery(DbCommand命令)    在Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteNonQuery(DbCommand命令)

1 个答案:

答案 0 :(得分:1)

好的,这似乎有效:

Public Function GetNextJobNumber() As Object
    Dim db As Database = DatabaseFactory.CreateDatabase()
    Using cmd As DbCommand = db.GetStoredProcCommand("CORPHIER_PKG.get_job_no")
        db.AddParameter(cmd, "retval", DbType.Int32, 0, ParameterDirection.ReturnValue, True, 0, 0, String.Empty, DataRowVersion.Current, Convert.DBNull)
        db.ExecuteNonQuery(cmd)
        Return db.GetParameterValue(cmd, "retval")
    End Using
End Function