c# 调用存储过程

1.调用返回OUT的存储过程

a.调用OUT字符串的存储过程;

csharp 复制代码
      #region 连接数据库
      /// <summary>
      /// 连接数据库
      /// </summary>
      /// <param name="connStatus"></param>
      /// <param name="dbNode"></param>
      /// <returns></returns>
      public OracleConnection OpenConn(ref string connStatus, DBConnEntity dbNode)
      {
          try
          {
              if (conn == null || conn.State == ConnectionState.Closed)
              {
                  //默认端口
                  if (dbNode.Port.Length < 1)
                  {
                      dbNode.Port = "1521";
                  }

                  conn = new OracleConnection();
                  //单节点
                  if (dbNode.IP == null || dbNode.IP2 == null)
                  {
                      conn.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=" + dbNode.IP +
                          ")(PORT=" + dbNode.Port + "))(CONNECT_DATA=(SERVICE_NAME=" + dbNode.DbSid + ")));" +
                      "Persist Security Info=True;User ID=" + dbNode.DbUser + ";Password=" + dbNode.DbPassW + ";";
                  }
                  else
                  {
                      //集群方式
                      string connStr = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =" + dbNode.IP + ")(PORT = " + dbNode.Port + "))(ADDRESS = (PROTOCOL = TCP)(HOST =" + dbNode.IP2 + ")(PORT = " + dbNode.Port + "))(LOAD_BALANCE = yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME =" + dbNode.DbSid + ")(FAILOVER_MODE=(TYPE = session)(METHOD = basic)(RETRIES = 180)(DELAY = 5) ) ));" +
                      "Persist Security Info=True;User ID=" + dbNode.DbUser + ";Password=" + dbNode.DbPassW + ";";
                      conn.ConnectionString = connStr;
                  }

                  conn.Open();
                  connStatus = "success";
              }
              else
              {
                  connStatus = "success";
              }
          }
          catch (Exception ex)
          {
              connStatus = ex.Message;
              conn = null;
          }

          return conn;
      }
//执行存储过程
  public string execCancelJCErpProcedure(string targetId, DBConnEntity dbNode, ref string strNGPASS)
        {
            string returnMsg = "";
            try
            {
                conn = OpenConn(ref strNGPASS, dbNode);
                if (strNGPASS == "success")
                {
                    using (conn)
                    {
                        using (OracleCommand cmd =
                            new OracleCommand("存储过程名称", conn))
                        {
                            cmd.CommandType = CommandType.StoredProcedure;

                            OracleParameter locatorId =
                            new OracleParameter("locatorId", OracleDbType.Varchar2);
                            locatorId.Direction = ParameterDirection.Input;
                            locatorId.Value = targetId;
                            cmd.Parameters.Add(locatorId);

                            OracleParameter vs_msg =
                                new OracleParameter("resultOut", OracleDbType.Varchar2, 4000);//返回字符串时,需要设置字符串大小
                            vs_msg.Direction = ParameterDirection.Output;
                            cmd.Parameters.Add(vs_msg);

                            cmd.ExecuteNonQuery();

                            returnMsg = vs_msg.Value.ToString();                           
                        }
                    }
                }
                //连接数据库异常
                else
                {
                    strNGPASS = "NG";
                    returnMsg = strNGPASS; //连接数据库异常描述
                }
            }
            catch (Exception ex)
            {
                strNGPASS = "NG";
                returnMsg = ex.Message;
            }
            return returnMsg;
        }

b.调用OUT 数值的存储过程;

csharp 复制代码
 public int execZLProcedure(string proceName, DBConnEntity dbNode,string model_name, ref string strNGPASS)
        {
            int batch_Id = 0;
            try
            {
                conn = OpenConn(ref strNGPASS, dbNode);//连接数据库;
                if (strNGPASS == "success")
                {
                    using (conn)
                    {
                        using (OracleCommand cmd = new OracleCommand(proceName, conn))  //proceName存储过程名称
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            OracleParameter modelName = new OracleParameter("modelName", OracleDbType.Varchar2);
                            modelName.Direction = ParameterDirection.Input;
                            modelName.Value = model_name;
                            cmd.Parameters.Add(modelName);

                            OracleParameter batchId = new OracleParameter("batchId", OracleDbType.Int64);//
                            batchId.Direction = ParameterDirection.Output;
                            cmd.Parameters.Add(batchId);

                            cmd.ExecuteNonQuery();

                            batch_Id = int.Parse(batchId.Value.ToString());
                            strNGPASS = "OK";
                        }
                    }
                }
                else    //连接数据库异常
                {
                    strNGPASS = "NG";
                }
            }
            catch (Exception ex)
            {
                strNGPASS = "NG";
            }
            return batch_Id;
        }
相关推荐
明月看潮生1 小时前
青少年编程与数学 02-007 PostgreSQL数据库应用 15课题、备份与还原
数据库·青少年编程·postgresql·编程与数学
明月看潮生1 小时前
青少年编程与数学 02-007 PostgreSQL数据库应用 14课题、触发器的编写
数据库·青少年编程·postgresql·编程与数学
钢铁男儿4 小时前
C# 委托和事件(事件)
开发语言·c#
喜-喜5 小时前
C# HTTP/HTTPS 请求测试小工具
开发语言·http·c#
加酶洗衣粉5 小时前
MongoDB部署模式
数据库·mongodb
Suyuoa5 小时前
mongoDB常见指令
数据库·mongodb
添砖,加瓦5 小时前
MongoDB详细讲解
数据库·mongodb
Zda天天爱打卡5 小时前
【趣学SQL】第二章:高级查询技巧 2.2 子查询的高级用法——SQL世界的“俄罗斯套娃“艺术
数据库·sql
我的运维人生5 小时前
MongoDB深度解析与实践案例
数据库·mongodb·运维开发·技术共享
步、步、为营6 小时前
解锁.NET配置魔法:打造强大的配置体系结构
数据库·oracle·.net