OleDbCommand should be disposed? Holding conn reference...

  • Thread starter andre luiz via .NET 247
  • Start date
A

andre luiz via .NET 247

Hello Everybody,

Can someone help me? I'm not sure about the code below. It'sworking but... . I create a new OledbConnection, opened andcalled a method. This method creates a new OleDbCommand that usethe connection (via another class; cn is a member in an dataclass). But the OleDbCommand is not disposed after the method isfinished (should I dispose it?). After that, the connection isdisposed. See below:

' Creates and open a connection
Dim cn as OleDb.OleDbConnection
cn = New OleDb.OleDbConnection(strConn)
cn.Open()

' call method
retDatTable()

' Dispose connection
cn.Dispose()

Function retDatTable(ByVal strSQL As String) As DataTable
Dim oDataTable As New DataTable()
Dim oOleDbCommand As OleDb.OleDbCommand
oOleDbCommand = New OleDb.OleDbCommand(strSQL, cn)
Dim oDataAdapter As New OleDb.OleDbDataAdapter(oOleDbCommand)
oDataAdapter.Fill(oDataTable)
Return oDataTable
End Function

The connection was disposed. But... the OleDbCommand that usedthe connection was not. This connection will be returned to thepool? I'm not sure because OleDbCommand was not disposed... so,he continue to hold the connection reference, right? Or I'mmissing something...??

Thanks!!

Andre
 
W

William \(Bill\) Vaughn

You do not need to dispose of a Connection (or Command) object to return the
connection to the pool but you do need to Close it. The problem might be
that the connection is orphaned (dropped from scope before it's closed). You
should not open the connection before using it with the Fill (in most
cases). Let Fill manage that for you. In this case you won't need the
Cn.Dispose (but it won't hurt anything).

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Hello Everybody,

Can someone help me? I'm not sure about the code below. It's working but...
.. I create a new OledbConnection, opened and called a method. This method
creates a new OleDbCommand that use the connection (via another class; cn is
a member in an data class). But the OleDbCommand is not disposed after the
method is finished (should I dispose it?). After that, the connection is
disposed. See below:

' Creates and open a connection
Dim cn as OleDb.OleDbConnection
cn = New OleDb.OleDbConnection(strConn)
cn.Open()

' call method
retDatTable()

' Dispose connection
cn.Dispose()

Function retDatTable(ByVal strSQL As String) As DataTable
Dim oDataTable As New DataTable()
Dim oOleDbCommand As OleDb.OleDbCommand
oOleDbCommand = New OleDb.OleDbCommand(strSQL, cn)
Dim oDataAdapter As New OleDb.OleDbDataAdapter(oOleDbCommand)
oDataAdapter.Fill(oDataTable)
Return oDataTable
End Function

The connection was disposed. But... the OleDbCommand that used the
connection was not. This connection will be returned to the pool? I'm not
sure because OleDbCommand was not disposed... so, he continue to hold the
connection reference, right? Or I'm missing something...??

Thanks!!

Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top