none
EnterpriseLibrary的事务 RRS feed

  • 问题

  • EnterpriseLibrary操作Oracle数据库,有以下代码,如果第2个SQL出错,回滚不了,请问是什么原因?

    StringBuilder m_sqlInsert1 = new StringBuilder();
                StringBuilder m_sqlInsert2 = new StringBuilder();
                DbConnection m_con = null;
                DbTransaction m_trans = null;
                DbCommand m_com = null;
                Int32 m_intResult = 0;

                m_sqlInsert1.Append("INSERT INTO User_Member");
                m_sqlInsert1.Append("(LoginName,UserName,Sex,Creater,ParentDepID,DepartID,Role,CSSFileName,Remarks)");
                m_sqlInsert1.Append("VALUES(");
                m_sqlInsert1.Append("'").Append(p_clsUser.LoginName).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.UserName).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.Sex).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.Creater).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.ParentDepID).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.DepartID).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.Role).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.CSSFileName).Append("',");
                m_sqlInsert1.Append("'").Append(p_clsUser.Remarks).Append("')");

                m_sqlInsert2.Append("INSERT INTO User_Password");
                m_sqlInsert2.Append("(LoginName,Password,ISUse)");
                m_sqlInsert2.Append("VALUES(");
                m_sqlInsert2.Append("'").Append(p_clsUser.LoginName).Append("',");
                m_sqlInsert2.Append("'").Append(EncryptMd5("111111")).Append("',");
                m_sqlInsert2.Append("'1')");

                try
                {
                    m_con = g_db.CreateConnection();
                    m_con.Open();
                    m_trans = m_con.BeginTransaction();
                    m_com = g_db.GetSqlStringCommand(m_sqlInsert1.ToString());
                    m_intResult = g_db.ExecuteNonQuery(m_com);

                    m_com = g_db.GetSqlStringCommand(m_sqlInsert2.ToString());
                    m_intResult += g_db.ExecuteNonQuery(m_com);

                    if (m_intResult == 2)
                    {
                        m_intResult = 1;
                        m_trans.Commit();
                    }
                    else
                    {
                        m_intResult = 0;
                        m_trans.Rollback();
                    }
                }
                catch (Exception ex)
                {
                    m_trans.Rollback();
                    throw ex;
                }
                finally
                {
                    m_con.Close();
                    m_com.Dispose();
                }
    2008年8月21日 5:28

全部回复