• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#Transaction事务处理

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

class

 1 //student
 2  [Serializable]
 3     public class Student
 4     {
 5         public string FirstName { get; set; }
 6         public string LastName { get; set; }
 7         public string Company { get; set; }
 8         public int Id { get; set; }
 9 
10         public override string ToString()
11         {
12             return String.Format("{0} {1}", FirstName, LastName);
13         }
14     }
15 
16 //student 处理类
17  public async Task AddStudentAsync(Student student, Transaction tx)
18         {
19             SqlConnection connection = new SqlConnection(Settings.Default.CourseConnection);
20             await connection.OpenAsync();
21             try
22             {
23                 if (tx != null)
24                     connection.EnlistTransaction(tx);
25                 SqlCommand command = connection.CreateCommand();
26                 command.CommandText = "INSERT INTO Students (FirstName, LastName, Company) " +
27                "VALUES (@FirstName, @LastName, @Company)";
28                 command.Parameters.AddWithValue("@FirstName", student.FirstName);
29                 command.Parameters.AddWithValue("@LastName", student.LastName);
30                 command.Parameters.AddWithValue("@Company", student.Company);
31                 await command.ExecuteNonQueryAsync();
32             }
33             catch (Exception ex)
34             {
35                 Trace.WriteLine("AddStudentAsync(Student student, Transaction tx) Error :" + ex.Message);
36                 throw;
37             }
38             finally
39             { connection.Close(); }
40         }
41 
42 //操作类
43   public static class Utilities
44     {
45        public static bool AbortTx()
46        {
47            Console.Write("Abort the Transaction (y/n)?");
48            return Console.ReadLine().ToLower().Equals("y");
49        }
50        public static void DisplayTransactionInformation(string title, TransactionInformation ti)
51        {
52            Contract.Requires<ArgumentNullException>(ti != null);
53 
54            Console.WriteLine(title);
55            Console.WriteLine("Creation Time: {0:T}", ti.CreationTime);
56            Console.WriteLine("Status: {0}", ti.Status);
57            Console.WriteLine("Local ID: {0}", ti.LocalIdentifier);
58            Console.WriteLine("Distributed ID: {0}", ti.DistributedIdentifier);
59            Console.WriteLine();
60        }
61     }
View Code

 

执行类

 1   static void Main(string[] args)
 2         {
 3            Task t= CommittableTransactionAsync();
 4            t.Wait();
 5         }
 6         static async Task CommittableTransactionAsync()
 7         {
 8             var tx = new CommittableTransaction();
 9             Utilities.DisplayTransactionInformation("TX created", tx.TransactionInformation);
10 
11             try
12             {
13                 var s1 = new Student() { FirstName = "Stephanie", LastName = "Nage1", Company = "China" };
14                 var db = new StudentData();
15                 await db.AddStudentAsync(s1,tx);
16 
17                 var s2 = new Student() { FirstName = "Stephanie2", LastName = "Nage2", Company = "China" };
18                 await db.AddStudentAsync(s2, tx);
19 
20                 Utilities.DisplayTransactionInformation("2nd connection enlisted ", tx.TransactionInformation);
21 
22                 if(Utilities.AbortTx())
23                 {
24                     throw new ApplicationException("transaction abort");
25                 }
26                 tx.Commit();
27             }
28             catch (Exception ex)
29             {
30                 Console.WriteLine(ex.Message);
31                 Console.WriteLine();
32                 tx.Rollback();
33             }
34             Utilities.DisplayTransactionInformation("TX completed", tx.TransactionInformation);
35         }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++中的布局new操作符发布时间:2022-07-13
下一篇:
Asp.net(C#)-显示所有缓存清除所有缓存发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap