Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态更改SQL Server数据库连接_C#_Sql_Sql Server_Database_Connection String - Fatal编程技术网

C# 动态更改SQL Server数据库连接

C# 动态更改SQL Server数据库连接,c#,sql,sql-server,database,connection-string,C#,Sql,Sql Server,Database,Connection String,我有一个连接类,我用它连接到我的数据库,每次我做一个选择,插入等。。。陈述我在课堂上硬编码了连接字符串 问题是,如果我必须更改数据库服务器,那么我必须更改每个应用程序上的字符串,然后重新发布 这是我的连接字符串: var cnnString = string.Format("user id=sa;" + "password=pw;server=database\\instance;" +

我有一个连接类,我用它连接到我的数据库,每次我做一个选择,插入等。。。陈述我在课堂上硬编码了连接字符串

问题是,如果我必须更改数据库服务器,那么我必须更改每个应用程序上的字符串,然后重新发布

这是我的连接字符串:

var cnnString = string.Format("user id=sa;" +
                                          "password=pw;server=database\\instance;" +
                                          "database=dbase; " +
                                          "connection timeout=10");
我曾考虑过创建一个表并将数据库信息存储在其中,但如果无法使用connection类从数据库中进行选择,则这将无济于事


是否有一种动态执行此操作的方法?

将连接字符串放入.config文件并使用ConfigurationManager类:


将所有连接字符串加载到数组/字典中,然后一旦需要使用适当的连接字符串创建连接。

您可以使用Appconfig文件

<appSettings>    
    <add key="dbserver" value="IP_SERVER" />
    <add key="dbname" value="DB_NAME" />
    <add key="dbuser" value="sa" />
    <add key="dbpass" value="PASSWORD" />
 </appSettings>
您可以使用表单来更改配置值

//打开可执行文件的App.Config

    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        config.AppSettings.Settings.Remove("dbserver");
        config.AppSettings.Settings.Remove("dbname");
        config.AppSettings.Settings.Remove("dbuser");
        config.AppSettings.Settings.Remove("dbpass");

        // Add an Application Setting.
        config.AppSettings.Settings.Add("dbserver", txtDBServer.Text);
        config.AppSettings.Settings.Add("dbname", txtDBName.Text);
        config.AppSettings.Settings.Add("dbuser", txtDBUser.Text);
        config.AppSettings.Settings.Add("dbpass", txtDBPassword.Text);
将更改保存在App.config文件中

 config.Save(ConfigurationSaveMode.Modified);
强制重新加载已更改的节

ConfigurationManager.RefreshSection("appSettings");

尝试将数据库连接字符串放在Web.config文件中,并在代码中读取。@VimalanJayaGanesh有趣的想法是,如何动态更新Web.config?为什么需要动态更新Web配置?您计划多久更换一次服务器?使用web配置的优点是连接字符串信息是一个位置。然后,当您需要更改它时,您可以更改web配置,并且您的所有代码都使用新信息。@Steve感谢您的链接!
ConfigurationManager.RefreshSection("appSettings");