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 tran = con.BeginTransaction();//先实例SqlTransaction类,使用这个事务使用的是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方法,回滚到事务操作开始之前;
            }
     
        }

  • 相关阅读:
    Go jaegerde 应用【logger+gorm+grpc+http】
    Go gRPC 调试工具
    iris和xxl-job整合
    Go Grpc部署到 k8s【端口共享】
    rocketmq事务 go 采用rocketmq-client-go的实现
    Go Grpc部署到 k8s【负载均衡】
    ubuntu18安装Kubernetes 1.20.5
    k8s Python API
    go nacos服务发现
    k8s集群日志收集ELK和graylog
  • 原文地址:https://www.cnblogs.com/lh123/p/3872573.html
Copyright © 2011-2022 走看看