Database Connection Module

G

Guest

I have a module that handles creating a sql connection object as per below
code. What do you think, is there a better way to do this?

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager

Public Module DBConnections
'Handles Opening and Closing connections to the Database
Public objConnection As SqlConnection

'Get the Connection string from the web config file
Public ConnStr As String = ConnectionStrings("DBString").ConnectionString

Public Sub openDB()
If objConnection Is Nothing = True Then
objConnection = New SqlConnection(ConnStr)
objConnection.Open()
Else

If objConnection.State <> ConnectionState.Open Then
objConnection.Open()
End If
End If
End Sub

Public Sub closeDB()
If objConnection Is Nothing = False Then
objConnection.Close()
objConnection = Nothing
End If
End Sub

End Module
 
B

Bruce Barker

bad idea if used with asp.net.

the code is not threadsafe, and am not sure why you want to share the same
connection object with all current page requests. if you add the required
locking, you website could only process one request at a time.

-- bruce (sqlwork.com)
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top