2012-01-07 118 views
1

我试图改变连接字符串值更改连接字符串值在app.config

的connectionString =“数据源=本地主机;初始目录=跟踪;坚持安全信息= FALSE;用户ID = SA ;密码= p @ ssw0rd“

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

+4

你想要的应用程序来更新它自己的app.config文件?从应用程序本身重写应用程序的配置文件不是一个好主意。 – 2012-01-07 05:23:21

+0

感谢您的回复。 – user735627 2012-01-07 05:24:07

+2

借调。这不是一个好策略。如果您需要创建动态连接字符串,则可能不希望将它们存储在app.config文件中。加密到文件或注册表是我推荐的。 – 2012-01-07 05:24:59

回答

1

从原始帖子的评论主题,听起来像OP需要枚举数据源并允许用户选择一个(也许存储该首选项)。假设情况如下...

  • 如果可能的话,应该使用集成的Windows安全保护到数据库的实际连接。这是一个best practice。集成的安全性消除了在任何地方存储凭据的需求。

  • 如果目标是选择一个数据源,使用C#列出所有databases within an environment并不困难。

  • 用户的偏好/选择应该存储在除app.config之外的其他地方。如果不涉及凭证,注册表,独立存储或XML文件都是有效的选项。

1

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

的web.config是用于定义配置设置,如连接字符串,认证,授权等应用程序配置文件,ASP.NET应用程序

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

Securing Connection Strings

见让我澄清一下,这是你问的不是,但通过阅读这一点,你就能够找到一些线索,来了解建议是什么,什么是不。

1

如果这是一个Windows应用程序,那么您不需要更改app.config文件以更改活动数据库连接。我已经写了关于这个on my blog。以下方法

0

用户使用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 
} 
相关问题