【问题标题】:Change ConnectionString in VS2005 (C#)在 VS2005 (C#) 中更改 ConnectionString
【发布时间】:2008-11-24 12:40:12
【问题描述】:

我知道有some similar issue

但是,问题仍然没有回答!我需要修改连接字符串而不是添加新的。

【问题讨论】:

  • 请更具体。您能解释一下为什么要更改字符串吗?
  • 好吧,想象一下这种情况。您有一个支持多个公司的业务应用程序,每个公司都有不同的数据库,因此,当用户登录系统时,他/她必须先选择数据库。这就是我需要更改数据库文件的原因。
  • 同样,如果您的应用程序是客户端,并且您连接到驻留在一台服务器上的服务器应用程序,则需要在运行时更改连接字符串,以便连接到服务器应用程序

标签: c# visual-studio-2005


【解决方案1】:

这将帮助你!

        /// <summary>
        /// Add a connection string to the connection
        /// strings section and store it in the
        /// configuration file. 
        /// </summary>
        /// <param name="csName">The name of the property.</param>
        /// <param name="connectionString">The connectionstring as specified.</param>
        public static void AddConnectionStrings(string csName, string connectionString)
        {

            // Get the configuration file.
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

            // Add the connection string.
            ConnectionStringsSection csSection = config.ConnectionStrings;
            csSection.ConnectionStrings.Add(
                new ConnectionStringSettings(csName,
                    connectionString, "System.Data.SqlClient"));

            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Full);       
        }

为了更新,很难找到任何有用的东西 更新连接字符串的代码。这是不可能的 更新连接字符串,所以我删除了 连接字符串,并添加一个新的。那是不是很奇怪? 微软留下了一些未完成的东西......好吧也许它没有用 更改您的 app.config,但是无论如何,我都必须做到。 所以动态 app.config 或 web.config 文件有点奇怪,因为你不能动态更新它。不,您必须先由配置管理器将其删除。

        /// <summary>
        /// First remove the old connectionstring and after that
        /// add a connection string to the connectionstrings
        /// section and store it in the configuration file. 
        /// </summary>
        /// <param name="csName">The name of the property.</param>
        /// <param name="connectionString">The connectionstring as specified.</param>
        public static void UpdateConnectionStrings(string csName, string connectionString)
        {
            // Get the configuration file
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

            // Remove the existing connectionstring.
            config.ConnectionStrings.ConnectionStrings.Remove(csName);
            // Add the connectionstring
            ConnectionStringsSection csSection = config.ConnectionStrings;
            csSection.ConnectionStrings.Add(
                new ConnectionStringSettings(csName, 
                connectionString, "System.Data.SqlClient"));

            // Save the configuration file
            config.Save(ConfigurationSaveMode.Full);
        }

【讨论】:

    【解决方案2】:

    这是一个老生常谈的问题,我想知道最终为您解决了什么问题。如果您还没有解决,this question 会尝试回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      相关资源
      最近更新 更多