Using MySQL Connection in a different Class

KriZa

Member
Joined
Jun 22, 2007
Messages
21
Programming Experience
3-5
Hello,
It concerns the following problem:
I got a form, in which I declare a MySQL-Connection to my DB. now I need this connection in different foms. how do I get access? Tried this: created a new Module and put the connect function into it. theoretical I have to write every function using that connection into that Module, but I think I loose the outline of my whole code - too many functions in a single Module.

Form Login
VB.NET:
Imports blablabla
Public Class Login
    Dim conn As MySqlConnection
    Dim...
    Public Sub Login_Load()
        Logging.connect()
    End Sub
End Class

Modul Logging
VB.NET:
Imports blablabla
Public Module Logging
    Dim conn As MySqlConnection
    Dim...
    Public Function connect()
        Dim connStr As String
        connStr = String.Format("server=*; user id=*; password=*; database=*; CharSet=utf8; pooling=false;")
        Try
            conn = New MySqlConnection(connStr)
            conn.Open()
        Catch ex As MySqlException
            MessageBox.Show("Fehler bei Verbindung zum Server: " + ex.Message)
        End Try
        'fill Loginform
        ...
    End Function
End Module
so far so good. but now I won't get into the Logging Module to make my querys and pass all the variables back to my initial Classfunction - that is inefficient! I want to use the variable 'conn' in every other Class - just as using it in the Logging class.

for example:

Formular Control
VB.NET:
Imports blablabla
Public Class Control
    Dim...
    Public Sub Abfrage()
         Try
            daten_nutzer = New DataTable
            da_daten_nutzer = New MySqlDataAdapter("SELECT * FROM nutzer)", conn)
            da_daten_nutzer.Fill(daten_nutzer)
        Catch ex As MySqlException
            MessageBox.Show("Failed to populate database list: " + ex.Message)
        End Try
    End Sub
End Class
whhat to do?:confused::confused::confused:

thanks for helping!
 
I would try something like:

Public shared Function connect() as sqlconn

and call it as:
dim objconnection as sqlconn

sqlconn = myfunction.connect
 
Back
Top