Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Database connections

Status
Not open for further replies.

johnrg1

Programmer
Jan 24, 2003
38
0
0
GB
In ASP, an include file could be used to hold the the database connection string, meaning changing it in the one file would mean the entire site would connect differently. is there a way to do this in ASP.NET?
At the moment i use VS.NET to create a connection, from which i drag the data onto the screen, making the connections, but each page hard codes this info into the aspx.vb page, meaning the entire site will need to be edited. if i change the location of the database.

any ideas?

Cheers
 
I think when you use the gui to generate connections and dataAdapters that you need on all pages in your app you can drag and drop the lot on the global.asax as this is a "template" for all aspx pages of that application.

Or you do not use the gui to generate connections but create a class that exposes a connection, command, dataset, dataAdapter, transaction ...



Greetings, Harm Meijer
 
Add an AppSettings area, (directly after the <configuration tag and before the <system.web> tag), to the Web.config file and store it there.
I.E.,
<CODE>

<appSettings>
<!-- Connection String -->
<!--- Development -->
<add key=&quot;ConnectString&quot; value=&quot;data source=ServerName;initial catalog=DBName;uid=LogonId;pwd=Password;Connect Timeout=600&quot; />
</appSettings>

</CODE>

Adding 'using System.Configuration;' to the top of the code behind page will let you retrieve it thus...
<CODE>
SqlConnection cnSQLLogin = new SqlConnection(ConfigurationSettings.AppSettings[&quot;ConnectString&quot;]);
</CODE>

I have a connection class which hold the above, so from any page I can instantiate a Page Level SqlConnection like this...
<CODE>
SqlConnection cnSQLLogin = Connection.Connect();
</CODE>

OK?


Rhys
Thought out... Maybe,
Opinionated... Probably
But it is only an opinion!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top