事务回滚失败

时间:2018-03-05 03:24:24

标签: c# mysql

如果出现错误,我在尝试回滚事务时遇到问题。

我做的是:

1)插入表A

2)从表A中获取最后插入的ID

3)插入表B

以下是我的代码:

 public Int32 insertECN(RegisterECN modelRegister)
    {

        IDataAccess access = this.CreateAccess(true);
        // true in createAccess will create an transaction
        try
        {
            DAparam[] param = new DAparam[]
           {
                new DAparam("@loginDate", modelRegister.loginDate),
                new DAparam("@ectype", modelRegister.ecType),
                new DAparam("@ecRecDate", modelRegister.ecReceiveDate),
                new DAparam("@ecRef", modelRegister.ecReference),
                new DAparam("@changeDesc", modelRegister.changeDescription),
                new DAparam("@reasonOfChan", modelRegister.reasonOfChange),
                new DAparam("@reworkIns", modelRegister.reworkInstructionsSummary),
                new DAparam("@remark", modelRegister.remarks),
                new DAparam("@custName", modelRegister.customerName),
                new DAparam("@proName", modelRegister.projectName),
                new DAparam("@ecOwnerName", modelRegister.ownerName),
                new DAparam("@materialImpact", modelRegister.materialImpact)

            };
            access.ExecSQL(SQL.registerNewECNToDB, param);

            //registerNewECNToDB =   **INSERT INTO eccs_registerecn_tbl(LOGINDATE,ECTYPE,ECRECEIVEDATE,ECREFERENCE,CHANGEDESCRIPTION,
              REASONOFCHANGE,REWORKINSTRUCTIONSUMMARY,REMARKS,CUSTOMERNAME,PROJECTNAME,ECOWNERNAME,MATERIALIMPACT) VALUES(@loginDate, @ectype,
              @ecRecDate, @ecRef, @changeDesc, @reasonOfChan, @reworkIns, @remark, @custName, @proName, @ecOwnerName, @materialImpact)**



            DataTable result2 = new DataTable();
            result2 = access.GetDataSet(SQL.selectLastID).Tables[0].Copy();

            //selectLastID = SELECT LAST_INSERT_ID() AS 'ID'


            int lastID = 0;
            lastID = Convert.ToInt32(result2.Rows[0][0]);


            DAparam[] param1 = new DAparam[]
                        {
                             new DAparam("@file", content),
                             new DAparam("@moduleRef", "ECN"),
                             new DAparam("@refID", lastID),
                             new DAparam("@fileName", file.FileName),
                             new DAparam("@fileType", file.ContentType)

                        };

            access.ExecSQL(SQL.insertAttachment, param1);

            // insertAttachment = INSERT INTO eccs_attachments_tbl(FILE,MODULEREF,REFID,FILENAME,FILETYPE) VALUES(@file, @moduleRef,@refID,@fileName,@fileType)

            return lastID;
        }
        catch (Exception e)
        {
            access.Rollback();

            throw new Exception("Unable to create ECN.. Please contact IT..", e);
        }
        finally
        {
            if (access != null)
            {
                access.Dispose();
            }
        }


    }

如果在将数据插入表B时出现任何错误,我的表A事务无法回滚。

但是如果我尝试删除SELECT LAST ID查询,它就能够成功回滚。我可以知道为什么以及如何解决这个问题? 它是否不能在一个事务中包含SELECT和INSERT查询?

0 个答案:

没有答案