• T - SQL使用事务 及 在Winform使用事务


    事务适用场景

            1 事务使用在存储过程中,直接在数据库中进行编写
            2 事务使用在Winfrom项目中

    SQl:使用事务转账操作的实例

    一般都会找一个变量记录错误的个数,@error记录上一句sql的错误和错误编号

    1. declare @errornum int = 0 -- 定义一个记录错误个数的变量
    2. begin transaction -- 开启事务
    3. begin
    4. -- 转出操作
    5. update Card set CurrentMoney = CurrentMoney - 100000 where StudentId = 1002
    6. set @errornum = @errornum + @@error -- 把@@error加到总的记录上
    7. -- 转入操作
    8. update Card set CurrentMoney = CurrentMoney + 100000 where StudentId = 1004
    9. set @errornum = @errornum + @@ERROR
    10. if(@errornum > 0) -- 证明有错误,把之前已经执行的操作回滚到原先的数据上
    11. rollback transaction -- 回滚事务
    12. else -- 没有错误,提交事务
    13. commit transaction
    14. end
    15. go
    16. select * from Card

    声明一个过程 包含三个输入参数 分别是两个转账的卡号 和转账的金额

    1. use YinHangDB
    2. go
    3. create procedure tt10000
    4. @outZhangHao int,
    5. @ruZhangHao int,
    6. @Money int
    7. as
    8. declare @c1 int = 0--错误的个数
    9. begin transaction --开启事务
    10. begin
    11. --转出
    12. update Card set CurrentMoney = CurrentMoney - @Money where StudentId = @outZhangHao
    13. set @c1 = @c1 + @@ERROR
    14. --转入
    15. update Card set CurrentMoney = CurrentMoney + @Money where StudentId = @ruZhangHao
    16. set @c1 = @c1 + @@ERROR
    17. if(@c1>0)
    18. rollback transaction
    19. else
    20. commit transaction
    21. end
    22. go
    23. select * from Card
    24. --测试转账失败的操作
    25. exec tt10000 1000,1001,100000
    26. --测试转账成功
    27. exec tt10000 1000,1001,50000

    Winfrom 使用事故实例

    链接SQl 数据库

    public string connString = @"Server=.;Database=SMDB;Uid=sa;Pwd=123456";

    搭建Winfrom界面

     测试事务回滚的方法

    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3. //删除前学生总个数
    4. int num1 = (int)GetSingleCount("select count(*) from Students");
    5. this.listBox1.Items.Add("删除前学生总个数:"+ num1); // 展示在listbox下
    6. // 测试删除学生失败进行事务回滚操作
    7. //定义一个列表字符串
    8. List<string> list = new List<string>()
    9. {
    10. "delete from Students where StudentId = 1000005",
    11. "delete from Students where StudentId = 1000024",
    12. "delete from Students where StudentId = 1000006"
    13. };
    14. //执行删除操作 把list里面每一句sql都去执行一下
    15. int result = 0;
    16. try
    17. {
    18. result = UpdateDataBase(list);
    19. }
    20. catch (Exception ex)
    21. {
    22. this.listBox1.Items.Add(ex.Message);
    23. }
    24. if (result > 0)
    25. {
    26. this.listBox1.Items.Add("删除成功");
    27. }
    28. else
    29. {
    30. this.listBox1.Items.Add("删除失败");
    31. }
    32. this.listBox1.Items.Add("删除之后剩余人数为:" + (int)GetSingleCount("select count(*) from Students"));
    33. }

    封装多个删除语句执行操作
    参数是多个sql的集合
    返回值 int是否执行成功

    1. public int UpdateDataBase(List<string> list)
    2. {
    3. SqlConnection conn = new SqlConnection(connString);
    4. // new SqlCommand(sql, conn)
    5. // 没有指定执行sql 没有指定连接,下面必须在写代码把连接对象添加上
    6. SqlCommand cmd = new SqlCommand();
    7. cmd.Connection = conn;
    8. try
    9. {
    10. conn.Open();
    11. //开启事务
    12. cmd.Transaction = conn.BeginTransaction();
    13. int result = 0;//记录删除时候受影响的行数
    14. //取出每一个sql语句 分别执行
    15. foreach (string sql in list)
    16. {
    17. //设置执行的sql
    18. cmd.CommandText = sql;
    19. result += cmd.ExecuteNonQuery();
    20. // cmd.ExecuteNonQuery() 返回受影响行数如果删除成功了,受影响行数不为0
    21. }
    22. //如果执行错误了 跳转到catch里面 在catch执行回滚
    23. // 没有出错 提交事务
    24. cmd.Transaction.Commit();
    25. return result;
    26. }
    27. catch(Exception ex)
    28. {
    29. if (cmd.Transaction != null)
    30. {
    31. cmd.Transaction.Rollback(); // 执行sql语句有错误的情况 执行回滚操作
    32. }
    33. throw new Exception("执行删除sql事务出错:" + ex.Message);
    34. }
    35. finally
    36. {
    37. // 不管是否执行有误 把事务置为null 清除事务
    38. if(cmd.Transaction!=null)
    39. {
    40. cmd.Transaction = null;
    41. }
    42. conn.Close();
    43. }
    44. }

    获取数据库个数的方法

     public object GetSingleCount(string sql)
     {
         SqlConnection conn = new SqlConnection(connString);
         SqlCommand cmd = new SqlCommand(sql, conn);
         conn.Open();
         return cmd.ExecuteScalar();

     }

    测试事务提交的方法 

    1. private void button2_Click(object sender, EventArgs e)
    2. {
    3. //删除前学生总个数
    4. int num1 = (int)GetSingleCount("select count(*) from Students");
    5. this.listBox1.Items.Add("删除前学生总个数:" + num1); // 展示在listbox下
    6. // 测试删除学生失败进行事务回滚操作
    7. //定义一个列表字符串
    8. List<string> list = new List<string>()
    9. {
    10. "delete from Students where StudentId = 1000033",
    11. "delete from Students where StudentId = 1000031",
    12. "delete from Students where StudentId = 1000032"
    13. };
    14. //执行删除操作 把list里面每一句sql都去执行一下
    15. int result = 0;
    16. try
    17. {
    18. result = UpdateDataBase(list);
    19. }
    20. catch (Exception ex)
    21. {
    22. this.listBox1.Items.Add(ex.Message);
    23. }
    24. if (result > 0)
    25. {
    26. this.listBox1.Items.Add("删除成功");
    27. }
    28. else
    29. {
    30. this.listBox1.Items.Add("删除失败");
    31. }
    32. this.listBox1.Items.Add("删除之后剩余人数为:" + (int)GetSingleCount("select count(*) from Students"));
    33. }

  • 相关阅读:
    使用.NET Jieba.NET 的 PosSegmenter 实现中文分词匹配
    java继承简介说明
    简单爬虫案例——爬取快手视频
    龙芯3A3000源码编译安装deepin-ide
    Springboot考研教室管理系统 毕业设计-附源码221757
    Macos 远程登录 Ubuntu22.04 桌面
    01 介绍
    跨领域个性化迁移用户兴趣偏好
    2.2.1操作系统之处理机调度的概念及层次
    塔望3W消费战略全案丨绿力冬瓜茶 三十年饮料老品牌,两年复兴战全国
  • 原文地址:https://blog.csdn.net/lu2023_8_6/article/details/136353968