oracle 通用事务使用

private void dothing()
        {

            OracleConnection con = DBHelperOracle.init();
             
            OracleTransaction tran = con.BeginTransaction();

            try
            {

                DBHelperOracle.ExecuteNonQuery(tran, CommandType.Text, @"
update  xxxxxx",
                                                                                                                null);
                DataTable dt = DBHelperOracle.ExecuteTable(tran, CommandType.Text, @"selectxxxxxx", null);

                DBHelperOracle.ExecuteNonQuery(tran, CommandType.Text, @"
update t0070_enterprise set C0047_SECRET_CODE='1' where c0070_enterprise_code ='370682006' and  c0003_year=2006",
                                                                                                              null);
                dt = DBHelperOracle.ExecuteTable(tran, CommandType.Text, @"select a.C0047_SECRET_CODE from t0070_enterprise a where a.c0070_enterprise_code ='370682006' and  a.c0003_year=2006", null);


                
                tran.Commit();

            }

            catch
            {

                tran.Rollback();

            }

            finally
            {

                if (con.State == ConnectionState.Open)
                {
                    con.Close();

                }
            }
        }

         public static OracleConnection init()
        {
            try
            {
                conn = new OracleConnection(connstr);
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }
            return conn;
        }


         public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] CommandParams)
        {

            OracleCommand cmd = new OracleCommand();
            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
            /*if (cmdType == CommandType.StoredProcedure)
            {
            cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;
            cmd.ExecuteNonQuery();

            val = (int)cmd.Parameters["@RETURN_VALUE"].Value;
            }
            else
            if (cmdType==CommandType.Text)
            {
            val = cmd.ExecuteNonQuery();
            }*/
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();

            return val;
        }

        public static DataTable ExecuteTable(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] CommandParams)
        {
            DataTable temptable = new DataTable();
            OracleCommand cmd = new OracleCommand();

            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
            OracleDataAdapter da = new OracleDataAdapter(cmd);
            OracleCommandBuilder scb = new OracleCommandBuilder(da);
            da.Fill(temptable);
            cmd.Parameters.Clear();

            return temptable;

        }

        private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] cmdParms)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (OracleParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

  

posted @ 2019-12-04 10:36  gds111789  阅读(402)  评论(0编辑  收藏  举报