Not sure if this is the correct place for this question. I created a module for handling connect and disconnect actions to my database. The reason was to prevent from having to put the same 20+ lines of code in each button action or form that queried the db. This way I can call the DbConnect, handle my db business, the call the DbDisconnect. Here is my module so far:
The question is how do I reference conn in the DbDisconnect in order to .close and .dispose the connection? As a side question, is it OK to do it this way? My reasoning was that if I ever had to change my connection string, it was only in one place.Code:Friend Module Connections Public Sub DbConnect() Using conn As New MySql.Data.MySqlClient.MySqlConnection Try Dim myConnectionString As String = "server=127.0.0.1;" _ & "uid=root;" _ & "pwd=12345;" _ & "database=test" conn.ConnectionString = myConnectionString conn.Open() Catch ex As MySql.Data.MySqlClient.MySqlException MessageBox.Show(ex.Message) End Try End Using End Sub Public Sub DbDisconnect() End Sub End Module
Thanks for any assistance or recommendations.
(VS 2019, MySQL 8.0)




Reply With Quote
