vb.net 如何在运行时更改 app.config 文件的连接字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19429477/
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
How can i change my app.config file's connection string at runtime?
提问by Vigneshwaran
I created my vb.net project to .exe file. During installation on another machine, one can change the location of installing package to any drive. In my project, i have set my app.config to point the database that is available in c:\project. If i suppose while installing, when i change the location of installation to d:\ or anywhere, i was getting invalid access db. What i want is, i have to reconfigure my app.config file automatically, by detecting its current db location.
我将我的 vb.net 项目创建为 .exe 文件。在另一台机器上安装期间,可以将安装包的位置更改为任何驱动器。在我的项目中,我已将 app.config 设置为指向 c:\project 中可用的数据库。如果我在安装时假设,当我将安装位置更改为 d:\ 或任何地方时,我将无法访问 db。我想要的是,我必须通过检测其当前的数据库位置来自动重新配置我的 app.config 文件。
I have no clue about how to do this.. Please i need your precious help doing this possible. Thanks.
我不知道如何做到这一点.. 请我需要你的宝贵帮助才能做到这一点。谢谢。
回答by Carlos Landeras
Imports System.Configuration
Imports System.Configuration.ConfigurationManager
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.ConnectionStrings.ConnectionStrings("MyProject.Properties.Settings.MyProjectConString").ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\SampleDB;Persist Security Info=True;"
config.Save(ConfigurationSaveMode.Modified)
Where MyProject.Properties.Settings.MyProjectConStringis the name of your project and connection string.
MyProject.Properties.Settings.MyProjectConString您的项目名称和连接字符串在哪里。
回答by Mohammad Zulfikar
Although this is too late to answer as the question is very old but I think this might help someone else in the future.
虽然现在回答已经太晚了,因为这个问题已经很老了,但我认为这可能会在未来对其他人有所帮助。
So, there is a way to change the Connection String value in the runtime. Since connection string is a read-onlyitem like all other items that are on Application Scope under My.Settingsso we can't change it using My.Setting.ConnectionString = "Something". But here is the code by which you can solve the issue and change any My.Settingsitem that is on Application Scope in the runtime.
因此,有一种方法可以在运行时更改连接字符串值。由于连接字符串与My.Settings下的 Application Scope 上的所有其他项目一样是只读项目,因此我们无法使用. 但这里是代码,您可以通过它解决问题并更改运行时中 Application Scope 上的任何My.Settings项目。My.Setting.ConnectionString = "Something"
So the code is,
所以代码是,
My.Settings.Item("ConnectionString") = "Something"
回答by athiq bari
simple...
简单的...
MsgBox(My.Settings.Item("remoteAddress").ToString)
My.Settings.Item("remoteAddress") = "abcserver.servebbs.net"
My.Settings.Save()
MsgBox(My.Settings.Item("remoteAddress").ToString)
回答by Jonathan Ismaila
You have three options: 1.) Create and use a folder in C:\Databse and set your connection string at design time.
您有三个选项: 1.) 在 C:\Databse 中创建和使用一个文件夹,并在设计时设置您的连接字符串。
2.)Add the database to the project's data source at design time, then use '|Data Directory|\mydb.mdb' as your connection string.
2.) 在设计时将数据库添加到项目的数据源中,然后使用“|Data Directory|\mydb.mdb”作为连接字符串。
3.) And if you use sqlserver, you don't need to worry about the location of the database once you have attached the database to sqlserver. You only need to use the proper connection string eg 'Data Source=.; Database = mydb; Integrated Security = False; Username=myusername; Password = mypassword; User Instance = false'. The above is an example of a sql server with SQL Authentication mode as login, if you use Windows Authentication, set Integrated Security = True and remove both username and password.
3.) 如果您使用 sqlserver,一旦您将数据库附加到 sqlserver,您就无需担心数据库的位置。您只需要使用正确的连接字符串,例如 'Data Source=.; 数据库 = mydb; 综合安全=假;用户名=我的用户名;密码 = 我的密码;用户实例 = false'。上面是一个sql server 以SQL Authentication 模式作为登录的例子,如果你使用Windows Authentication,设置Integrated Security = True 并删除用户名和密码。

