-1

i have an app.config file that has a connection string for my database. what i want to do is to connect to different databases that's why i used to this code:

 connectionString = "Data Source=blah;Initial Catalog=blah;UID=blah;password=blah";
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
        connectionStringsSection.ConnectionStrings[nameofConnectionString].ConnectionString = connectionString;
        config.Save();
        ConfigurationManager.RefreshSection(nameofConnectionString);

it works well, it changed the connection string, but the problem is that it sends me an error saying "facerec6.0.cdcol does not exist"

my default initial catalog = facerec6.0

what will i do ?

2 Answers 2

0

The ConnectionStringSection is just a container to store named connection strings. If you want to connect to different databases then it's best to start by storing those different connection strings in that section from the start and then determining how your application will choose which one to use at runtime.

Think of that section as just that and nothing more - it's a convenient, known place to store connection strings with a standard way to retrieve them. Other developers working on the code will know where to look for them and know how to retrieve them.

Even though it's technically possible to modify that section at runtime and save the file I wouldn't do that. If you have the same code that may use different connection strings while running in the same environment (it's not a case of one for development, one for QA, and one for production) then you could have your class depend on an interface something like this:

public interface IConnectionStringFactory
{
    string GetConnectionString(Something key);
}

Where Something is a value that the class requiring the connection string can pass to the factory, and the factory can use it to determine which connection string to retrieve. That way the class that uses the connection string is insulated from that logic. It doesn't know why it uses one connection string or another. It just gets a connection string from the factory and then uses it.

If it's a case of varying connection strings by environment then that's much, much easier - you can do that with config transforms. In most cases if it's a different environment then all the connection strings will be different for each environment, so you can just replace the whole section.

<connectionStrings xdt:Transform="Replace">
    <add name="connectionStringA" connectionString="...whatever..." />
</connectionStrings>
Sign up to request clarification or add additional context in comments.

1 Comment

If that's the answer please don't forget to mark it as answered. Thanks
0

Please try this instead:

ConfigurationManager.RefreshSection("connectionStrings");

because:

<connectionStrings> <- this is the section
   <add name="facerec6.0"/> <- this is the element
   <add ... />
</connectionStrings>

You need to refresh the section, not the element, when using RefreshSection.

2 Comments

i already did what you say but it gives me an Exception "table 'facerec6.0.cdcol does not exist'"
I think that is a different error. Can you check if 'facerec6.0.cdcol exists in the connection string section of your configuration file?

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.