C# Transaction 事务处理
class
//student
[Serializable]
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public int Id { get; set; } public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
}
} //student 处理类
public async Task AddStudentAsync(Student student, Transaction tx)
{
SqlConnection connection = new SqlConnection(Settings.Default.CourseConnection);
await connection.OpenAsync();
try
{
if (tx != null)
connection.EnlistTransaction(tx);
SqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO Students (FirstName, LastName, Company) " +
"VALUES (@FirstName, @LastName, @Company)";
command.Parameters.AddWithValue("@FirstName", student.FirstName);
command.Parameters.AddWithValue("@LastName", student.LastName);
command.Parameters.AddWithValue("@Company", student.Company);
await command.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
Trace.WriteLine("AddStudentAsync(Student student, Transaction tx) Error :" + ex.Message);
throw;
}
finally
{ connection.Close(); }
} //操作类
public static class Utilities
{
public static bool AbortTx()
{
Console.Write("Abort the Transaction (y/n)?");
return Console.ReadLine().ToLower().Equals("y");
}
public static void DisplayTransactionInformation(string title, TransactionInformation ti)
{
Contract.Requires<ArgumentNullException>(ti != null); Console.WriteLine(title);
Console.WriteLine("Creation Time: {0:T}", ti.CreationTime);
Console.WriteLine("Status: {0}", ti.Status);
Console.WriteLine("Local ID: {0}", ti.LocalIdentifier);
Console.WriteLine("Distributed ID: {0}", ti.DistributedIdentifier);
Console.WriteLine();
}
}
执行类
static void Main(string[] args)
{
Task t= CommittableTransactionAsync();
t.Wait();
}
static async Task CommittableTransactionAsync()
{
var tx = new CommittableTransaction();
Utilities.DisplayTransactionInformation("TX created", tx.TransactionInformation); try
{
var s1 = new Student() { FirstName = "Stephanie", LastName = "Nage1", Company = "China" };
var db = new StudentData();
await db.AddStudentAsync(s1,tx); var s2 = new Student() { FirstName = "Stephanie2", LastName = "Nage2", Company = "China" };
await db.AddStudentAsync(s2, tx); Utilities.DisplayTransactionInformation("2nd connection enlisted ", tx.TransactionInformation); if(Utilities.AbortTx())
{
throw new ApplicationException("transaction abort");
}
tx.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine();
tx.Rollback();
}
Utilities.DisplayTransactionInformation("TX completed", tx.TransactionInformation);
}
C# Transaction 事务处理的更多相关文章
- C# Transaction 事务处理 -依赖事务
在DependentTransaction()方法中,实例化CommittableTransaction类,创建一个根事务,显示事务的信息.接着, tx.DependentClone()方法创建一个依 ...
- C# Transaction 事务处理 -环境事务
一.TransactionScope 环境事务 static async Task TransactionScopeAsync() { using (var scope = new Transacti ...
- word20161222
T.120 tag / 标记 TAPI, Telephony API / 电话 API target journaling / 目标日志 taskbar / 任务栏 taskbar button / ...
- SQL标签
SQL标签库提供了与关系型数据库进行交互的标签. 引入语法:<%@ taglib prefix="sql" uri="http://java.sun.com/jsp ...
- (火炬)MS SQL Server数据库案例教程
(火炬)MS SQL Server数据库案例教程 创建数据库: CREATE DATABASE TDB //数据库名称 ON ( NAME=TDB_dat,//逻辑文件名 在创建数据库完成之后语句中引 ...
- MySQL (八)
1 事务 需求:有一张银行账户表,A用户给B用户转账,A账户先减少,B账户增加,但是A操作完之后断电了. 解决方案:A减少钱,但是不要立即修改数据表,B收到钱之后,同时修改数据表. 事务:一系列要发生 ...
- MySQL (八)-- 事务、变量、触发器
1 事务 需求:有一张银行账户表,A用户给B用户转账,A账户先减少,B账户增加,但是A操作完之后断电了. 解决方案:A减少钱,但是不要立即修改数据表,B收到钱之后,同时修改数据表. 事务:一系列要发生 ...
- MySQL完整教程(共8章)
正文 [第一章] 回到顶部 1.1 MySQL学习路线 基础阶段:MySQL数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引, ...
- 物联网架构成长之路(17)-SpringCloud目前遇到的注意事项
1. STS插件最好是要安装的. 2. 对应的Decompiler插件也是要安装的. 3. 如果遇到maven工程因为找不到包问题的, 在确认pom.xml 文件没有问题的情况下, 右键项目-Mave ...
随机推荐
- 路由器04--OPKG
1.简介 https://oldwiki.archive.openwrt.org/doc/techref/opkg Opkg 是一个基于 ipkg 的轻量级的软件包管理系统,主要用于嵌入式系统,目前应 ...
- 斜率优化dp学习笔记 洛谷P3915[HNOI2008]玩具装箱toy
本文为原创??? 作者写这篇文章的时候刚刚初一毕业…… 如有错误请各位大佬指正 从例题入手 洛谷P3915[HNOI2008]玩具装箱toy Step0:读题 Q:暴力? 如果您学习过dp 不难推出d ...
- git使用中的一些命令及心得
Git 与 SVN 区别点: 1.Git 是分布式的,SVN 不是:这是 Git 和其它非分布式的版本控制系统,例如 SVN,CVS 等,最核心 的区别. 2.Git 把内容按元数据方式存储,而 SV ...
- python-pillow图像处理模块
from PIL import ImageColor ImageColor.getcolor('red','RGB') #颜色 模式 ImageColor.getcolor('red','RGBA') ...
- Python之正则表达式笔记
概述 概念 Regular Expression 一种文本模式,描述在搜索文本时要匹配的一个或多个字符串 典型场景 数据验证.文本扫描.文本提取.文本替换.文本分割 语法 字面值 普通字符 需转义:\ ...
- MySQL部分2
- 使用Duilib开发Windows软件(3)——控件的样式
摘抄下 https://www.cnblogs.com/Alberl/p/3344936.html 的一段代码 <?xml version="1.0" encoding=&q ...
- Asp.net core 学习笔记 2.2 migration to 3.0
Ef core 3.0 一些要注意的改变 refer : https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaki ...
- Apache2.4+Tomcat7.0+php5.5整合配置详解
在上一篇的基础上,继续添加php的配置 一.首先下载php5.5 首先下载php5.5,到官网下载http://www.php.net/downloads.php,参考http://www.cnblo ...
- 根据xsd文件生成对应的C#类,然后创建对应的xml文件
首先用xsd文件生产对应的C#类,这个VS已经自带此工单,方法如下: 1. 打开交叉命令行工具 2. 输入如下指令 xsd d:\123.xsd /c /language:C# /outputdir: ...