Scope of Opening a SQLConnection from a compiled class

M

Mark A. Deal

I have a compiled class that contains two functions. One opens a database
connection and another closes the connection.

If I have a button on a form that calls OpenDatabase(ErrorMessage) and then
call CloseDatabase from the same class, am I operating on the same
connection?

'***************************************************************************
*

' Opens connection to the database *

'***************************************************************************
*

Public Function OpenDatabase(ByRef DBError As String) As Boolean

SW_Connection = New SqlClient.SqlConnection(ReadRegKey("ConnectionString"))

Try

SW_Connection.Open()

Catch ex As Exception

DBError = "An error occurred opening connection to database: " & ex.Message

Return (False)

End Try

Return (True)

End Function

'***************************************************************************
*

' Closes connection to the database *

'***************************************************************************
*

Public Function CloseDatabase() As Boolean

SW_Connection.Close()

End Function


--
Mark A. Deal
Document & Data Solutions, LLC
http://www.docsol.com
Time Matters AIC
HotDocs Certified Consultant
GhostFill Certified Consultant
 
A

Alvin Bruney [MVP]

You created a heap object and saved the reference to the object in
SW_Connection. If you didn't change that reference somewhere in your code,
then the SW_Connection.Close() call,
closes the previously opened connection. If you changed the reference
somewhere in your code, you aren't closing the connection that you first
opened. This is a resource leak.

--
Regards,
Alvin Bruney

Shameless Author plug
The Microsoft Office Web Components Black Book with .NET
http://tinyurl.com/27cok
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top