I am wondering if I should open the MySQLConnection once at the beginning and close it when the form is disposed, or open and close every time I have to run a command or query.
I’m guessing opening it every time prevents open connections from being left open in case of a bug or a crash.
On the other hand, opening once seems like a more pratical and faster way of doing things. Any thoughts?
Right now this is what I use:
Using connection As New MySqlConnection("Data Source=localhost; user id=root; password=; database=mydata;")
Dim myDataReader As MySqlDataReader
Dim myDT As New DataTable()
Dim myCommand As MySqlCommand = New MySqlCommand("SELECT * FROM...;", connection)
' Code here
End Using
This in essence opens and closes the connection every time. I could also simply use a global MySQLConnection variable and open it when the app loads.
Please give me your thoughts on what is considered best.