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 ...
随机推荐
- [bzoj4665]小w的喜糖_二项式反演
小w的喜糖 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=4665 数据范围:略. 题解: 二项式反演裸题. $f_{i,j}$表示,前$i$种钦 ...
- [官网]PG12发布了
PostgreSQL 12 Press Kit https://www.postgresql.org/about/press/presskit12/zh/#original_release Conte ...
- Hadoop的eclipse的插件是怎么安装的?
[学习笔记] 1)网上下载hadoop-eclipse-plugin-2.7.4.jar,将该jar包拷贝到Eclipse安装目录下的dropins文件夹下,我的目录是C:\Users\test\ec ...
- python中内存地址
遇到一个朋友,给我提了一个问题:python中的两个相同的值,内存地址是否一样? 当时印象里有这样一句话:Python采用基于值的内存管理模式,相同的值在内存中只有一份 于是张嘴就说是一样的 朋友说不 ...
- WUST 设计模式 实验九 观察者模式的应用
实验九 观察者模式的应用 一.实验目的 掌握外观模式(Observer)的特点: 分析具体问题,使用外观模式进行设计. 二.实验内容和要求 网上商店中如果商品(product)在名称(name). ...
- scratch少儿编程第一季——01、初识图形化界面编程的神器
各位小伙伴大家好: 说到2018年互联教育的热门事件,那就不得不提Scratch. 相信各位不关注信息技术领域的各位家长也都听说过这个东西. 对于小学阶段想要接触编程或信息技术学生来说,Scratch ...
- 红帽linux系统开机自启动脚本。
其实很多东西在最后完成以后会觉得也就那样,有意思的是探究的过程. 前段时间老板要求把一个程序做成linux系统开机自启动脚本的模式. 首先你需要写一个脚本. 我这边建立了一个.sh的脚本,就是用脚本启 ...
- hdu 4745 two Rabits
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4745 题解:首先要抽象出题目要求的是啥.首先对于环的问题, 我们可以倍增成链,然后环的所有情况可以通过链来 ...
- (二)Spring框架之JDBC的基本使用(p6spy插件的使用)
案例一: 用Spring IOC方式使用JDBC Test_2.java package jdbc; import java.lang.Thread.State; import java.sql.Co ...
- MiniUI学习笔记一【转】
MiniUI Api文档:http://miniui.com/docs/api/index.html 1.取组件值 传递form data,load发送 请求加载数据 <script type= ...