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=173.201.88.22;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
            Next
        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.

TIA

Recommended Answers

All 2 Replies

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.