ITPub博客

首页 > > > 不能更改数据库中的数据

不能更改数据库中的数据

作者:lilyone 时间:2019-10-24 10:04:18 0 删除 编辑

最近在学习.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();这两行代码后能显示转账成功。但是数据中的数据却没有改变。。。。。纠结中,请求各位老鸟教导一下

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4635/viewspace-3155614/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-01

  • 博文量
    159
  • 访问量
    974