HighTechTalks DotNet Forums  

Changing Connection String Dynamically

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Changing Connection String Dynamically in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Ed
 
Posts: n/a

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


Reply With Quote
  #2  
Old   
Hillbilly
 
Posts: n/a

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

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

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

}
#endregion




"Ed" <abcde (AT) nospam (DOT) nospam> wrote

Quote:
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.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.