vb.net 如何更改 VB 2010 中 My.Settings 中保存的连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9069691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:54:39  来源:igfitidea点击:

How To Change The Connection String saved in My.Settings in VB 2010

vb.netconnection-string

提问by paulcheil

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 ?

我正在编写一个应用程序,我使用向导来创建自动创建自己的 xml 代码的数据集。此代码使用保存在 My.Settings 连接字符串中。作为具有不可更改的“应用程序”范围的设置,我无法在运行时更改其值。问题是,当我向我的客户安装应用程序时,连接字符串将不得不更改(可能不止一次)。那么有没有办法在运行时更改这些对象使用的连接字符串?

回答by CoderDennis

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.configfile. 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 .configfile automatically. Also, your users could edit the config file manually.

但是,更简单的解决方案是仅使用该app.config文件。当向导创建您的数据集时,它应该将连接字符串添加到app.config,它将作为<assemblyName>.config. 然后您的安装程序可以确定正确的连接字符串并.config自动编辑文件。此外,您的用户可以手动编辑配置文件。