c#实现sqlserver事务处理示例

时间:2022-03-28 19:39:50 


private static void ExecuteSqlTransaction(string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlCommand command = connection.CreateCommand();
            SqlTransaction transaction;
            // Start a local transaction.
            transaction = connection.BeginTransaction("SampleTransaction");
            // Must assign both transaction object and connection
            // to Command object for a pending local transaction
            command.Connection = connection;
            command.Transaction = transaction;
            try
            {
                command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
                command.ExecuteNonQuery();
                command.CommandText =  "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
                command.ExecuteNonQuery();
                // Attempt to commit the transaction.
                transaction.Commit();
                Console.WriteLine("Both records are written to database.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
                Console.WriteLine("  Message: {0}", ex.Message);
                // Attempt to roll back the transaction.
                try
                {
                    transaction.Rollback();
                }
                catch (Exception ex2)
                {
                    // This catch block will handle any errors that may have occurred
                    // on the server that would cause the rollback to fail, such as
                    // a closed connection.
                    Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                    Console.WriteLine("  Message: {0}", ex2.Message);
                }
            }
        }
    }

标签:sqlserver事务
0
投稿

猜你喜欢

  • Java五种方式实现多线程循环打印问题

    2023-03-07 20:34:12
  • SpringBoot路径映射实现过程图解

    2023-11-13 04:01:11
  • Java中List常用操作比for循环更优雅的写法示例

    2023-08-08 23:25:14
  • C# 创建高精度定时器的示例

    2023-03-09 12:35:58
  • Spring Cloud 整合Apache-SkyWalking实现链路跟踪的方法

    2023-04-05 09:17:52
  • Java常见问题之javac Hello.java找不到文件的解决方法

    2023-12-15 21:19:45
  • Android消息机制Handler用法总结

    2022-08-10 23:58:56
  • java客户端登陆服务器用户名验证

    2023-11-09 07:03:09
  • Java中joda日期格式化工具的使用示例

    2023-03-01 11:49:10
  • Java实现红黑树(平衡二叉树)的详细过程

    2021-08-08 15:03:12
  • Java SpringMVC异步处理详解

    2021-08-10 15:03:58
  • Springboot打成war包并在tomcat中运行的部署方法

    2022-06-29 07:53:32
  • 浅析Spring Boot中的spring-boot-load模块

    2023-11-23 02:39:31
  • C#对象为Null模式(Null Object Pattern)实例教程

    2023-07-27 15:53:28
  • 解决线程池中ThreadGroup的坑

    2023-08-24 00:13:47
  • SpringTask实现定时任务方法讲解

    2022-06-11 11:43:42
  • java swing 创建一个简单的QQ界面教程

    2022-09-08 06:51:39
  • 解决spring boot启动扫描不到自定义注解的问题

    2023-10-29 14:31:48
  • Java常量池详解

    2023-05-19 19:36:27
  • springmvc如何使用POJO作为参数

    2021-06-02 00:29:46
  • asp之家 软件编程 m.aspxhome.com