0

I'm trying to overwrite some values in a few config files on a MVC solution. I'm doing something like this:

 config.AppSettings.Settings["Key"].Value =newValue;
 config.Save(ConfigurationSaveMode.Modified);

My question is, how to do something similar for ConnectionString file?. I'm trying with this code.

 System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
    builder["InitialCatalog"] = newValue;
6
  • SqlConncetionStringBuilder has a ConnectionString property that could be modified. Are you after this one? Or you want to update the ConnectionString in the config file? Commented Jan 18, 2016 at 22:58
  • what I want is to select the name of a database, updated values on ​​connectionString file and then recompile the solution with the new values ​​,I do not know if is the best way to select multiple databases for a project , it was only what occurred to me to do Commented Jan 18, 2016 at 23:13
  • yes I called ConnectionString to my configuration file , sorry for not be so specific. Commented Jan 18, 2016 at 23:22
  • Can you give me some more details about what you are doing? Why cannot you add your connectionstrings in the configuration file, say app.config? Commented Jan 18, 2016 at 23:39
  • Yeah,the connectionString is in .config file the problem is that I have one solution that should to be tested with different databases. Actually for do that we change the values on connectionString manually so... I want to find a way for do that on the system and the way that I trying to do is select a value in dropdown , save the value and update the database name on config files that I need , I´m not really sure if is the best way Commented Jan 18, 2016 at 23:54

1 Answer 1

1

There could be several ways you could achieve this. One easy way would be to create a code that could dynamically determine the connectionstring based on user selection from dropdown and using a predefined sets of ConnectionString you have in your config file. Lets say you have defined following connectionstrings,

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <add name="Development" connectionString="data source=MyDataSource;Initial Catalog=DevDatabase;user id=sa;Pwd=devpassword;"/>
    <add name="Test" connectionString="data source=MyDataSource;Initial Catalog=TestDatabase;user id=sa;Pwd=testpassword;"/>
  </connectionStrings>
</configuration>

Now you can have following code to loop through the connectionstring and determine which one should be used based on the dropdown selection

string selectedEnvironemntName = "Development"; //set from dropdown selection
var predefinedConnections = ConfigurationManager.ConnectionStrings;
SqlConnectionStringBuilder connStringBuilder = null;

foreach( ConnectionStringSettings connString in predefinedConnections)
{
    if (connString.Name == selectedEnvironemntName )
    {
        connStringBuilder = new SqlConnectionStringBuilder(connString.ConnectionString);
    }
}

//the below two lines can be anywhere in your solution as long as you can pass the connStringBuilder to SqlConnection definition
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connStringBuilder.ConnectionString;

Ofcourse, one dependency is there. You have to ensure that the dropdown values are loaded based on the ConnectionString name from the config file. Having this dependency ensures that adding a new ConnectionString in your config file will automatically result in having a new entry in your dropdown in UI

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

1 Comment

The thing is that I have different connectionString names for different providers, and I don´t would like to add to much connectionsString because I´m not sure if I can affect the solution for change that values names. Is a solution with different projects that use that files thats what I want to change just name of the database ,but probably is the best thing to add more connections for each environment, thanks for your help.

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.