使用.Net2.0的事务
来源:互联网 发布:禁毒知识网络竞赛 编辑:程序博客网 时间:2024/05/18 01:12
在2.0框架下,可以使用System.Transactions这个命名空间下的一些类,其中System.Transactions.TransactionScope,实现了隐式的事务处理方式,可以判断事务的类型来使用本地级事务或者是分布式事务.我的测试代码如下:
try
{
using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
{
using (System.Data.SqlClient.SqlConnection sql1 = new System.Data.SqlClient.SqlConnection(@"Data Source=LYSERVER/LYSERVER;Initial Catalog=pubs;Persist Security Info=True;User ID=sa"))
{
sql1.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = sql1;
cmd.CommandText = "INSERT INTO stores(stor_id,stor_name,stor_address,city,state,zip) values('8888','测试分布式事务','沈阳市三好街','沈阳','LY','111')";
cmd.ExecuteNonQuery();
sql1.Close();
}
if (MessageBox.Show("您要中断对本地数据库的访问吗?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
throw new Exception("用户中断,自动回滚全部事务!");
}
using (System.Data.SqlClient.SqlConnection sql1 = new System.Data.SqlClient.SqlConnection(@"Data Source=wu;Initial Catalog=pubs;Integrated Security=True"))
{
sql1.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = sql1;
cmd.CommandText = "INSERT INTO stores(stor_id,stor_name,stor_address,city,state,zip) values('8888','测试分布式事务','沈阳市三好街','沈阳','LY','111')";
cmd.ExecuteNonQuery();
sql1.Close();
}
scope.Complete();
MessageBox.Show("commited");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
注意:在使用System.Transactions时,VS2005并没有引用其相应的Dll,需要手动的引用System.Transaction.dll.
{
using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
{
using (System.Data.SqlClient.SqlConnection sql1 = new System.Data.SqlClient.SqlConnection(@"Data Source=LYSERVER/LYSERVER;Initial Catalog=pubs;Persist Security Info=True;User ID=sa"))
{
sql1.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = sql1;
cmd.CommandText = "INSERT INTO stores(stor_id,stor_name,stor_address,city,state,zip) values('8888','测试分布式事务','沈阳市三好街','沈阳','LY','111')";
cmd.ExecuteNonQuery();
sql1.Close();
}
if (MessageBox.Show("您要中断对本地数据库的访问吗?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
throw new Exception("用户中断,自动回滚全部事务!");
}
using (System.Data.SqlClient.SqlConnection sql1 = new System.Data.SqlClient.SqlConnection(@"Data Source=wu;Initial Catalog=pubs;Integrated Security=True"))
{
sql1.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = sql1;
cmd.CommandText = "INSERT INTO stores(stor_id,stor_name,stor_address,city,state,zip) values('8888','测试分布式事务','沈阳市三好街','沈阳','LY','111')";
cmd.ExecuteNonQuery();
sql1.Close();
}
scope.Complete();
MessageBox.Show("commited");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
- 使用.Net2.0的事务
- ADO.Net2.0新的事务类型
- 理解.NET2.0中的事务
- ASP.NET2.0 ObjectDataSource的使用详解
- ASP.NET2.0 ObjectDataSource的使用详解
- ASP.NET2.0 ObjectDataSource的使用详解
- asp.net2.0中Freetextbox的使用
- .NET2.0中客户端脚本的使用
- ASP.NET2.0 ObjectDataSource的使用详解
- asp.net2.0中泛型的使用
- .NET2.0App_Code文件夹的使用
- asp.net2.0中泛型的使用
- ASP.NET2.0 ObjectDataSource的使用详解
- asp.net2.0 GridView的整理使用
- asp.net2.0中泛型的使用
- ASP.NET2.0登陆控件的使用
- ASP.NET2.0中XSLT的使用
- ASP.NET2.0中XSLT的使用
- 工作中
- java 高性能网络编程
- 装修日记19 装修上的风水学问
- Mysql UDF
- SQL Server 2005 常用配置
- 使用.Net2.0的事务
- C#与C++的区别
- linux平台调试技术总结。
- VS2005开发windows服务的注意事项
- 在Visual C#中访问不同数据库(转)
- 如何判断两个矩形相交
- EXISTS和in
- 我的博客
- Jack Wu -- Implementation Search help EXIT