PC Review


Reply
Thread Tools Rate Thread

Changing Connection String Dynamically

 
 
Ed
Guest
Posts: n/a
 
      15th May 2009
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
 
 
 
 
Hillbilly
Guest
Posts: n/a
 
      15th May 2009
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" <(E-Mail Removed)> wrote in message
news:A307E6D5-15B0-4BE8-A928-(E-Mail Removed)...
>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
 
Paul
Guest
Posts: n/a
 
      27th May 2009
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.



"Ed" <(E-Mail Removed)> wrote in message
news:A307E6D5-15B0-4BE8-A928-(E-Mail Removed)...
>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
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trimming a dynamically changing string Eli Microsoft Access 2 13th Dec 2009 06:06 AM
Changing a control property using a dynamically created string Rick Schneider Microsoft Access 2 12th Oct 2009 09:20 PM
Manage connection string dynamically? Hillbilly Microsoft ADO .NET 2 30th Sep 2008 03:24 AM
change connection string dynamically sam44 Microsoft ASP .NET 37 29th Jul 2006 09:24 PM
How to dynamically change the connection string John Microsoft Access External Data 0 13th Jul 2004 05:50 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:19 PM.