更改app.config的连接字符串值

我正在尝试更改连接字符串值

connectionString =“Data Source = localhost; Initial Catalog = Tracker; Persist Security Info = false; User ID = sa; Password = p @ ssw0rd”

从我的用户界面。 任何人都可以帮助从Windows应用程序中的用户界面更改设置吗?

从原始post的评论主题来看,听起来OP需要枚举数据源并允许用户选择一个(并且可能存储该首选项)。 假设是这样的……

  • 如果可能的话,应使用集成的Windows安全性来保护与数据库的实际连接。 这是一种最佳实践 。 集成安全性无需在任何地方存储凭据。

  • 如果目标是选择数据源,则使用C#列出环境中的所有数据库并不困难。

  • 用户的偏好/选择应存储在app.config以外的其他位置。 如果不涉及凭据,则注册表,隔离存储或XML文件都是有效选项。

ASP.NET提供了网站管理工具来查看和管理ASP.NET网站的配置设置。 这些配置设置存储在名为web.config的xml文件中。

web.config是一个应用程序配置文件,用于为ASP.NET应用程序定义配置设置,如连接字符串,身份validation,授权等

该工具提供了一个易于使用的界面来编辑配置设置。 但是,当您必须手动进行更改时,此工具可以正常工作。 有时,我们可能需要在运行时对web.config进行更改。 例如:用户请求控制连接字符串,更改会话超时等。 为此,ASP.NET提供了配置API,以编程方式操作web.config中的配置设置。 API包含在System.ConfigurationSystem.Web.Configuration命名空间中。 实际上,网站管理工具内部使用相同的API来编辑配置设置。 System.Web.Configuration命名空间中的WebConfigurationManager类用于创建Configuration对象。 此对象用于读取和写入对web.config文件的更改。 见 。

另请参阅保护连接字符串

让我澄清一下,这不是你所要求的,但通过阅读本文,你将能够找到一些线索,并了解推荐的内容和不推荐的内容。

如果这是一个Windows应用程序,则无需更改app.config文件即可更改活动数据库连接。 我在博客上写过这篇文章 。

用户在下面使用C#更改连接字符串的方法:

public void SaveConnectionString(DbInfo dbinfo, string path,string appConfigFile) { try { string configFile = Path.Combine(path, appConfigFile); var doc = new XmlDocument(); doc.Load(configFile); XmlNodeList endpoints = doc.GetElementsByTagName("connectionStrings"); foreach (XmlNode item in endpoints) { if (item.HasChildNodes) { foreach (var c in item.ChildNodes) { if (((XmlNode)c).NodeType == XmlNodeType.Element) { var adressAttribute = ((XmlNode)c).Attributes["name"]; if (adressAttribute.Value.Contains("YourConStrName")) { if (dbinfo.dbType == dataBaseType.Embedded) { ((XmlNode)c).Attributes["connectionString"].Value = SetupConstants.DbEmbededConnectionString; ((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbEmbededConnectionProvider; } else if (dbinfo.dbType == dataBaseType.Microsoft_SQL_Server) { if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.SQL_Server_Authentication) { // ((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, SetupConstants.SqlDbName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;"; ((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, dbinfo.DatabaseName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;"; } else if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.Windows_Authentication) { //((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, SetupConstants.SqlDbName); ((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, dbinfo.DatabaseName); } ((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbSqlConnectionProvider; } } } } } } doc.Save(configFile); string exePath = Path.Combine(path, "EDC.Service.exe"); InstallerHelper.EncryptConnectionString(true, exePath); } catch (Exception ex) { //TODO://log here exception Helper.WriteLog(ex.Message + "\n" + ex.StackTrace); throw; } } 

添加下面的类DBinfo

 public class DbInfo { public DataBaseType dbType { get; set; } public SqlServerAuthenticationType sqlServerAuthType { get; set; } public string ConnectionString { get; set; } public string databaseAdress { get; set; } public string userId { get; set; } public string password { get; set; } public string Port { get; set; } public string DatabaseName { get; set; } } public enum DataBaseType { Unknown = 0, Embedded = 1, Microsoft_SQL_Server =2, } public enum SqlServerAuthenticationType { Windows_Authentication = 0 , SQL_Server_Authentication =1 }