Passing Null/empty string to Oracle stored procedure from asp.net

你离开我真会死。 提交于 2019-12-31 03:48:24

问题


We have an ASP.NET web service that invokes a stored procedure on our DB (Oracle 11g). The problem is that the call to the procedure blows up every time an empty string is passed as one of the parameters. We're seeing an ORA-01084 error, which indicates to me that call is failing before the procedure is actually run.

Here's the procedure, minus some logic that I believe is not relevant:

PROCEDURE CreateReport
( 
  from_dt IN NVARCHAR2,
  to_dt IN NVARCHAR2,
  p_table_id   IN NVARCHAR2,
  p_column_id  IN NVARCHAR2,
  device_name IN NVARCHAR2,
  ret_cursor OUT t_cursor
)
AS
  v_cursor t_cursor;
  dateStr NVARCHAR2(200);
  sqlStmt VARCHAR2(10000);
  extraColumns NVARCHAR2(10000);
BEGIN

    /* SNIP Cut logic that builds the query statement based on inputs */

    OPEN v_cursor FOR sqlStmt;
    ret_cursor := v_cursor;

END CreateReport;

Here's the invocation of CreateReport from the web service:

public DataSet CreateReport(string fromDt, string toDate, string tableNames, 
                            string columnNames, string deviceName)
{
    DataSet dataSet;

    string fmDateStr = String.IsNullOrEmpty(fromDt) ? String.Empty : fromDt.Trim();
    string toDateStr = String.IsNullOrEmpty(toDate) ? String.Empty : toDate.Trim();

    OracleCommand oleDBCmd = new OracleCommand();

    oleDBCmd.CommandType = CommandType.StoredProcedure;
    oleDBCmd.CommandText = "Web_Pkg.CreateReport";

    oleDBCmd.Parameters.Add(new OracleParameter("from_dt", OracleType.NVarChar));
    oleDBCmd.Parameters["from_dt"].Value = fmDateStr;
    oleDBCmd.Parameters["from_dt"].Direction = ParameterDirection.Input;

    oleDBCmd.Parameters.Add(new OracleParameter("to_dt", OracleType.NVarChar));
    oleDBCmd.Parameters["to_dt"].Value = toDateStr;
    oleDBCmd.Parameters["to_dt"].Direction = ParameterDirection.Input;

    oleDBCmd.Parameters.Add(new OracleParameter("p_table_id", OracleType.NVarChar));
    oleDBCmd.Parameters["p_table_id"].Value = tableNames;
    oleDBCmd.Parameters["p_table_id"].Direction = ParameterDirection.Input;

    oleDBCmd.Parameters.Add(new OracleParameter("p_column_id", OracleType.LongVarChar));
    oleDBCmd.Parameters["p_column_id"].Value = columnNames;
    oleDBCmd.Parameters["p_column_id"].Direction = ParameterDirection.Input;

    oleDBCmd.Parameters.Add(new OracleParameter("device_name", OracleType.LongVarChar));
    oleDBCmd.Parameters["device_name"].Value = deviceName;
    oleDBCmd.Parameters["device_name"].Direction = ParameterDirection.Input;


    oleDBCmd.Parameters.Add(new OracleParameter("ret_cursor", OracleType.Cursor));
    oleDBCmd.Parameters["ret_cursor"].Direction = ParameterDirection.Output;

    try
    {
        // Throws exception if any of the paramters are String.Emtpy
        dataSet = DB.SQLSelect(oleDBCmd, connStr);
    }
    catch (Exception exp)
    {
        return null;
    }
    finally
    {
        if (oleDBCmd != null)
        {
            oleDBCmd.Dispose();
        }
    }

    return dataSet;
}

As an experiment, I modified the web service to pass null rather than an empty strings. When null is passed in, I see an error indicating "wrong number or types of arguments in call to 'CREATEREPORT."

I also tried passing DBNull.Value whenever the params were null/empty, but that resulted in the error

Parameter 'p_column_id': No size set for variable length data type: String.

(Of course, p_column_id was the empty parameter in this case).

So, how can I successfully pass empty strings as parameters to my stored procedure? We definitely want to allow the p_column_id parameter to be empty.


回答1:


You can do the following for any nullable parameters.

oleDBCmd.Parameters.Add(new OracleParameter("to_dt", OracleType.NVarChar));
if(string.IsNullOrEmpty(toDateStr)) {
    oleDBCmd.Parameters["to_dt"].Value = DBNull.Value;
} else {
    oleDBCmd.Parameters["to_dt"].Value = toDateStr;
}
oleDBCmd.Parameters["to_dt"].Direction = ParameterDirection.Input;

That way, you're not relying on string -> null conversion by the oracle adapter.

Edit: If this doesn't fix the issue, it is most possibly a mismatch between types, check NVarChar vs VarChar



来源:https://stackoverflow.com/questions/2359324/passing-null-empty-string-to-oracle-stored-procedure-from-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!