Brian Prodigy

Hello All

I have an application that retrieves data off a database on a LAN.
Is there any way I can change the IP address of the data source during runtime.
So that I can configure the source when the application is running


Re: Visual Basic Express Edition Data Source IP

Derek Smyth

Hi,

You'll be using a connection string to connect to the database. If you change the connection string you can change which database the applications uses. Your connection string is like this

Data Source=127.0.0.1;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
There are a couple ways you can do it, you could have more than one connection string defined in your app.config file. You could then switch database by referring by the connection strings name. Or the other option would be to use the SqlConnectionStringBuilder. Load your connection string into the builder (there should be a constructor for this) and then you can set new IP using the DataSource property of the builder.





Re: Visual Basic Express Edition Data Source IP

Brian Prodigy

Great, but is there a way of altering an existing string ie if I dont know all the source IP addresses before I compile the application. Is there a way I can specify a new source eg by changing the IP address of the source database. The database name and other variable will remain the same, just the IP address will change. Is there a way to do this




Re: Visual Basic Express Edition Data Source IP

Derek Smyth

Hi,

Yes you can use the applications configuration file (app.config / web.config). There are many ways to do this, it all depends on what your specifically looking for. Example, you could place your connection string in the config file and use the appsettings section to specify the ip address.

I have a project that works off any Access database that has a certain structure, the user selects the path to the database using a file open dialog. This is the connection string in my configuration file it's missing the Data Source.

<configuration>

<connectionStrings>

<add name ="OLEDBase" connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" providerName ="System.Data.OleDb"/>

</connectionStrings>

<configuration>

I load the connection string into my application and using the OleDBConnectionStringBuilder I set the path of the DataSource using the input of the user.

Dim connBuildr As New OleDbConnectionStringBuilder(ConfigurationManager.ConnectionStrings("OLEDBase").ConnectionString)

connBuildr.DataSource = DBaseFilename 'the file path the user selected

Me.m_connString = connBuildr.ConnectionString

The application dynamically works with any Access database (with the structure) without having to recompile.

What you could do is have your connection string in your file minus the IP address (just as the example is minus the DataSource). At runtime you can either load your IP address from a file, from a user input, from the AppSettings section of the config file, from any where you need to and just as the example sets the DataSource in the OleDbConnectionStringBuilder you do it with the Sql Server equivalent.