Changing Connection String Dynamically - 05-15-2009 , 02:24 PM
I have an ASP.NET application with a lot of generated code for
SQL database access. I would like to change the SQL connection
String, dynamically at runtime (not in web.config). Is there a way that
I can override in code the connectionString that is in web.config.
Where would I place this code to insure that it is acessable throughout
the application. I do not want to write back to web.config, since that
would cause an appliction restart. Any sample code showing how to
do that would be greatly appreciated ( I prefer VB.NET, but will look at
C# if that's what you got.
Thanks in advance...Ed B.
Re: Changing Connection String Dynamically - 05-15-2009 , 03:09 PM
We define one or more connection strings in the web.config file and then we
use a method such as the following GetConnectionString method which returns
a connection string to you whenever you want.
If you really want to clean it up and make it more secure you can learn to
use another .config file for your secured connection strings and that too
has something to do with web.config. You learn how to do it in web.config.
Here's a method for you to convert into that other stuff...
// Method returns a connection string as defined in web.config
public string GetConnectionString(string connectionString)
// Get the connectionStrings section.
connectionStringsSection = WebConfigurationManager
// Get the connectionStrings[key] or value pairs collection.
ConnectionStringSettingsCollection connectionStrings =
return connectionStrings["" + connectionString +
"Ed" <abcde (AT) nospam (DOT) nospam> wrote
|Thread Tools||Search this Thread|