How To Change The Connection String saved in My.Se

2020-07-23 04:53发布

问题:

I am writting an application and I used Wizard to create DataSets which auto-created their own xml code. This code uses the saved in My.Settings Connection String. Being a setting with an unchangable 'Application' scope i cannot change its value at runtime. The problem is that when I install the Application to my customer the Connection String will have to change (maybe more than once). So is there a way to change the Connection String used by these objects at runtime ?

回答1:

Here's how to edit the setting via code:

My.Settings.Item("ConnectionString") = "some connection string"

However, an easier solution would be to just use the app.config file. When the wizard creates your DataSets it should be adding the connection strings to app.config, which will get copied to your project's output directory as <assemblyName>.config. Then your installer could determine the proper connection string and edit the .config file automatically. Also, your users could edit the config file manually.