我正在尝试使用Oracle.DataAccess从 Visual Studio 2012 (c#) 连接到 Oracle 数据库并运行作为 PACKAGE 一部分的 FUNCTION。下面是SQL:
FUNCTION TEST (test_name in varchar2,
test_year in number,
test_outmessage out varchar2)
RETURN NUMBER IS
test_exists VARCHAR2(1) := 'N';
BEGIN
BEGIN
SELECT 'Y' INTO test_exists
FROM Test_Table
WHERE table_name = test_name
AND table_year = test_year;
EXCEPTION
WHEN NO_DATA_FOUND THEN
test_exists := 'N';
WHEN OTHERS THEN
test_outmessage := 'error'
DBMS_OUTPUT.put_line (test_outmessage);
RETURN -1;
END;
IF test_exists = 'Y' THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END TEST;
我已经编写了 c# 代码来从我的应用程序中执行此功能:
using (OracleConnection connection = new OracleConnection("Data Source=Database; User ID=user; Password=pass"))
{
OracleCommand command = connection.CreateCommand();
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "USER.PACKAGE.TEST";
OracleParameter test_name = new OracleParameter();
test_name.ParameterName = "test_name";
test_name.OracleDbType = OracleDbType.NVarchar2;
test_name.Direction = System.Data.ParameterDirection.Input;
test_name.Value = "myname";
command.Parameters.Add(test_name);
OracleParameter test_year = new OracleParameter();
test_year.ParameterName = "test_year";
test_year.OracleDbType = OracleDbType.Decimal;
test_year.Direction = System.Data.ParameterDirection.Input;
test_year.Value = 2004;
command.Parameters.Add(test_year);
OracleParameter test_outmessage = new OracleParameter();
test_outmessage.ParameterName = "test_outmessage ";
test_outmessage.OracleDbType = OracleDbType.NVarchar2;
test_outmessage.Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(test_outmessage);
OracleParameter result = new OracleParameter();
result.ParameterName = "ReturnValue";
result.OracleDbType = OracleDbType.Decimal;
result.Direction = System.Data.ParameterDirection.ReturnValue;
command.Parameters.Add(result);
try
{
connection.Open();
command.ExecuteNonQuery();
string r = result.Value.ToString();
string r1 = test_outmessage.Value.ToString();
}
catch (Exception)
{
}
finally
{
connection.Close();
}
}
运行此代码时也不例外,但是,返回值(r 和 r1)为空。在函数中,我猜想字符串 r应该是 -1、1 或 0 。