简体   繁体   中英

How to use function to pass parameter to oracle stored procedure in c#

I want to pass a HEX string as the parameter to a oracle stored procedure in c# program. The stored procedure take varchar2 as the input parameter. Therefore I need to convert the HEX to RAW and then RAW to varchar2 . The idea is shown in the following codes.

OracleCommand cmd = new OracleCommand();

cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = 
  "call checking(utl_raw.cast_to_varchar2(hextoraw(:in_data)), :return_cde)";

cmd.Parameters.Add("in_data", 
                    OracleDbType.Varchar2, 
                    5000, 
                   "000102030405060708090A0B0C0D", 
                    ParameterDirection.Input);

cmd.Parameters.Add("return_cde", 
                    OracleDbType.Varchar2, 
                    8, 
                   "OK", 
                    ParameterDirection.InputOutput);

cmd.ExecuteNonQuery();

return_cde = cmd.Parameters["return_cde"].Value.ToString();

At the moment, this code cannot work . Can anyone give me suggestion on how I can do it correctly.

In case of Oracle try using anonymous block ( begin..end; syntax, not call ):

//DONE: do not forget to dispose IDsiposable
using (OracleCommand cmd = new OracleCommand()) {
  cmd.Connection = conn;
  cmd.CommandType = CommandType.Text; // redundant, can be dropped

  cmd.CommandText = 
    @"begin 
        checking(utl_raw.cast_to_varchar2(hextoraw(:in_data)), :return_cde);
      end";

  // 5000 - be careful - in SQL Oracle can operate with VarChar2 up to 4000;
  // in PL/SQL, however, the limit is 32000 
  cmd.Parameters.Add("in_data", 
                      OracleDbType.Varchar2, 
                      5000, 
                     "000102030405060708090A0B0C0D",
                      ParameterDirection.Input);

  cmd.Parameters.Add("return_cde", 
                      OracleDbType.Varchar2, 
                      8, 
                     "OK", 
                      ParameterDirection.InputOutput);

  cmd.ExecuteNonQuery();

  return_cde = cmd.Parameters["return_cde"].Value.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM