C# 更改VS2005(C)中的连接字符串

C# 更改VS2005(C)中的连接字符串,c#,visual-studio-2005,C#,Visual Studio 2005,我知道有 但是,这个问题仍然没有得到回答!我需要修改连接字符串,而不是添加新字符串。这将帮助您解决问题 /// <summary> /// Add a connection string to the connection /// strings section and store it in the /// configuration file. /// </summary>

我知道有


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

这将帮助您解决问题

        /// <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,我必须删除 connectionstring,并添加一个新的。那不是很奇怪吗?? 微软没有做什么。。。也许这没用 要更改你的app.config,但无论如何,我必须这样做。 所以动态app.config或web.config文件有点奇怪,因为您无法动态更新它。不,您必须先通过configurationmanager将其删除

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

这会帮你的

        /// <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,我必须删除 connectionstring,并添加一个新的。那不是很奇怪吗?? 微软没有做什么。。。也许这没用 要更改你的app.config,但无论如何,我必须这样做。 所以动态app.config或web.config文件有点奇怪,因为您无法动态更新它。不,您必须先通过configurationmanager将其删除

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

这是一个老生常谈的问题,我只想知道什么样的解决方案最终适合你们。如果您还没有解决,请尝试回答。

这是一个老生常谈的问题,我只想知道什么解决方案最终适合您。如果您尚未解决问题,请尝试回答。

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