I have a function which I open a connection object and assign to a command object. Does the connection object need to also be closed in addition to the command object's connection or is the iCon closing by reference of closing the iCmd.Connection.Close() method? Thanks (see below)
Public Function ExecuteNonQuery(ByVal dict As IDictionary) As Boolean Implements IDataAccess.ExecuteNonQuery
Dim success As Boolean = True
Dim iCon As IDbConnection = GetConnection(FWPortal.Globals.Configuration.Profile.GetUserAccountConnectionString, DataProvider.SQL)
Dim iCmd As IDbCommand
iCon.Open()
Dim iTxn As IDbTransaction
Dim result As Integer
iTxn = iCon.BeginTransaction
Try
Dim iEnum As IEnumerator = dict.GetEnumerator
While iEnum.MoveNext
iCmd = CType(CType(iEnum.Current, DictionaryEntry).Value, IDbCommand)
iCmd.Connection = iCon
iCmd.Transaction = iTxn
iCmd.ExecuteNonQuery()
End While
iTxn.Commit()
Catch ex As Exception
iTxn.Rollback()
Throw ex
success = False
Finally
====================================================================================
We open a connection “iCon” so we can assign it to a transaction, then assign it to the iCmd object, which we close here in the Finally block
Do we need to also close the “iCon” object or are doing this (by reference) when closing the iCmd.Connection.Close()?
If iCon.State <> ConnectionState.Closed Then
iCon.Close()
End If
====================================================================================
If iCmd.Connection.State <> ConnectionState.Closed Then
iCmd.Connection.Close()
End If
End Try
Return success
End Function
regards,
Brian
Public Function ExecuteNonQuery(ByVal dict As IDictionary) As Boolean Implements IDataAccess.ExecuteNonQuery
Dim success As Boolean = True
Dim iCon As IDbConnection = GetConnection(FWPortal.Globals.Configuration.Profile.GetUserAccountConnectionString, DataProvider.SQL)
Dim iCmd As IDbCommand
iCon.Open()
Dim iTxn As IDbTransaction
Dim result As Integer
iTxn = iCon.BeginTransaction
Try
Dim iEnum As IEnumerator = dict.GetEnumerator
While iEnum.MoveNext
iCmd = CType(CType(iEnum.Current, DictionaryEntry).Value, IDbCommand)
iCmd.Connection = iCon
iCmd.Transaction = iTxn
iCmd.ExecuteNonQuery()
End While
iTxn.Commit()
Catch ex As Exception
iTxn.Rollback()
Throw ex
success = False
Finally
====================================================================================
We open a connection “iCon” so we can assign it to a transaction, then assign it to the iCmd object, which we close here in the Finally block
Do we need to also close the “iCon” object or are doing this (by reference) when closing the iCmd.Connection.Close()?
If iCon.State <> ConnectionState.Closed Then
iCon.Close()
End If
====================================================================================
If iCmd.Connection.State <> ConnectionState.Closed Then
iCmd.Connection.Close()
End If
End Try
Return success
End Function
regards,
Brian