I've inherited an application that uses a global database sqlconnection object in order to access the database from every form in the application. The connection is established when the application starts.
I think to have the connection open all the time it's not good practice and I would prefer to change it so I would open the database connection and close it every time I need to access the database.
So I would like to know if I am in the right here.
This is what I would use instead, any suggestion for improvement is welcome:
Public Sub UpdateDatabase(ByVal command As SqlCommand, ByRef NumError As Double, ByRef DescError As String)
Using connection As New SqlConnection(connectionString)
Try
command.ExecuteNonQuery()
command.Dispose()
NumError = 0
DescError = ""
Catch ex As Exception
NumError = Err.Number
DescError = Err.Description
End Try
End Using
End Sub
I send the SqlCommand object to the method instead of a query string because I can add parameters to an SqlCommand object.