一、事务定义:
显式事务:明确指定事务的开始,connection需要打开方可使用,默认为closed,即:显示调用con.BeginTransaction()
隐式事务:无法明确指定事务的开始,默认connection已被打开为open
分布式隐式事务:使用TransactionScope类 , 【跨库且使用同一个事务提交】
分布式显式事务:使用CommittableTransaction类,con.EnlistTransaction(Transaction对象)//将连接登记到事务 【跨库且使用同一个事务提交】
//以下事例采用Linq to SQL 数据源
二、显示事务实例:
using (DataClasses1DataContext datacontext = new DataClasses1DataContext())
{
try
{
//必须打开连接,用于事务的创建
if (datacontext.Connection.State == ConnectionState.Closed) datacontext.Connection.Open();
//创建事务以及将创建的事务分配给该DataContext的Transaction
datacontext.Transaction = datacontext.Connection.BeginTransaction();
var test_tran = datacontext.Products.Where(a => a.ProductID < 5).OrderBy(a => a.Produc