• C#中数据库事务、存储过程基本用法


    SQL 事务

     public bool UpdateQsRegisterSql(List<string> ids, int newQueueId, string newQueueName)
            {
                using (SqlConnection con = new SqlConnection(DBHelper.Instance.ConStr))
                {
                    SqlTransaction tran = null;
                    try
                    {
                        con.Open();
                        SqlCommand cmd = con.CreateCommand();
                        tran = con.BeginTransaction(IsolationLevel.ReadCommitted);
                        cmd.Transaction = tran;
    
                        foreach (string item in ids)
                        {
                            cmd.CommandText = @"update qs_register set queueid='" + newQueueId
                                + "',queuename='" + newQueueName + "' where id='" + item + "'";
                            cmd.ExecuteNonQuery();
                        }
    
                        tran.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }

    Oracle事务

      public bool UpdateQsRegisterOrcl(List<string> ids, int newQueueId, string newQueueName)
            {
                using (OleDbConnection con = new OleDbConnection(DBHelper.Instance.ConStr))
                {
                    OleDbTransaction tran = null;
                    try
                    {
                        con.Open();
                        OleDbCommand cmd = con.CreateCommand();
                        tran = con.BeginTransaction(IsolationLevel.ReadCommitted);
                        cmd.Transaction = tran;
    
                        foreach (string item in ids)
                        {
                            cmd.CommandText = @"update qs_register set queueid='" + newQueueId
                                + "',queuename='" + newQueueName + "' where id='" + item + "'";
                            cmd.ExecuteNonQuery();
                        }
    
                        tran.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }
    

    Sql存储过程

         /// <summary>
            /// 直接登记
            /// </summary>
            /// <param name="hisExamno">申请单号</param>
            /// <param name="enrolDoctor">登记医生</param>
            /// <param name="engname">患者英文名称</param>
            /// <param name="queueName">队列名称</param>
            /// <returns></returns>
            public bool DirectRegisterSql(string hisExamno, string enrolDoctor, string engname, string queueName)
            {
                using (SqlConnection conn = new SqlConnection(DBHelper.Instance.ConnectionStr))
                {
                    conn.Open();
                    SqlCommand cmd = conn.CreateCommand();
                    try
                    {
                        //插入号源属性
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "PROC_HISEXAMTOENROL";
                        cmd.Parameters.Add("@p_HISEXAMNO", SqlDbType.VarChar).Value = hisExamno;
                        cmd.Parameters.Add("@p_ENROLDOCTOR", SqlDbType.VarChar).Value = enrolDoctor;
                        cmd.Parameters.Add("@p_ENGNAME", SqlDbType.VarChar).Value = engname;
                        cmd.Parameters.Add("@p_AppKind", OleDbType.VarChar).Value = PubVariable.Instance.CurrentDept;
                        cmd.Parameters.Add("@p_QueueName", OleDbType.VarChar).Value = queueName;
                        cmd.Parameters.Add("@p_result", SqlDbType.Int).Value = -1;
    
                        if (cmd.ExecuteNonQuery() > 0) return true;
                    }
                    catch { return false; }
                    finally
                    {
                        if (conn.State != ConnectionState.Closed)
                        {
                            conn.Close();
                        }
                    }
                }
                return false;
            }
    

    Oracle存储过程

      /// <summary>
            /// 直接登记
            /// </summary>
            /// <param name="hisExamno">申请单号</param>
            /// <param name="enrolDoctor">登记医生</param>
            /// <param name="engname">患者英文名称</param>
            /// <param name="queueName">队列名称</param>
            /// <returns></returns>
            public bool DirectRegisterOrcl(string hisExamno, string enrolDoctor, string engname, string queueName)
            {
                using (OleDbConnection conn = new OleDbConnection(DBHelper.Instance.ConnectionStr))
                {
                    conn.Open();
                    OleDbCommand cmd = conn.CreateCommand();
                    try
                    {
                        //插入号源属性
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "PROC_HISEXAMTOENROL";
                        cmd.Parameters.Add("@p_HISEXAMNO", OleDbType.VarChar).Value = hisExamno;
                        cmd.Parameters.Add("@p_ENROLDOCTOR", OleDbType.VarChar).Value = enrolDoctor;
                        cmd.Parameters.Add("@p_ENGNAME", OleDbType.VarChar).Value = engname;
                        cmd.Parameters.Add("@p_AppKind", OleDbType.VarChar).Value = PubVariable.Instance.CurrentDept;
                        cmd.Parameters.Add("@p_QueueName", OleDbType.VarChar).Value = queueName;
                        cmd.Parameters.Add("@p_result", OleDbType.Integer).Value = -1;
    
                        if (cmd.ExecuteNonQuery() > 0) return true;
                    }
                    catch { return false; }
                    finally
                    {
                        if (conn.State != ConnectionState.Closed)
                        {
                            conn.Close();
                        }
                    }
                }
                return false;
            }
    

      

  • 相关阅读:
    【leetcode】1. Two Sum
    【leetcode】32. Longest Valid Parentheses
    【leetcode】20. Valid Parentheses
    scala细节
    【转】CentOS下firefox安装flash说明
    Spring 容器(一)
    源码学习快捷键
    Gradle编译spring3.x报错找不到itextpdf4.2.2解决方案
    Insertion Sort
    Bubble Sort
  • 原文地址:https://www.cnblogs.com/YYkun/p/9359513.html
Copyright © 2020-2023  润新知