一个简单的存储过程(实现事务执行两个sql语句,修改可实现执行更多条sql语句)

 //实现数据库事务
        public static void ExecuteSqlTran(string SQLString1, string SQLString2)
        {
            using (OracleConnection conn = new OracleConnection(_DBConnectionString))
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                OracleTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    cmd.CommandText = SQLString1;
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = SQLString2;
                    cmd.ExecuteNonQuery();
                    tx.Commit();
                }
                catch (System.Data.OracleClient.OracleException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
            }
        }

posted on 2014-03-24 15:35  梦回大唐吟诗篇  阅读(611)  评论(0编辑  收藏  举报