My app is using MySQL. Right now, the IP/Port, username and all that stuff is hard coded in a string.

Public urlMySQLDatabase1 As String = "Server=;port=3306;Database=DBName;Uid=DBName;Pwd=DBpassword;"

So all the code that needs this string gets it here.

But I was thinking- what if later in the future I need to change this- I could send out an email to the users to open a form I created and they could change the IP. So I created a form, and I can write this IP to an .ini file my app uses to hold some other data.

When the app pulls anything from the .ini file, it calls the loadSettingsFile()

Public Sub loadSettingsFile()
        If File.Exists(mySettingsFile) Then
            Dim arFileLines() As String = IO.File.ReadAllLines(mySettingsFile)
            For Each line As String In arFileLines
                With frmLogin
                    If line.StartsWith("Login ID = ") Then .txtUserID.Text = line.Substring(line.IndexOf("=") + 2)
                    If line.StartsWith("Password = ") Then .txtUserPassword.Text = line.Substring(line.IndexOf("=") + 2)
                End With
                With frmServerIP
                    If line.StartsWith("ServerIP = ") Then .txtServerIP.Text = line.Substring(line.IndexOf("=") + 2)
                End With
        End If
    End Sub

So my question is- how can I write that string above (urlMySQLDatabase1) to use the IP that's written in the .ini file and not always hard coded? I can't seem to wrap my head around this.


5 Years
Discussion Span
Last Post by mrbungle

Use the app.config (or web.config for a web app). In the <Configuration> section of the app.config add this:

        <add name="name_of_your_connection_string"
            providerName="System.Data.SqlClient" />

There you access it in code via:

Dim conStr As String = ConfigurationManager.ConnectionStrings(name_of_your_connection_string).ConnectionString;

Thanks for taking the time to reply hericles. For some reason I forgot about the config settings.

This question has already been answered. Start a new discussion instead.
Please be thoughtful and detailed and be sure to adhere to our posting rules.