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;
        }
相关推荐
马克学长6 小时前
SSM青岛恒星科技学院机房管理系统0k0u9(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·科技·ssm 框架·教育信息化·机房管理系统·青岛恒星科技学院
7***68437 小时前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
L***d6707 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
java_logo7 小时前
MySQL Server Docker 容器化部署指南
linux·运维·数据库·docker·容器
likuolei7 小时前
XSL-FO 软件
java·开发语言·前端·数据库
p***95007 小时前
Springboot3 Mybatis-plus 3.5.9
数据库·oracle·mybatis
CS_浮鱼7 小时前
【MySQL】InnoDB存储引擎
数据库·mysql
合作小小程序员小小店7 小时前
桌面开发,在线%信息管理%系统,基于vs2022,c#,winform,sql server数据。
开发语言·数据库·sql·microsoft·c#
q***18847 小时前
解决phpstudy无法启动MySQL服务
数据库·mysql·adb
e***95647 小时前
【HTML+CSS】使用HTML与后端技术连接数据库
css·数据库·html