有关交易的问题 [英] A question regarding transaction

查看:63
本文介绍了有关交易的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我写下面的代码来删除所有符合条件的雇员,如下所示

Hi all i write the below code to delete the selected employees if all conditions are true as follows

if(max())
{
   if(seq()
   {
      b_rsltcnt=true;
   }
    else
    {
     if(dbseq()
     {
      b_rsltcnt=false;
     }
     else
     {
     }
}



如果我的条件正常,那么我将编写以下代码



If my condition works then i will write the below code

if (b_rsltcnt)
{
    foreach (int iEmpID1 in oAdmin.odictAddValues.Keys)
    {
       oAdmin.EmpID = iEmpID1;
       foreach (int iPayPeriodIDs1 in oAdmin.odictAddValues[iEmpID1].lstPayPeriodID)
       {
         lstdictPayPeriodID.Add(iPayPeriodIDs1);
       }
         foreach (DateTime dtPaymentYear1 in Admin.odictAddValues[iEmpID1].lstPaymentDate)
       {
          lstdictDateTime.Add(dtPaymentYear1);
       }
       foreach (int iPayYears1 in oAdmin.odictAddValues[iEmpID1].lstPayYear)
      { 
        lstdictPayYear.Add(iPayYears1);
      }
        for (int ipayID = 0; ipayID < lstdictPayPeriodID.Count; ipayID++)
       {
          oAdmin.Payperiodnumber = lstdictPayPeriodID[ipayID];
          for (int ipayDate = idtcnt; ipayDate < lstdictDateTime.Count; ipayDate++)
          {
            oAdmin.PaymentDate = lstdictDateTime[ipayDate];
            idtcnt++;
            break;
          }
           for (int ipayYear1 = iPayYearcnt; ipayYear1 < lstdictPayYear.Count; ipayYear1++)
           {
              oAdmin.PayYear = lstdictPayYear[ipayYear1];
              iPayYearcnt++;
              break;
            }
            mlocal_strStoredProcName = "usp_delete_payroll";
            if (oAdmin.deletePayRoll(mlocal_strStoredProcName))
            {
              oMsg.Message = "Deleted Sucessfully";
              oMsg.AlertMessageBox(out m_locallblMessage);
              Page.Controls.Add(m_locallblMessage);
              oAdmin.FedTaxID = ddlFedTaxID.SelectedValue;
              oAdmin.PayFrequency = ddlPaymentType.SelectedValue.ToString();
              mlocal_strStoredProcName = "uspSearchPayRoll";
              oAdmin.getPayRollDetails(out mlocal_ds, mlocal_strStoredProcName);
             //grdPayroll.Visible = true;
             grdPayroll.DataSource = mlocal_ds;
             grdPayroll.DataBind();
            if (mlocal_ds != null)
            {
              btnDelete.Visible = true;
            }
            else
              btnDelete.Visible = false;
           }
        }
     }
 }



我的班级文件包含以下删除代码



My class file consists of delete code as follows

<pre lang="c#">public bool deletePayRoll(string storeproc)
        {
            m_bFlag = false;
            this.m_oConn = Utilities.GetConnection();
            try
            {
                if (m_oConn.State != ConnectionState.Open)
                {
                    m_oConn.Open();
                    m_oSqlTrans = m_oConn.BeginTransaction();

                }
                m_oCmd = new MySqlCommand(storeproc, m_oConn);
                m_oCmd.CommandType = CommandType.StoredProcedure;
                m_oCmd.Transaction = m_oSqlTrans;
                m_oCmd.Parameters.AddWithValue("_EmpId", EmpID);
                m_oCmd.Parameters.AddWithValue("_FedTaxID", FedTaxID);
                m_oCmd.Parameters.AddWithValue("_payperiodnumber", Payperiodnumber);
                m_oCmd.Parameters.AddWithValue("_payyear", PayYear);
                m_oCmd.Parameters.AddWithValue("_paymentdate", PaymentDate);

                if (m_oCmd.ExecuteNonQuery() > 0)
                {
                    m_oSqlTrans.Commit();
                    m_bFlag = true;
                }
            }
            catch (MySqlException oSqlEx)
            {
                m_oSqlTrans.Rollback();      
                m_sbErrMsg.Length = 0;
                m_sbErrMsg = Utilities.SqlErrorMessage(oSqlEx);
                //DB write the Error Log
                m_oErrlog.Add(m_sbErrMsg.ToString(), DateTime.Now);
            }
            catch (Exception oEx)
            {

                m_sbErrMsg = Utilities.ErrorMessage(oEx);
                //DB write the Error Log
                m_oErrlog.Add(m_sbErrMsg.ToString(), DateTime.Now);
            }

            finally
            {
                m_oConn.Close();
            }
            return m_bFlag;
        }



假设我有5条记录要删除,并且我的所有书面条件始终为真.假设在5条记录中成功删除了4条记录,而在第5条记录中,我遇到了一些连接问题或其他问题,并假设异常导致所有先前删除的记录都将被回滚



Assume that i have 5 records to delete and all my conditions as per written are always true. For suppose among 5 records 4 deleted successfully and while coming to 5th one i had some connection issues or some other issues and assume that an Exception causes will all the previously deleted records will be rollbacked or not

推荐答案

很难弄清大多数应用程序中正在发生的事情,但是从本质上讲,答案是否".调用Commit后,所做的更改将不可撤销,即使您稍后尝试回滚同一事务.
在每次成功更改数据库后,您似乎都将提交,从而一开始就取消了事务的使用.
如果要在发生任何故障时回滚所有更改,请在进行任何更改之前开始事务,在第一个故障回滚并退出,或者在所有事务完成后提交.
It is difficult to work out what is going on in most of your application, but in essence, the answer is "No". As soon as you call Commit, the changes are irrevocably made, even if you try to roll back on the same transaction later.
You appear to commit after every successful change to your database, which negates the use of transactions in the first place.
If you want to roll back all changes on any failure, then begin your transaction before you do any changes, rollback on the first failure and exit, or commit when all transactions are complete.


这篇关于有关交易的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆