Question Can I make 1 db connection to be called by entire application?

motoburn

Member
Joined
Jun 15, 2010
Messages
17
Location
Collinwood, TN
Programming Experience
Beginner
Hello,
I am building a front-end to a mysql database on our local network. I would like to establish a connection to the server at start-up and keep that connection available to all of my forms.

a. is this possible
b. if it is possible, is this a good idea, or would that pose issues

I am fairly new at vb.Net, but have had no problem creating a login form and connecting to the server, passing statements to the server and retrieving data sets.. just not sure how to go about openning some sort of static connection and calling it from any form that might need it...

I tried to make a Friend connection in a module that I could call... but that did not work
VB.NET:
Imports MySql.Data.MySqlClient

Module staticVars
    Friend dbName As String = My.Settings.dbName
    Friend host As String = My.Settings.serverHost
    Friend uName As String = My.Settings.uName
    Friend uPwd As String
    Friend conn As MySqlConnection = New MySqlConnection

    Friend connStr As String = "server=" & host & ";" & _
                               "user id=" & uName & ";" & _
                               "password=" & uPwd & ";" & _
                               "database=" & dbName
    [COLOR="Red"]conn.ConnectionString = connstr[/COLOR]

End Module
but other than identifying the variable names as Friend, I can't seem to accomplish what I want.

I've looked through several variations of questions in google, and the forums, but didnt find much (probably not sure what keywords to ask about...)


Thanks in advance for any guidance

Chris




(using VS 2008 - Windows Forms Application - VB.net)
 
Back
Top