using (TransactionScope transaction = new TransactionScope()) { try { Store.DBDataContext dc = new Store.DBDataContext(); Store.Product product = GetProduct("foo"); dc.InsertOnSubmit(product); dc.SubmitChanges(); transaction.Complete(); } catch (Exception ex) { throw ex; } }
但是,我不断收到以下错误:
合作伙伴事务管理器已禁用其对远程/网络事务的支持. (HRESULT异常:0x8004D025)
但是,如果我使用传统交易设置交易,它可以正常工作.所以这很好用:
Store.DBDataContext dc = new Store.DBDataContext(); try { dc.Connection.Open(); dc.Transaction = dc.Connection.BeginTransaction(); Store.Product product = GetProduct("foo"); dc.InsertOnSubmit(product); dc.SubmitChanges(); dc.Transaction.Commit(); } catch (Exception ex) { dc.Transaction.Rollback(); throw ex; } finally { dc.Connection.Close(); dc.Transaction = null; }
我想知道TransactionScope是否正在做一些与我的第二次实现不同的事情.如果没有,我没有使用TransactionScope失去了什么?此外,任何导致错误的指导都会很好.我已经确认MSDTC在sql server和我的客户端机器上运行.
解决方法
使用System.Transactions和Microsoft sql Server 2000进行快速事务
http://blogs.msdn.com/florinlazar/archive/2005/09/29/475546.aspx
和这里:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=230390&SiteID=1
First verify the “Distribute Transaction Coordinator” Service is
running on both database server computer and client computers
1. Go to “Administrative Tools > Services”
2. Turn on the “Distribute Transaction Coordinator” Service if it is not runningIf it is running and client application is not on the same computer as
the database server,on the computer running database server
1. Go to “Administrative Tools > Component Services”
2. On the left navigation tree,go to “Component Services > Computers > My Computer” (you may need to double click and wait as some nodes
need time to expand)
3. Right click on “My Computer”,select “Properties”
4. Select “MSDTC” tab
5. Click “Security Configuration”
6. Make sure you check “Network DTC Access”,“Allow Remote Client”,
“Allow Inbound/Outbound”,“Enable TIP” (Some option may not be
necessary,have a try to get your configuration)
7. The service will restart
8. BUT YOU MAY NEED TO REBOOT YOUR SERVER IF IT STILL DOESN’T WORK
(This is the thing drove me crazy before)On your client computer use the same above procedure to open the
“Security Configuration” setting,make sure you check “Network DTC
Access”,“Allow Inbound/Outbound” option,restart service and computer
if necessary.On you sql server service manager,click “Service” dropdown,select “Distribute Transaction Coordinator”,it should be also running on your server computer.