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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best place to store my database connection string in C#?

Status
Not open for further replies.

Robert1000

Programmer
Feb 25, 2004
4
0
0
AU
Hi

Firstly hello, im new to this forum

Anyways I've also fairly new to C# although have been programming in various other languages and web languages for the past few years ... java, asp(jscript), vb, php etc etc.

Im working on my first web service application for a client and i've structured it so that the webservice calls my class library which in turn adds relevant database entries. Im just wondering where i can store my db connect string globally so that i can access it from all the classes within my class library instead of declaring the string as a private const at the top of each library.

Also, have i designed this correctly? should i be using a class library? At the moment all i have is a validation(login/session) class and another class which handles adding/deleting of records

Cheers for any help
Rob
 
Use the 'web.config' (.Config files are not served up by web servers) file to store the strings in an 'appSettings' section, and pull them in from a connections class to build a database connection object, (E.G., SqlConnection, OracleConnection etc).


The config file is not compiled either, it's an XML file so can be simply edited by yourself or an administrator to change the 'appSetting' element without having to do anything to your code.

Rhys
"When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas
"As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates
 
The web.service file is part of asp.net though right?

Say the class library wasn't being called from a web service at all, where would i store the connection string then? I'd really prefer if the connection string was associated with the class library rather than the web service which calls it

this might be where i going about it all wrong though?

Is there a standard place strings like this are stored in c#? I seem to remember vb used a .res file to store this kind of thing, apart from the registry.
 
You can then store it in an "app.config" file. Again, simple XML formatted file. Only caveat is that you can't write to them without doing some work. Reading is easy, though -- the framework has classes for it.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Can't you make a global class where you define your global variables like:

Code:
using System;

namespace YourNameSpace
{

	public class Globals
	{
		// SQLConnectionString
		public static string SqlConnectionString = "";
	}
}

Then you can call this Global using:

Globals.SqlConnectionString;

- Raenius

"Free will...is an illusion"
 
The trouble with putting it in the code is if you need to change it, you have to drop new code.

Which might be OK for some applications... I guess.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top