c# 执行调用Oracle Procedure传参及回传值

时间:2021-10-31 04:27:23

////定義參數
               //IDataParameter[] parameters =
               //             {
               //                 new OracleParameter(":p_stage", OracleDbType.Varchar2,model.Stage, ParameterDirection.Input),//传值
               //                  new OracleParameter(":ReturnValue", OracleDbType.Varchar2, ParameterDirection.Output)//回传
               //             };

               ////參數賦值
               ////parameters[0].Value = model.Stage;
               //string prconn = System.Configuration.ConfigurationManager.AppSettings["DBConnStr_tnboa1insert"];
               //OracleConnection conn = new OracleConnection(prconn);
               //conn.Open();
               //OracleCommand cmd = new OracleCommand("PRO__SETTLEMENT", conn);  //Procedure &连接数据库
               //cmd.CommandType = CommandType.StoredProcedure;
               //cmd.Parameters.AddRange(parameters);
               //OracleDataAdapter oda = new OracleDataAdapter(cmd);
               //DataTable dt = new DataTable();
               //oda.Fill(dt);
               //conn.Close();
               //string iReturn = cmd.Parameters["RETURNVALUE"].Value.ToString();
               ////return ds.Tables[0];

              // string iReturn = ConnInstance.ExecuteQuery_Sp("PRO_SETTLEMENT", parameters);//执行完成后得到回传的结果



               DataTable dt = new DataTable();
               string prconn = System.Configuration.ConfigurationManager.AppSettings["DBConnStr_tnboa1insert"];
               OracleConnection conn = new OracleConnection(prconn);
               conn.Open();
               OracleCommand cmd = new OracleCommand("PRO_SETTLEMENT",conn);  //Procedure&连接数据库
               //cmd.CommandText = "PRO_SETTLEMENT";  //Procedure

               cmd.CommandType = CommandType.StoredProcedure;

               cmd.Parameters.Add("P_STAGE", OracleDbType.Varchar2).Direction = ParameterDirection.Input;//传值类型定义
                cmd.Parameters["P_STAGE"].Value = model.Stage;//赋值
                cmd.Parameters.Add("RETURNVALUE", OracleDbType.Varchar2,200).Direction = ParameterDirection.Output;//回传  注:回传定义字符长度,忽略会报错

                cmd.ExecuteNonQuery();
                string iReturn = cmd.Parameters["ReturnValue"].Value.ToString();////执行完成后得到回传的结果
                conn.Close();