不能更改数据库中的数据
最近在学习.net中遇到一个关于事务处理例子。
代码如下()
protected void Button1_Click(object sender, EventArgs e) { string accountStrA = TextBox1.Text.ToString(); string accountStrB = TextBox2.Text.ToString(); float fundA = (float)Convert.ToSingle(Label1.Text) - (float)Convert.ToSingle(TextBox3.Text); float fundB = (float)Convert.ToSingle(Label2.Text) - (float)Convert.ToSingle(TextBox3.Text); SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); sqlConn.Open(); SqlTransaction myTran = sqlConn.BeginTransaction(); SqlCommand myCom = new SqlCommand(); myCom.Transaction = myTran; try { myCom.CommandText = "update yinhang set jine = '" + fundA + "'where zhanghu = '" + accountStrA + "'"; myCom.ExecuteNonQuery(); myCom.CommandText = "update yinhang set jine = '" + fundB + "'where zhanghu = '" + accountStrB + "'"; myCom.ExecuteNonQuery(); myTran.Commit(); Response.Write(""); } catch (Exception ex) { myTran.Rollback(); Response.Write(ex.ToString()); Response.Write(""); } finally { sqlConn.Close(); }
其中我运行Button1_Click后就显示转账错误并说明是myCom.ExecuteNonQuery();这里的错误
所以我注释了myCom.ExecuteNonQuery();这两行代码后能显示转账成功。但是数据中的数据却没有改变。。。。。纠结中,请求各位老鸟教导一下
3回答
-
神不在的星期二
myCom.CommandText = "update yinhang set jine = '" + fundA + "'where zhanghu = '" + accountStrA + "'";
myCom.CommandText+=";update yinhang set jine = '" + fundB + "'where zhanghu = '" + accountStrB + "'"myCom.ExecuteNonQuery();
-
慕无忌1623718
自己代码错误了,已经解决
-
幕布斯7119047
是不是myCom.ExecuteNonQuery();这个只需要写一次??