473,606 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sql Client
Imports System.Configur ation.Configura tionManager

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 = ConnectionStrin gs("DBString"). ConnectionStrin g

Public Sub openDB()
If objConnection Is Nothing = True Then
objConnection = New SqlConnection(C onnStr)
objConnection.O pen()
Else

If objConnection.S tate <> ConnectionState .Open Then
objConnection.O pen()
End If
End If
End Sub

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

End Module

Feb 24 '06 #1
2 1408
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******** *************** ***********@mic rosoft.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.Sql Client
Imports System.Configur ation.Configura tionManager

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 =
ConnectionStrin gs("DBString"). ConnectionStrin g

Public Sub openDB()
If objConnection Is Nothing = True Then
objConnection = New SqlConnection(C onnStr)
objConnection.O pen()
Else

If objConnection.S tate <> ConnectionState .Open Then
objConnection.O pen()
End If
End If
End Sub

Public Sub closeDB()
If objConnection Is Nothing = False Then
objConnection.C lose()
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******** *************** ***********@mic rosoft.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.Sql Client
Imports System.Configur ation.Configura tionManager

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 =
ConnectionStrin gs("DBString"). ConnectionStrin g

Public Sub openDB()
If objConnection Is Nothing = True Then
objConnection = New SqlConnection(C onnStr)
objConnection.O pen()
Else

If objConnection.S tate <> ConnectionState .Open Then
objConnection.O pen()
End If
End If
End Sub

Public Sub closeDB()
If objConnection Is Nothing = False Then
objConnection.C lose()
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
4394
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 few questions first. I'm able to establish session variables and test them with isset() to avoid re-defining them with each new web-page load. So is this really as simple as if (!isset($_SESSION)) {
2
1269
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 a form like this: Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim iPortNumber As Short
7
1396
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 string? In VB6 this was very easy, but under VB.NET I'm not able to do it.
7
23914
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 it happens, I'll get 10 to 30 in a row, then it goes away for a week or so. It always comes back though. It's driving me crazy. At the start of a web page, I instantiate a 'global' module. The global module sets all of the connection...
13
2344
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 (replacement for PHP's include() statement). My problem is, in PHP if you open a database connection it's always in scope for the duration of the script. Even if you use an abstraction layer ($db = DB::connect(...)) you can `global $db` and bring...
1
2029
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 deletion or insertion in the dataset but when I am updating the
5
3759
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 with Python. My problem is that I keep hitting screwy records with what appears to be a malformed dbiDate object when I insert certain records. I get the following traceback: Traceback (most recent call last):
5
2520
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 periodically import data from a text file to a table in a database. The capabilities of the import wizard are not sufficient to support what we need for manipulating/parsing the ascii text file.
2
3363
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 making great progress so far. However, I'd like to know your opinions on whether it is better to open a database connection once, globally, at the beginning of each module, and keep it open "forever" or to do a "drive by query"; i.e. open/query/close...
0
8010
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7942
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8429
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8084
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5461
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3922
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1550
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.