Answered by:
Changing ConnectionString at runtime

Question
-
In my application, i am not sure where will be the database. So i thought to put the connection string in app.conf file. So that i may read it runtime and create connection. Working good. But what if i have to change the connectionString define in app.conf at runtime?
It may not be necessary that i must use app.conf, you people can suggest me one better approach so that i may not be dependent on databasestring. It could be created/change at runtime.Friday, February 16, 2007 6:47 AM
Answers
-
Hi
MSDN covers FileSystemWatcher adequately, but there is a VB article here http://www.codeproject.com/dotnet/folderwatcher.asp that you might find useful for getting started. The fsw can be used to notify your code at runtime of a change to a file, so you can accommodate the change.
You can always use the XML or Config classes to make changes to the application config file but for a simple task like this why not look to use application settings.
Go to Properties --> Settings and add a new setting called ConnectionString ..
You can now access the value in code using My.Settings.ConnectionString and update it through the same property. To save changes to the connection string, use My.Settings.Save.
When the fsw notifies you of a change to the app config file, you can use My.Settings.Reload to bring the changed settings into your application.
It's a very simple way of maintaining the value if it's a practical approach for you.
Richard
Friday, February 16, 2007 10:44 AM -
Hi
Actual user scoped settings are not stored in the app.config file. This file will contain the default initial value for the setting only.
When you call the Save method, user scoped changes are stored in a config file located under the user's application data directory, and this is where the setting value is sourced from the next time the user uses your app. The user config is tied to the application config using the installation path, therefore if you change the installation path, the user config will not be located and the main app config is the source of the setting value.
Sounds like it is all working as it should.
Richard
PS - Here is an MSDN article that may be worth a read http://msdn2.microsoft.com/en-us/library/ms379611(vs.80).aspx
Monday, February 19, 2007 12:38 PM
All replies
-
Hi
Using the app.config file is a proven approach.
If the user wants to switch databases at runtime, then perhaps a friendly form in your application would allow them to select the new database and your code can handle the generation of the new connection string and new connections. You could save the new connection string back to the config file if that is what you wanted to do.
If you are concerned that the config file may be changed manually while your application is running, then I would expect it to be acceptable to expect the person who made the change to restart your application. Config file changes do not normally reflect in running applications. Of course, you could attach a file system watcher to your config file and respond to any saved changes at runtime if this is more preferable.
Give me a shout if any of the above requires further detail.
Richard
Friday, February 16, 2007 9:55 AM -
My proposed plan is to have a friendly form. But the problem lies how can i modify the app.conf file at runtime from the application. I have consulted with few people and they said to use the following code
string connect = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
It is great if i have to read the conf file, but how can i write into conf file at runtime. Doing a lil bit search gave me some referece to codeproject article in which the person has use XMLDocument to access app.conf and then changing it. But i think there must be some configuration property like above via which i can edit the configuration file.
It would be great if you also refer me to some good article for fileWatcher, so that i may apply it at the same time. One question. will using the filewatcher ensure me that user don't have to restart the application after changes has been made?P.S:
the above code is in C#, but i am looking for VB2005 thing.
Friday, February 16, 2007 10:18 AM -
Hi
MSDN covers FileSystemWatcher adequately, but there is a VB article here http://www.codeproject.com/dotnet/folderwatcher.asp that you might find useful for getting started. The fsw can be used to notify your code at runtime of a change to a file, so you can accommodate the change.
You can always use the XML or Config classes to make changes to the application config file but for a simple task like this why not look to use application settings.
Go to Properties --> Settings and add a new setting called ConnectionString ..
You can now access the value in code using My.Settings.ConnectionString and update it through the same property. To save changes to the connection string, use My.Settings.Save.
When the fsw notifies you of a change to the app config file, you can use My.Settings.Reload to bring the changed settings into your application.
It's a very simple way of maintaining the value if it's a practical approach for you.
Richard
Friday, February 16, 2007 10:44 AM -
cool, i'll definitely look over it. thanks for helping me out.Friday, February 16, 2007 10:54 AM
-
Dick Donny wrote: Hi
MSDN covers FileSystemWatcher adequately, but there is a VB article here http://www.codeproject.com/dotnet/folderwatcher.asp that you might find useful for getting started. The fsw can be used to notify your code at runtime of a change to a file, so you can accommodate the change.
You can always use the XML or Config classes to make changes to the application config file but for a simple task like this why not look to use application settings.
Go to Properties --> Settings and add a new setting called ConnectionString ..
You can now access the value in code using My.Settings.ConnectionString and update it through the same property. To save changes to the connection string, use My.Settings.Save.
When the fsw notifies you of a change to the app config file, you can use My.Settings.Reload to bring the changed settings into your application.
It's a very simple way of maintaining the value if it's a practical approach for you.
Richard
It's only readonly property. After saving it does not reflect any changes to app.conf file.
Monday, February 19, 2007 7:45 AM -
Hi
Application scoped settings are read only (if using the default provider) ... change it to user scope.
Let me know if this was not the cause of the problem.
Richard
Monday, February 19, 2007 9:40 AM -
i changed the scope to "user". The behaviour of application was strange. Let me try to explain it.
I made a text filed and 2 buttons. On buttone1 press the value of textfield is set to ConnectionString using the method you told. Pressing the button2 give a message which reads the value from app.conf file and displays it. Now, suppose the application is placed in folder name "test". If i change the string by putting it in textfield and then display it using button2. It displays fine, but the sooner i change the name of folder from "test" to any thing it resets the value of connection string. Moreover, if folder name is not changed it display the CHANGED value of connection string but does not show any modification in app.conf file.
Monday, February 19, 2007 10:44 AM -
Hi
Actual user scoped settings are not stored in the app.config file. This file will contain the default initial value for the setting only.
When you call the Save method, user scoped changes are stored in a config file located under the user's application data directory, and this is where the setting value is sourced from the next time the user uses your app. The user config is tied to the application config using the installation path, therefore if you change the installation path, the user config will not be located and the main app config is the source of the setting value.
Sounds like it is all working as it should.
Richard
PS - Here is an MSDN article that may be worth a read http://msdn2.microsoft.com/en-us/library/ms379611(vs.80).aspx
Monday, February 19, 2007 12:38 PM -
That was really great info Richard. Thanks a lot. I guess i may use this "user" scope "connectionString" setting for my app. As Document and Settings is a bit reliable source if the application is installed and has to access settings.
Thanks a lot man!.
cheers
Monday, February 19, 2007 1:13 PM