如何在 C# 中更新 app.config 连接字符串数据源值?

发布于 2024-12-01 04:26:44 字数 991 浏览 2 评论 0原文

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["MyConnectionString",String.Format("DataSource={0};")].ConnectionString=textBox1.Text;
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");

我在第二行遇到了麻烦。我似乎无法得到正确的语法。如您所见,我只想更新数据源值。例如,如果当前值为 Data Source=PC001\SQL2008EXPRESS,我希望将其更新为客户端在 textBox1 中输入的内容。

编辑:示例 ConnectionString

<add name="ERPDatabaseTables" connectionString="metadata=res://*/ERPTables.csdl|res://*/ERPTables.ssdl|res://*/ERPTables.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=PC001\SQL2008EXPRESS;Initial Catalog=MyDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

因此只想更新 Data Source=PC001\SQL2008EXPRESS 部分

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["MyConnectionString",String.Format("DataSource={0};")].ConnectionString=textBox1.Text;
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");

I'm having trouble at line two. I cant seem to get the syntax correct. As you can see, i only want to update the DataSource value only. For example, if current value is Data Source=PC001\SQL2008EXPRESS, i want it to be updated to what the client enters in textBox1.

EDIT: Example ConnectionString

<add name="ERPDatabaseTables" connectionString="metadata=res://*/ERPTables.csdl|res://*/ERPTables.ssdl|res://*/ERPTables.msl;provider=System.Data.SqlClient;provider connection string="Data Source=PC001\SQL2008EXPRESS;Initial Catalog=MyDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>

so want to update Data Source=PC001\SQL2008EXPRESS portion only

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2024-12-08 04:26:44

您真正想要的是:

Configuration config = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Because it's an EF connection string it's not a normal connection string
// so we pull it into the EntityConnectionStringBuilder instead
EntityConnectionStringBuilder efb = 
    new EntityConnectionStringBuilder(
        config.ConnectionStrings.ConnectionStrings["ERPDatabaseTables"]
            .ConnectionString);

// Then we extract the actual underlying provider connection string
SqlConnectionStringBuilder sqb = 
    new SqlConnectionStringBuilder(efb.ProviderConnectionString);

// Now we can set the datasource
sqb.DataSource = textBox1.Text;

// Pop it back into the EntityConnectionStringBuilder 
efb.ProviderConnectionString = sqb.ConnectionString;

// And update...
config.ConnectionStrings.ConnectionStrings["ERPDatabaseTables"]
    .ConnectionString = efb.ConnectionString;

config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");

这假设:

  1. 您的 EF 连接字符串存在于应用程序的配置文件中

  2. 您有对 System.Data 的引用.Entity

What you actually want is:

Configuration config = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Because it's an EF connection string it's not a normal connection string
// so we pull it into the EntityConnectionStringBuilder instead
EntityConnectionStringBuilder efb = 
    new EntityConnectionStringBuilder(
        config.ConnectionStrings.ConnectionStrings["ERPDatabaseTables"]
            .ConnectionString);

// Then we extract the actual underlying provider connection string
SqlConnectionStringBuilder sqb = 
    new SqlConnectionStringBuilder(efb.ProviderConnectionString);

// Now we can set the datasource
sqb.DataSource = textBox1.Text;

// Pop it back into the EntityConnectionStringBuilder 
efb.ProviderConnectionString = sqb.ConnectionString;

// And update...
config.ConnectionStrings.ConnectionStrings["ERPDatabaseTables"]
    .ConnectionString = efb.ConnectionString;

config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");

This presumes:

  1. Your EF connection string exists in the app's config file

  2. You have a reference to System.Data.Entity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文