473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Database Connection Module

NH
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

Feb 24 '06 #1
2 1400
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)

"NH" <NH@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
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

Feb 24 '06 #2
NH
can you suggest the best way to handle connection objects?

"Bruce Barker" wrote:
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)

"NH" <NH@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
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


Feb 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: M. Katz | last post by:
I'm relatively new to PHP/MySQL and I've heard about maintaining a persistent connection to the database. To save overhead, I'd like to maintain the connection in a session variable, but I have a...
2
by: Mike D | last post by:
I am using oracle, not by choice. I am getting some errors that I can't debug. I have my db stuff in a module with public functions named database.vb Then I am trying to call the function from...
7
by: Luis Mendes | last post by:
Hello, I've a application in VB.NET that uses a connection to a Access data base stored in my Hard Drive. How can I change the location of the database (To a network) and change the conection...
7
by: Brian Kitt | last post by:
I frequently get the above error on my website. It happens only for short periods of times, then the error goes away. I cannot recreate this. I have an error trap that picks up this error. When...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
1
by: r2destini | last post by:
Hi Friends, I am new to .Net. So I don't know much. I am facing a problem in updating database through ADO.Net I am creating the dataset and there is no problem in the updation and...
5
by: kyosohma | last post by:
Hi, I am populating a mySQL database with data from the MS Access database. I have successfully figured out how to extract the data from Access, and I can insert the data successfully into mySQL...
5
by: rm | last post by:
When using ADO in a module inside an Access 2003 database do I need to open a db connection even though I am working with a table inside the same database? I am writing a bit of code to...
2
by: cybervegan | last post by:
Hi, I'm a bit new to MySQL; here's a philisophical question. I'm working on getting my head round apache2+mod_python+mysql for web applications. It all seems pretty straight-forward, and I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.