Changing Connection String Dynamically

E

Ed

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.
 
H

Hillbilly

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
connectionStringsSection = WebConfigurationManager
.GetSection("connectionStrings")
as ConnectionStringsSection;

// Get the connectionStrings[key] or value pairs collection.
ConnectionStringSettingsCollection connectionStrings =
connectionStringsSection.ConnectionStrings;

return connectionStrings["" + connectionString +
""].ConnectionString.ToString();

}
#endregion
 
P

Paul

Why?

Should you not be splitting you DAL to represent the different databases you
are talking to or seperating out distinct elements of functionality and
letting them interact with each other.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top