Question

Say I have a connection to a database. After reading some stuff I want to connect to the same database. So I naturally use the first database mysqlconnection.serverstring

Well guess what, the password and username is not included in connect1.ConnectionString. So how can I do so? Is this by design?

      Using connect1 As New MySqlConnection(ConnectLocalhost.serverString)
            connect1.Open()
bla bla bla
                Using connect2 As New MySqlConnection(connect1.ConnectionString) ' this won't work
                    connect2.Open()
                End Using
Was it helpful?

Solution

You need to put Persist Security Info = true in your original connection string otherwise it won't be read back.

Course if you do that you might as well not have a user name and password...

OTHER TIPS

  1. Store your Connection String in your configuration - as It need to be changed outside the code (for example - production vs development).

  2. Write an function that returned a active connection or wrapper object, so you can use:

    Using MyHelper.GetConnection()
    
       ...
    
    End Using
    
  3. In this way, you can use connection and close it, as the connection pooling save the connection for later use.

Can you not just re-use your original connection? Unless you have explicitly closed it, you should still have access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top