在C#中运行时读取,写入和更新app.config文件中的connectionstring

时间:2017-03-21 04:58:41

标签: c# winforms connection-string app-config

我已经为连接设置创建了一个表单,用户可以在其中更新服务器名称,数据库以及用户ID和密码。我已将连接字符串存储在app.config文件中。

我的问题是,如何在运行时更新app.config文件中的连接字符串?如何在第一次通过表单上的文本框更改连接字符串的信息,文本框将显示信息第一次使用后的服务器名称,ID,密码

这是我的app.config

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Data123.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>   
    </configSections>
  <connectionStrings>
   <add name="Data123Entities" connectionString="metadata=res://*/Data123DB.csdl|res://*/Data123DB.ssdl|res://*/Data123DB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=nguyenduyhai;initial catalog=Data123;persist security info=True;user id=sa;password=1234567;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <!--<add name="Data123Entities" connectionString="metadata=res://*/Data123DB.csdl|res://*/Data123DB.ssdl|res://*/Data123DB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=NGUYENDUYHAI\SQLEXPRESS;initial catalog=Data123;persist security info=True;user id=sa;password=1234567;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />-->
  </connectionStrings>
</configuration>

这里是我正在尝试但无法正常工作的代码,请帮助我

     void saveconect(string address,string id,string pass)
{

            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.ConnectionStrings["Data123Entities"].ConnectionString = "data source=" + address + ";initial catalog=" + "Data123" + ";user id=" + id + ";password=" + pass + "";
            config.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection("connectionStrings");
}




 private void buttonUpdate_Click(object sender, EventArgs e)
    {


        saveconect(textboxServerAddress.Text, textBoxUserID.Text, textBoxPassword.Text);
    }

2 个答案:

答案 0 :(得分:0)

您是否尝试在发布文件夹中运行应用程序? 因为在调试模式下不会改变。

答案 1 :(得分:0)

使用 System.Xml.Linq; ,它将类似于:

var doc = XElement.Load(fileName); 
var target = doc.Element("configuration").Elements("configurationStrings").Where(e => e.Element("name").Value == "Data123Entities").Single(); 
target.Element("connectionString").Value = "metadata=res://*/Data123DB.csdl|res://*/Data123DB.ssdl|res://*/Data123DB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=" + dataSource + ";initial catalog=" + initCatalog + ";persist security info=True;user id=sa;password=1234567;MultipleActiveResultSets=True;App=EntityFramework&quot;"
doc.Save(fileName);