Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 它总是成为例外_Mysql_Rollback_Oledbcommand_Executenonquery_Oledbexception - Fatal编程技术网

Mysql 它总是成为例外

Mysql 它总是成为例外,mysql,rollback,oledbcommand,executenonquery,oledbexception,Mysql,Rollback,Oledbcommand,Executenonquery,Oledbexception,我正在展示必要的代码,知道如何计算变量(如finalMoneyBuyer或finalQuantity)并不重要。当我阅读这段代码时,它在int num2=cmd2.ExecuteNonQuery()部分出错。最后,它转到Response.Redirect(“home.aspx?err=Error2”)。我希望它提交整个事务,而不是 我想可能是因为我没有在数据库中很好地写下列的名称,或者是因为SQL查询中出现了错误,所以我试图尽可能多地展示一些东西,以便您很容易识别问题谢谢 在Microsoft

我正在展示必要的代码,知道如何计算变量(如
finalMoneyBuyer
finalQuantity
)并不重要。当我阅读这段代码时,它在
int num2=cmd2.ExecuteNonQuery()部分出错。最后,它转到
Response.Redirect(“home.aspx?err=Error2”)。我希望它提交整个事务,而不是

我想可能是因为我没有在数据库中很好地写下列的名称,或者是因为SQL查询中出现了错误,所以我试图尽可能多地展示一些东西,以便您很容易识别问题谢谢

在Microsoft Visual Studio 2013中运行网站后对sql2的SQL查询:

我有一个lol表,一个叫做money的列,一个叫做UserName的列


您得到的确切错误消息是什么?我使用了
try
,因此它没有准确显示错误。如何显示它?请尝试调试并在catch块中查看您收到的异常消息。在调试时,发布
sql2
的输出字符串是什么?
 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{}
            }
        }