Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2005, 09:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to create a Global Database connection?

OK, I'm sure this is simple, but I'm only just beginning to play with ADO.NET.

How do I create (and use) a single database connection to use with all Adapters in my project? It's going to be really awkward to do search and replace across all the connections I've defined when it is time to a new database.

Cheers,

Chris

There are two secrets to success in this world:
1. Never tell everything you know
__________________
There are two secrets to success in this world:
1. Never tell everything you know
 
Old July 19th, 2005, 12:54 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You'll want to store the connection *string* in the application configuration file (either app.config or web.config). Your code will get the connection string from this file thru the ConfigurationSettings class. Then you can change the connection string in a single place without having to recompile any code.

-Peter
 
Old July 21st, 2005, 09:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cheers Peter,

It was a simple as I'd hoped :)

Chris

There are two secrets to success in this world:
1. Never tell everything you know
 
Old August 8th, 2005, 08:26 AM
Registered User
 
Join Date: Aug 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you write the code in web.config.
<appSettings>
<add key="ConnectionString" value="server = .;uid=sa;pwd=;database=abc"/>
</appSettings>

akd
 
Old August 16th, 2005, 09:22 AM
Registered User
 
Join Date: Aug 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Create the connection in the webconfig file

 
Old November 8th, 2006, 12:35 AM
Registered User
 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to menteriPertahanan
Default

can any one give an example after create in .config how to use it. :P
i'm sorry coz a'm very new in ado.net

 
Old November 8th, 2006, 07:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

To retrieve the connection string from the appSettings element with a key named "ConnectionString" in you web.config file use:

C#

string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

VB.NET

Dim strConnectionString As String = ConfigurationSettings.AppSettings.Get("ConnectionS tring")

Then just use the connection string variable as you normally would with ADO.NET objects:

OleDbConnection cn = new OleDbConnection(connectString);

The ConfigurationSettings class is in the System.Configuration namespace so be sure you have the proper using or Imports directive.

HTH,

Bob








Similar Threads
Thread Thread Starter Forum Replies Last Post
Global Connection Object sarahmapg ADO.NET 1 May 18th, 2005 06:00 AM
Global SQL Connection tallbry VB Databases Basics 2 November 8th, 2004 10:58 PM
how to create Global Variable VIshwastrivedi SQL Server 2000 5 September 30th, 2004 08:38 AM
Global SQL connection MichaelTJ .NET Web Services 16 December 16th, 2003 09:34 PM
global.asa and dreamweaver connection riprod Classic ASP Databases 4 October 11th, 2003 10:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.