2012-07-24 91 views
1

我有一个ASP.NET WCF Web服务,它从另一个服务获取数据并通过事务将其保存在数据库中。即所有来的数据都保存在数据库(提交)中或者没有(回滚)。VB6在.NET事务中的事务

我需要在数据库中保存数据的过程中添加一个新阶段,该阶段将调用VB6 dll中的函数,该函数也使用事务来连接到同一个数据库。那可能吗?

这里是用来调用VB6功能的.NET代码:

object oMissing = System.Reflection.Missing.Value; 
ADODB.Recordset rsKR = new ADODB.Recordset(); 
rsKR.Fields.Append("F1", ADODB.DataTypeEnum.adVarChar, 10, ADODB.FieldAttributeEnum.adFldFixed, null); 
rsKR.Fields.Append("F2", ADODB.DataTypeEnum.adVarChar, 10, ADODB.FieldAttributeEnum.adFldFixed, null); 

rsKR.Open(oMissing, oMissing, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1); 
rsKR.AddNew(oMissing, oMissing); 

rsKR.Fields["F1"].Value = someObject.Id; 
rsKR.Fields["F2"].Value = someObject.Name; 

rsKR.Update(oMissing, oMissing); 
rsKR.MoveFirst(); 

VB6Project.ClassAPI objVBAPI = new VB6Project.ClassAPI(); 
objVBAPI.InsertIntoDBFunction(rsKR); 

在此先感谢..

+0

我试图从.NET服务调用VB6的dll和顺利,但我得到异常,当VB6获取对数据库中插入数据的阶段。 – Lina 2012-07-24 14:09:09

+0

因此,Vb6 DLL中的其他函数可以正常工作,并且在尝试将数据插入到数据库时会出现问题。 – JMK 2012-07-24 14:10:08

+0

是的,这正是发生了什么 – Lina 2012-07-24 14:11:51

回答

1

你需要做的是使用.NET TransactionScope对象包装所有的什么你的操作,你想在特例情况下回滚。您还需要确保您的VB6组件在COM +服务中运行,并且已启用事务。

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, 
     new TransactionOptions(), EnterpriseServicesInteropOption.Full)) 
{ 
    dotNetDatabaseOperations(); //call whatever .net code here 
    comProxy.comMethod(); //call to your VB6 component with interop wrapper 

    scope.Complete(); 
} //if any exception happens in either .net or COM, 
    //entire transaction will be rolled back here 
+0

非常感谢您的回答,我漫长的一天刚刚结束,我会尝试明天,并告诉您如何去做。 – Lina 2012-07-24 14:29:59

+0

Goodluck,随时在这里添加任何后续问题,我会尽力帮助。 – EkoostikMartin 2012-07-24 14:30:35

+0

如果事务在VB中启动*,或者VBA和*在MTS中运行,那么这可能吗? – tbone 2014-06-05 03:36:37