c# - 在 VS2005 中更改 ConnectionString (C#)

标签 c# visual-studio-2005

我知道有 some similar issue .

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

最佳答案

这会帮助你!

        /// <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);       
        }

好吧,为了更新,很难找到任何有用的东西 更新连接字符串的代码。这是不可能的 更新连接字符串,所以我删除了 connectionstring,并添加一个新的。那不是很奇怪吗? 微软留下了一些未完成的东西......好吧也许它没有用 更改您的 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);
        }

关于c# - 在 VS2005 中更改 ConnectionString (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/314055/

相关文章:

c# - 在 MVC 5 View 中的可滚动列表中显示大量复选框

c++ - 如何在 Visual C++ 2005 中创建带有行号的映射文件?

visual-studio-2005 - 如何最好地处理 Visual Studio 中的巨大源代码文件

visual-studio - Visual Studio 可以告诉我打开了多少个文件吗?

c# - 从 VisualStudio 2005 升级到 2010(对于 WinForms 项目)是否容易/安全?

c# - 为什么一个基本的单线程 C# 控制台应用程序需要 3 个线程?

c# - 将 NLog 与 NServiceBus 3 一起使用

c# - 如何使用 SqlConnection.GetSchema() 获取列主键约束

c# - Stream.Write 多次或连接字符串和 Stream.Write 一次更好吗?

c# - 在 C# 中修剪 String 数组的内容