[源码下载]
步步为营VS 2008 + .NET 3.5(12) - DLINQ(LINQ to SQL)之事务处理和并发处理
作者:webabcd
介绍
以Northwind为示例数据库,DLINQ(LINQ to SQL)之事务处理和并发处理
示例
TransactionAndConcurrency.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="TransactionAndConcurrency.aspx.cs"
Inherits="LINQ_DLINQ_TransactionAndConcurrency" Title="事务处理和并发处理" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
事务处理和并发处理详见:TransactionAndConcurrency.aspx.cs
</asp:Content>
Inherits="LINQ_DLINQ_TransactionAndConcurrency" Title="事务处理和并发处理" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
事务处理和并发处理详见:TransactionAndConcurrency.aspx.cs
</asp:Content>
TransactionAndConcurrency.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DAL;
using System.Transactions;
public partial class LINQ_DLINQ_TransactionAndConcurrency : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 事务处理(DataContext.SubmitChanges()会做默认的事务处理)
TransactionDemo1();
// 事务处理(用DataContext.Connection.BeginTransaction()的方式做事务处理)
TransactionDemo2();
// 事务处理
TransactionDemo3();
// 并发处理
Concurrency();
}
/// <summary>
/// 事务处理(DataContext.SubmitChanges()会做默认的事务处理)
/// </summary>
private void TransactionDemo1()
{
try
{
NorthwindDataContext ctx = new NorthwindDataContext();
ctx.Categories.InsertOnSubmit(new Categories { CategoryName = "test", Description = "test" });
ctx.Categories.InsertOnSubmit(new Categories { CategoryID = 1 });
ctx.SubmitChanges();
}
catch
{ }
}
/// <summary>
/// 事务处理(用DataContext.Connection.BeginTransaction()的方式做事务处理)
/// </summary>
private void TransactionDemo2()
{
int? categoryId = null;
NorthwindDataContext ctx = new NorthwindDataContext();
if (ctx.Connection.State != ConnectionState.Open)
{
ctx.Connection.Open();
}
System.Data.Common.DbTransaction tran = ctx.Connection.BeginTransaction();
ctx.Transaction = tran;
try
{
ctx.AddCategory("test", "test", ref categoryId);
ctx.DeleteCategory(1);
tran.Commit();
}
catch
{
tran.Rollback();
}
}
/// <summary>
/// 事务处理(用System.Transactions.TransactionScope做事务处理)
/// </summary>
private void TransactionDemo3()
{
int? categoryId = null;
using (TransactionScope tc = new TransactionScope())
{
try
{
NorthwindDataContext ctx = new NorthwindDataContext();
ctx.AddCategory("test", "test", ref categoryId);
ctx.DeleteCategory(1);
tc.Complete();
}
catch
{ }
}
}
/// <summary>
/// 并发处理
/// </summary>
private void Concurrency()
{
// 修改字段的UpdateCheck为Never,可在对象关系设计器(Object Relational Designer)中修改
// [Column(Storage="_UnitPrice", DbType="Money")]
// [Column(Storage="_UnitPrice", DbType="Money", UpdateCheck=UpdateCheck.Never)]
NorthwindDataContext ctx = new NorthwindDataContext();
var products = ctx.Products;
foreach (var p in products)
{
p.UnitPrice ++;
}
try
{
// 表示即使发生冲突也要继续
ctx.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
}
catch (System.Data.Linq.ChangeConflictException e)
{
foreach (System.Data.Linq.ObjectChangeConflict occ in ctx.ChangeConflicts)
{
Products p = (Products)occ.Object;
// 以当前客户端中的值为准
occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
// 以当前数据库中的值为准
occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
// 如果数据库中的值没有发生变化,则以当前客户端中的值为准。否则,则以当前数据库中的值为准
occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
foreach (System.Data.Linq.MemberChangeConflict mcc in occ.MemberConflicts)
{
// 当前客户端中的值
string currentValue = mcc.CurrentValue.ToString();
// 原来数据库中的值
string originalValue = mcc.OriginalValue.ToString();
// 当前数据库中的值
string databaseValue = mcc.DatabaseValue.ToString();
// 以当前客户端中的值为准
mcc.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
// 以当前数据库中的值为准
mcc.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
// 如果数据库中的值没有发生变化,则以当前客户端中的值为准。否则,则以当前数据库中的值为准
mcc.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
}
}
}
// 表示只要发生冲突就不再继续
ctx.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
// ctx.SubmitChanges();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DAL;
using System.Transactions;
public partial class LINQ_DLINQ_TransactionAndConcurrency : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 事务处理(DataContext.SubmitChanges()会做默认的事务处理)
TransactionDemo1();
// 事务处理(用DataContext.Connection.BeginTransaction()的方式做事务处理)
TransactionDemo2();
// 事务处理
TransactionDemo3();
// 并发处理
Concurrency();
}
/// <summary>
/// 事务处理(DataContext.SubmitChanges()会做默认的事务处理)
/// </summary>
private void TransactionDemo1()
{
try
{
NorthwindDataContext ctx = new NorthwindDataContext();
ctx.Categories.InsertOnSubmit(new Categories { CategoryName = "test", Description = "test" });
ctx.Categories.InsertOnSubmit(new Categories { CategoryID = 1 });
ctx.SubmitChanges();
}
catch
{ }
}
/// <summary>
/// 事务处理(用DataContext.Connection.BeginTransaction()的方式做事务处理)
/// </summary>
private void TransactionDemo2()
{
int? categoryId = null;
NorthwindDataContext ctx = new NorthwindDataContext();
if (ctx.Connection.State != ConnectionState.Open)
{
ctx.Connection.Open();
}
System.Data.Common.DbTransaction tran = ctx.Connection.BeginTransaction();
ctx.Transaction = tran;
try
{
ctx.AddCategory("test", "test", ref categoryId);
ctx.DeleteCategory(1);
tran.Commit();
}
catch
{
tran.Rollback();
}
}
/// <summary>
/// 事务处理(用System.Transactions.TransactionScope做事务处理)
/// </summary>
private void TransactionDemo3()
{
int? categoryId = null;
using (TransactionScope tc = new TransactionScope())
{
try
{
NorthwindDataContext ctx = new NorthwindDataContext();
ctx.AddCategory("test", "test", ref categoryId);
ctx.DeleteCategory(1);
tc.Complete();
}
catch
{ }
}
}
/// <summary>
/// 并发处理
/// </summary>
private void Concurrency()
{
// 修改字段的UpdateCheck为Never,可在对象关系设计器(Object Relational Designer)中修改
// [Column(Storage="_UnitPrice", DbType="Money")]
// [Column(Storage="_UnitPrice", DbType="Money", UpdateCheck=UpdateCheck.Never)]
NorthwindDataContext ctx = new NorthwindDataContext();
var products = ctx.Products;
foreach (var p in products)
{
p.UnitPrice ++;
}
try
{
// 表示即使发生冲突也要继续
ctx.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
}
catch (System.Data.Linq.ChangeConflictException e)
{
foreach (System.Data.Linq.ObjectChangeConflict occ in ctx.ChangeConflicts)
{
Products p = (Products)occ.Object;
// 以当前客户端中的值为准
occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
// 以当前数据库中的值为准
occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
// 如果数据库中的值没有发生变化,则以当前客户端中的值为准。否则,则以当前数据库中的值为准
occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
foreach (System.Data.Linq.MemberChangeConflict mcc in occ.MemberConflicts)
{
// 当前客户端中的值
string currentValue = mcc.CurrentValue.ToString();
// 原来数据库中的值
string originalValue = mcc.OriginalValue.ToString();
// 当前数据库中的值
string databaseValue = mcc.DatabaseValue.ToString();
// 以当前客户端中的值为准
mcc.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
// 以当前数据库中的值为准
mcc.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
// 如果数据库中的值没有发生变化,则以当前客户端中的值为准。否则,则以当前数据库中的值为准
mcc.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
}
}
}
// 表示只要发生冲突就不再继续
ctx.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
// ctx.SubmitChanges();
}
}
OK
[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345015,如需转载请自行联系原作者