2012-07-24 92 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