从.NET调用Oracle函数作为存储过程

时间:2013-06-10 14:26:04

标签: asp.net vb.net oracle stored-procedures plsql

我编写了一个简单的Oracle函数,名为Testing,没有返回字符串的参数。当我尝试从我的.NET应用程序调用它时,我收到一个错误:

System.Data.OracleClient.OracleException:

  

ORA-06550:第1行,第1列   7:“”PLS-00221:'TESTING2'不是程序或未定义“。

当我将其更改为“从双重选择测试()”并将CommandType更改为文本时,它可以正常工作。我错过了什么?

Dim oracleConn As OracleConnection = CreateConnection(<connection info here>)

    Dim oracleCmd As New OracleCommand()
    oracleCmd.Connection = oracleConn
    'oracleCmd.CommandText = "SELECT TESTING2() FROM DUAL" 'this works
    oracleCmd.CommandText = "TESTING2"   'this does not work

    oracleCmd.CommandType = CommandType.StoredProcedure

    'oracleCmd.ExecuteReader()  'also tried this
    Dim tmpVar As String = oracleCmd.ExecuteScalar()

create or replace FUNCTION testing2
RETURN VARCHAR2
AS      
begin
  return 'hello';
end;

1 个答案:

答案 0 :(得分:1)

我不再使用Oracle了,所以我现在无法测试,但你告诉我这个例子是否有效

Dim oracleConn As OracleConnection = CreateConnection(<connection info here>)
Dim oracleCmd As New OracleCommand()
oracleCmd.Connection = oracleConn
oracleCmd.CommandText = "TESTING2"   
oracleCmd.CommandType = CommandType.StoredProcedure
Dim prm = new OracleParameter("returnvalue", OracleType.VarChar)
prm.Size = 1024
prm.Direction = ParameterDirection.ReturnValue
oracleCmd.Parameters.Add(prm)
oracleCmd.ExecuteNonQuery()

Console.WriteLine(prm.Value.ToString)

顺便说一句,我想CreateConnection会返回一个OPEN连接吗?