.NET事务中的VB6事务

时间:2012-07-24 14:05:12

标签: c# wcf vb6

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

我需要在数据库中保存数据的过程中添加一个新阶段,该阶段包含在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);

提前致谢..

1 个答案:

答案 0 :(得分: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
相关问题