简体   繁体   English

动态更改SQL Server数据库连接

[英]Change SQL Server Database Connection Dynamically

I have a connection class that I use to connect to my database, every time I do a select, insert, etc... statement. 每当执行select,insert等语句时,我都有一个连接类可用于连接数据库。 I have the connection string hard coded in the class. 我在类中对连接字符串进行了硬编码。

The issue with this is, if I have to change database servers, then I have to change the string on every application, and republish. 问题是,如果必须更改数据库服务器,则必须更改每个应用程序上的字符串,然后重新发布。

Here is my connection string: 这是我的连接字符串:

var cnnString = string.Format("user id=sa;" +
                                          "password=pw;server=database\\instance;" +
                                          "database=dbase; " +
                                          "connection timeout=10");

I thought about creating a table and storing the database info in there, but that doesn't help if can't use the connection class to select from the database. 我曾考虑过创建一个表并在其中存储数据库信息,但是如果不能使用连接类从数据库中进行选择,那将无济于事。

Is there a way of doing this dynamically? 有没有办法动态地做到这一点?

Place your connection strings in .config file and use ConfigurationManager class: https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager%28v=vs.110%29.aspx 将连接字符串放在.config文件中,并使用ConfigurationManager类: https : //msdn.microsoft.com/zh-cn/library/system.configuration.configurationmanager%28v=vs.110%29.aspx

Load all you connection strings into array / Dictionary then once you need to create connection using the appropriate connection string. 将所有连接字符串加载到数组/字典中,然后在需要使用适当的连接字符串创建连接时进行加载。

You can use your Appconfig file 您可以使用您的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>

then use this to create the string conection 然后用它来创建弦锥

string strConexion = "Data Source='" + ConfigurationManager.AppSettings["dbserver"] + "';" +
                                "Initial Catalog='" + ConfigurationManager.AppSettings["dbname"] + "';" +
                                "User Id='" + ConfigurationManager.AppSettings["dbuser"] + "';" +
                                "Password='" + ConfigurationManager.AppSettings["dbpass"] + "';";

And you can use a form to change the configuration values using this 您可以使用此表单来更改配置值

// Open App.Config of executable //打开可执行文件的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);

Save the changes in App.config file. 将更改保存在App.config文件中。

 config.Save(ConfigurationSaveMode.Modified);

Force a reload of a changed section. 强制重新加载已更改的部分。

ConfigurationManager.RefreshSection("appSettings");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM