2

I have a connection class that I use to connect to my database, every time I do a select, insert, etc... statement. 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?

6
  • 1
    Try to put the database connection string in Web.config file and read it in your code. Commented May 27, 2015 at 18:44
  • @VimalanJayaGanesh interesting thought, how would I update the web.config dynamically? Commented May 27, 2015 at 18:45
  • Why do you need to update the web config dynamically? How often and frequently are you planning on changing servers? The advantage of using your web config is that the connection string information is ONE location. Then when you need to change it, you change the web config and all your code uses the new information. Commented May 27, 2015 at 18:50
  • 2
    Change a web.config programmatically with C# (.NET) Commented May 27, 2015 at 18:51
  • Just some pointers, when I used to embed a connection string in a web.config, it was a best practice then to encrypt it (see How to Encrypt connection string in web.config ). Also, changing your web.config will recycle your app pool, so it's best to do during off-hours if you can. Commented May 27, 2015 at 19:01

2 Answers 2

2

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

Load all you connection strings into array / Dictionary then once you need to create connection using the appropriate connection string.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use your Appconfig file

<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

    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.

 config.Save(ConfigurationSaveMode.Modified);

Force a reload of a changed section.

ConfigurationManager.RefreshSection("appSettings");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.