如何从 VB.NET Windows 应用程序修改 app.config 文件中的 ConnectionString?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13950565/
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 to modify ConnectionString in app.config file from VB.NET windows application?
提问by Ljupco Sofijanov
I want to change the connection string of my application written in app.config file. I like to make changes in the connection string directly from my application.
我想更改在 app.config 文件中编写的应用程序的连接字符串。我喜欢直接从我的应用程序更改连接字符串。
My app.config looks like:
我的 app.config 看起来像:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
<connectionStrings>
<add name="ApplicationName.My.MySettings.ConnectionS"
connectionString="Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="Test" value="/" />
</appSettings>
</configuration>
I am able to make changes in appSettingswith this code:
我可以使用以下代码在appSettings 中进行更改:
Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.Settings("Test").Value = "New value"
But I am not able to make changes in connectionStringswhile debuging nither when the application is installed:
但是我无法在安装应用程序时调试 nither 时更改connectionStrings:
Dim config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim connectionStringsSection = DirectCast(config.GetSection("connectionStrings"), ConnectionStringsSection)
connectionStringsSection.ConnectionStrings("ApplicationName.My.MySettings.ConnectionS").ConnectionString = "New connection string"
config.Save()
ConfigurationManager.RefreshSection("connectionStrings")
回答by Cubsoft
To change it in code try something like the following;
要在代码中更改它,请尝试以下操作;
Dim con1 = ConfigurationManager.ConnectionStrings("connection1")
Dim con2 = ConfigurationManager.ConnectionStrings("connection2")
The 'connection1' and 'connection2' are the names of the connection strings within the app.config.
'connection1' 和 'connection2' 是 app.config 中连接字符串的名称。
回答by mustaphahawi
Dim c As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location)
Dim section As ConnectionStringsSection = DirectCast(c.GetSection("connectionStrings"), ConnectionStringsSection)
section.ConnectionStrings("Services.My.MySettings.hassanConnectionString").ConnectionString = createCnxString()
c.Save()
Private Function createCnxString() As String
Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder
builder("Data Source") = datasourceTxt.Text
builder("Integrated Security") = True
builder("Initial Catalog") = "XXXX"
builder("User ID") = usernameTxt.Text
builder("Password") = passwordTxt.Text
Return builder.ConnectionString
End Function

