2011-11-11 81 views
0

在“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 
+0

对于什么数据库? –

+0

您传递的dbTrans不是来自您的dbConnection的事务吗? – Carth

回答

1

好的结果是,我没有包含'dbCommand.Transaction = dbTrans'为tbl“COut”。

0

我看到你的代码:

dbCommand.Connection =的DbConnection(第2次)。

你真的需要重新分配命令连接吗?我想你不应该。我们应该只重新分配Command对象的sql文本。 这应该是这样的:

dbCommand.text = GetStoredProcedureCommand( “COUT”)

代替

的DbCommand = GetStoredProcedureCommand( “COut_Add”) dbCommand.Connection = DBConnection的

For i As Integer = 0 To COut.Tables(“COut”)。Rows.Count - 1 dbCommand.Parameters.Clear()

我想你必须在循环之前调用dbCommand.Parameters.Clear()。

HTH。