Question oledb connection commonto all forms

mathumathi

Member
Joined
Mar 25, 2009
Messages
9
Programming Experience
Beginner
hai
my application in vb.net contains 4 forms. my oledb connection to the database is needed in all these forms. at prsent i am opening and closing connectionfor each form. but i want the connection string to be given only once and to be used in all the forms.can anyone help me to do this.
thanks
 
hai
my application in vb.net contains 4 forms. my oledb connection to the database is needed in all these forms. at prsent i am opening and closing connectionfor each form. but i want the connection string to be given only once and to be used in all the forms.can anyone help me to do this.
thanks

You can place your connection string in a global variable within a module file that can be seen throughout your application. Use the Public keyword instead of Dim .

VB.NET:
Module Module1

    Public g_strDatabaseConnection As String = ""

End Module

I would definitely continue to open & close your connections to the database as needed. It will decrease performance if you try to leave it open throughout the life of the program.

And although I still think the opposite would have actually be true, recent testing that I have performed showed better performance when I create a new connection objects for each use; including when iterating through a loop compared to creating a single connection object with a higher scope. (all still opening & closing connections as needed)
 
The .NET convention is to store your connection string in the config file, which you can do from the Settings tab of the project properties. You can then access it using My.Settings.

ADO.NET is specifically designed such that using multiple connection objects is efficient. An OleDbConnection is actually a lightweight object. The actual database connection exists at a much lower level. Multiple OleDbConnection objects will all use the same low-level database connection as long as they all have the same connection string. For more information look up "connection pooling" on MSDN.
 
Back
Top