我正在尝试从 Oracle PL / SQL 中的 C#.NET 中的存储过程中获取字符串。 Oracle SQL Developer中的查询正常,但C#中的输出参数始终返回" 1" 。我尝试过设置不同的参数大小,方向等。没有任何帮助。它将返回" 1" / null或抛出错误。我还添加了一个INT32参数用于测试,每次都会返回null。以上是简化的测试查询和C#代码。在这个愚蠢的事情上挣扎了两天。
string sql = @"
DECLARE
eligProdFinal1 nvarchar2(128);
status integer;
testNR integer;
BEGIN
--dbms_output.put_line( eligProdFinal1 || '|' || eligProdFinal2 || '|' || eligProdFinal3 );
dbms_output.put_line('TEST');
eligProdFinal1 := '';
dbms_output.get_line( :eligProdFinal1, :status ); --status 0 is OK
dbms_output.put_line(37);
dbms_output.get_line( :testNR, :status ); --status 0 is OK
END;
";
.NET:
//added to get DBMS Ouput Line from a query in PL/SQL ORACLE
public static string GetDbmsOutputLine(string sqlExp)
{
string dbConSAPCCDEV = "...";
using (var connection = new OracleConnection() { ConnectionString = dbConSAPCCDEV })
{
using (OracleCommand command = new OracleCommand())
{
command.Connection = connection;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = string.Format(sqlExp);
connection.Open();
OracleParameter statusParameter = new OracleParameter();
statusParameter.ParameterName = "status";
statusParameter.Direction = ParameterDirection.Output;
command.Parameters.Add(statusParameter);
//tried adding the param like this, or in a single line like below
//OracleParameter lineParameter = new OracleParameter();
//lineParameter.ParameterName = "eligProdFinal1";
//lineParameter.OracleDbType = OracleDbType.Varchar2;
//lineParameter.Size = 760;
//lineParameter.Direction = ParameterDirection.ReturnValue;
//command.Parameters.Add(lineParameter);
command.Parameters.Add(new OracleParameter("eligProdFinal1", OracleDbType.NVarchar2, 128, null, ParameterDirection.Output));
OracleParameter testParameter = new OracleParameter();
testParameter.ParameterName = "testNR";
testParameter.Direction = ParameterDirection.Output;
command.Parameters.Add(testParameter);
command.ExecuteNonQuery();
if (command.Parameters["eligProdFinal1"].Value is DBNull)
return null;
string output = command.Parameters["eligProdFinal1"].Value.ToString();
string testNr = command.Parameters["testNR"].Value.ToString();
connection.Close();
return output;
}
}
}