在「CAM」中插入數據後,我有2個要插入數據的表格,表格「CAM」和表格「COut」,它將返回一個retAutoID作爲「COut」插入所需的參數之一。我在業務邏輯中實現了回滾,因此如果「Cout」在插入時遇到異常錯誤,則即使成功,「CAM」的先前插入也會回滾。SQL事務回滾失敗
與下面的代碼,我試圖做執行插入後收到此錯誤: 「的ExecuteNonQuery要求命令有事務時分配給該命令的連接處於待處理本地事務的事務屬性該命令尚未初始化。「
我認爲這個問題可以對由於:
的DbCommand = GetStoredProcedureCommand( 「COut_Add」) dbCommand.Connection = DBConnection的
如果刪除 「COUT」 的第二數據的插入完全,我將不會有問題。請提供建議。謝謝。
Public Function InsertCM(ByVal objCMBLL As CMBLL, ByVal dbTrans As DbTransaction, ByVal dbConnection As DbConnection, ByVal COut As DataSet) As Boolean
Dim dbCommand As DbCommand = Nothing
Dim retAutoID As Int32 = Nothing
Dim bol_Success As Boolean = False
Try
If dbConnection.State <> ConnectionState.Open Then
dbConnection.Open()
End If
dbCommand = GetStoredProcedureCommand("CAM_Add")
dbCommand.Connection = dbConnection
dbCommand.Transaction = dbTrans
With objCMBLL
AddInParameter(dbCommand, "@Code", DbType.String, 50, DBNull.Value)
If Not String.IsNullOrEmpty(.CamCode) Then
dbCommand.Parameters("@Code").Value = .CamCode
End If
retAutoID = CType(ExecuteScalar(dbCommand), Integer)
dbCommand = GetStoredProcedureCommand("COut_Add")
dbCommand.Connection = dbConnection
For i As Integer = 0 To COut.Tables("COut").Rows.Count - 1
dbCommand.Parameters.Clear()
AddInParameter(dbCommand, "@Ol_Code", DbType.String, 50, DBNull.Value)$
dbCommand.Parameters("@Ol_Code").Value = COut.Tables("CampaignOutlets").Rows(i).Item(0).ToString
AddInParameter(dbCommand, "@Campaign_AutoID", DbType.Int32, 0, DBNull.Value)
dbCommand.Parameters("@Campaign_AutoID").Value = retAutoID
Next i
ExecuteNonQuery(dbCommand)
End With
bol_Success = True
Catch ex As Exception
Throw New DALException(ex, dbCommand, Caching.GetCustomerCodeCookie(), "InsertCampaignManagementTable")
Finally
If Not dbCommand Is Nothing Then
dbCommand.Dispose()
End If
End Try
Return bol_Success
End Function
對於什麼數據庫? –
您傳遞的dbTrans不是來自您的dbConnection的事務嗎? – Carth