我有一个ASP.NET WCF WCF服务,它从另一个服务中获取数据,并通过事务将其保存在数据库中。即,所有到来的数据要么保存在数据库中(提交),要么什么都不保存(回滚)。
我需要在将数据保存到数据库的过程中添加一个新阶段,该阶段将调用VB6 dll中的函数,该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);提前谢谢..
发布于 2012-07-24 22:25:05
您需要做的是使用.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 herehttps://stackoverflow.com/questions/11632642
复制相似问题