它总是去OleDbException

时间:2015-06-20 15:44:52

标签: mysql rollback oledbcommand executenonquery oledbexception

 using(OleDbConnection con = DAL.GetConnection())
        {
            OleDbTransaction transaction = null;
            try
            {
                con.Open();
                transaction = con.BeginTransaction();

                if (realQuantity == quantity)
                {
                    sql = "DELETE FROM item WHERE (id =" + id + ")";
                }
                else if (realQuantity > quantity)
                {
                    sql = "UPDATE item SET quantity = " + finalQuantity + " WHERE (id = "+id+")";
                }
                OleDbCommand cmd = DAL.GetCommand(con, sql);
                cmd.Transaction = transaction;

                string sql2 = "UPDATE lol SET money = " + finalMoneyBuyer + " WHERE (UserName = '" + Session["username"] + "')";
                OleDbCommand cmd2 = DAL.GetCommand(con, sql2);
                cmd2.Transaction = transaction;

                string sql3 = "UPDATE lol SET money = " + finalMoneySeller + " WHERE (UserName = '" + seller + "')";
                OleDbCommand cmd3 = DAL.GetCommand(con, sql3);
                cmd3.Transaction = transaction;

                int num1 = cmd.ExecuteNonQuery();
                int num2 = cmd2.ExecuteNonQuery();
                int num3 = cmd3.ExecuteNonQuery();

                if(num1 == 0 || num2 == 0 || num3 == 0)
                {
                    //No esperamos a que sea 0, asi que vamos a echar para atras todo lo que hicimos
                    transaction.Rollback();
                    //mandar error
                    Response.Redirect("home.aspx?err=Error1");
                }
                else
                {
                    transaction.Commit();
                    Response.Redirect("home.aspx?err=Purchase was successful!");
                }
            }
            catch(OleDbException ex)
            {
                try
                {
                    //algo malo paso, vamos a echar para atras todo lo que hicimos.
                    transaction.Rollback();
                    Response.Redirect("home.aspx?err=Error2");
                }
                catch{}
            }
        }

我正在展示必要的代码,知道如何计算finalMoneyBuyerfinalQuantity等变量并不重要。当我查看此代码时,它会在int num2 = cmd2.ExecuteNonQuery();部分出错。最后,它转到Response.Redirect("home.aspx?err=Error2");。我希望它提交整个事务,但事实并非如此。

我想有可能我没有写好数据库中列的名称,或SQL查询中的错误,所以我试图展示我能做的最多的东西,所以你很容易找出问题所在。 谢谢!

在Microsoft Visual Studio 2013中运行WebSite后sql2的SQL查询:

sql2

我有一个lol表,一个名为money的列,一个名为UserName的列......

lol table

0 个答案:

没有答案