zoukankan      html  css  js  c++  java
  • Ado.Net事物处理

    protected void Button2_Click(object sender, EventArgs e)

        {
            SqlConnection con = new SqlConnection(@"Data Source=.SQLEXPRESS;database=aaaa;uid=sa;pwd=jcx");
            con.Open();
            //先实例SqlTransaction类,使用这个事务使用的是con 这个连接,
            //使用BeginTransaction这个方法来开始执行这个事务
            SqlTransaction tran = con.BeginTransaction(); 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.Transaction = tran;
            try
            {
                 //在try{} 块里执行sqlcommand命令,
                cmd.CommandText = "update bb set moneys=moneys-'" + Convert.ToInt32(TextBox1.Text) + "' where ID='1'";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "update bb set moneys=moneys+' aa ' where ID='2'";
                cmd.ExecuteNonQuery();
                tran.Commit();//如果两个sql命令都执行成功,则执行commit这个方法,执行这些操作
                 Label1.Text = "添加成功";
            }
            catch
            {
                Label1.Text = "添加失败";
                tran.Rollback();//如何执行不成功,发生异常,则执行rollback方法,回滚到事务操作开始之前;
            }
     
        }
  • 相关阅读:
    pandas isin 和not in
    游戏开发需要学什么?
    打开页面,数字会自增的效果怎么弄?
    jq 导航栏点击添加/删除类(a标签跳转页面)
    bootstrap+jq分页
    2020/12/18
    2020/12/17
    2020/12/16
    2020/12/15
    2020/12/14
  • 原文地址:https://www.cnblogs.com/jf-guo/p/6058666.html
Copyright © 2011-2022 走看看