【问题标题】:change the connection string values the app.config更改 app.config 的连接字符串值
【发布时间】:2017-03-31 15:48:45
【问题描述】:

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

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

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

【问题讨论】:

  • 您希望应用程序更新它自己的 app.config 文件吗?从应用程序本身重写应用程序的配置文件不是一个好主意。
  • 第二。这不是一个好策略。如果您需要创建动态连接字符串,您可能不想将它们存储在 app.config 文件中。我建议加密到文件或注册表。
  • 我只想从窗口更改 app.config 文件中的数据源。我想将用户的输入作为数据源。
  • 这不是一个好主意。尝试将连接首选项存储在数据库或另一个文件中。
  • 我认为您需要输入从一台服务器到另一台服务器的某种数据迁移,或与此相关的一些东西。不确定..如果你想实现这一点,你可以从你的代码本身打开连接..使用SQLConnection

标签: c#


【解决方案1】:

从原始帖子的评论线程中,听起来 OP 需要枚举数据源并允许用户选择一个(并可能存储该偏好)。假设是这样......

  • 应尽可能使用集成的 Windows 安全性来保护与数据库的实际连接。这是best practice。集成的安全性无需在任何地方存储凭据。

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

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

【讨论】:

    【解决方案2】:

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

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

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

    另见Securing Connection Strings

    让我澄清一下,这不是您要问的,但通过阅读本文,您将能够找到一些线索,并了解什么是推荐的,什么不是。

    【讨论】:

      【解决方案3】:

      如果这是一个 Windows 应用程序,那么您无需更改 app.config 文件即可更改活动的数据库连接。我已经写过这个on my blog

      【讨论】:

        【解决方案4】:

        用户使用以下方法使用 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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-27
          • 2012-04-24
          • 2013-02-01
          • 2012-04-30
          • 1970-01-01
          • 1970-01-01
          • 2023-04-04
          • 2011-07-07
          相关资源
          最近更新 更多