is it possible to use one database connection throughout an application. The way I have it now I open a connection when I need it then close it.
Printable View
is it possible to use one database connection throughout an application. The way I have it now I open a connection when I need it then close it.
you mean when the application is started the connection begins and remains connected until the application is closed???
You should be able to declare that in the app.config or web.config file, depending on which type of application you are creating.
This is how I was taught to do it from the book "Visual Basic .Net For Access Databases"
Now you should be able to use it from any control. I think you could use a variation on that for any type database / situation.Code:' Dim and set connection string above control code
Dim cnn1 As System.Data.OleDb.OleDbConnection = connectToNorthwind()
Function connectToNorthwind() As System.Data.OleDb.OleDbConnection
Dim str1 As String = ("Provider=Microsoft.Jet.OLEDB.4.0;")
str1 += ("Data Source=C:\Visual Studio Projects\Access2k\Northwind.mdb")
Dim cnn1 As New System.Data.OleDb.OleDbConnection
cnn1.ConnectionString = str1
Return cnn1
End Function
if you connect manualy the connection will stay open till you close it manualy ( like the exp above ), but if your using a dataadapter then the dataadapter will open and close the connection when needed
that's not completely true persianboy...with .NET's built in GC...any variables or references to variables that go out of scope with be collected by the GC and destroyed...so you can manually open the connection, but you don't have to manually close it, because one the connection loses it references, it is no longer in scope and the GC will come along and destroy it.
but when the connection is active , there is no reason for the GC to collect it, so it will stay open while the program is running. plz tell me if i got anything wrong, thanks
you still have to take in to consideration the connection timeout when the connection is idle. If the connection hasn't been referenced (used) within a certain amount of time it will release the references and destroy the connection.
Why not just use connection pooling?