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

How to store connection string and then call it from C#

Status
Not open for further replies.

dnjacks

MIS
Feb 23, 2004
4
US
Okay...I am starting my first C#/ASP.Net webpage and have it a stump off the bat. I want to store my connection string in the Webconfig file and then call it from code. I am connecting to an Access DB and using C#. The examples I have seen on configuring the Webconfig file does not address the provider portion of the connection string. Also, I want to use server.mappath for my database location. Any thoughts on how to accomplish this or do you all have any best practices to share?
 
Well until you get the global thing figured out... at the top of the page you can declare a Page variable with the con string...

Imports System.Web

string myConn = (bla bla bla)

void page_load
connect with myConn

void fill_DDL
connect with myConn


for global, i havent done yet but youd have to set in the web.config for each virtual dir an app settings like such
(mine is sql server tho)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<appSettings> 
		<add key="ConnectionString" value="server = localhost; Initial Catalog=helpdesk;integrated security=SSPI;Connect Timeout=15;Network Library=dbmssocn;"/>
	</appSettings>
	<system.web>
		<authentication mode="Windows" />
			<authorization>
    			<allow roles="Users, Admins"/>
    			<deny users="*"/>
			</authorization>
		<identity impersonate="true" />
	</system.web>
</configuration>
 
Best practice is definitely to put your ConnectionString in the web.config file. There are other alternatives such as the registry but web.config will do fine. The example adamroof have given above of adding an element in the appSettings element of web.config is correct.

If you are unsure of the Connection String itself then create a text file with the extension .udl and open it. This will start a "connection wizard" and when completed opening the file in notepad will display the string.

You use the appSetting in your code like this
Code:
SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
So when using Webconfig, you do not have to add any of the below information?
conn = new OleDbConnection "Provider=Microsoft.Jet.OLEDB.4.0; " + Data Source = " + Server.MapPath("DB/MyDB.mdb"));

This is where I am getting confused. I am not telling the webconfig file what type of connection it is. I guess I could just give it a shot and see how it works, but I want to understand the process as well.

 
Following on from above,

During development, I have created all my connections by dragging and dropping from the 'server explorer' pane.

How can I now configure my app so that it will use the connection settings in web.config?

Also, what can I do in my next app 'off the bat' so that I can drag and drop from server explorer, with server explorer picking up the web.config connection settings?

Thanks,

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 
Answered my own question.

For anyone else that comes across same query:

Update web.config as advised by AdamRoof above.

In design mode of web form, drag and drop from server explorer as normal.

Once you've finished, pull up the properties window for the SQLConnection that you've created. In the Dynamic Properties you'll see a property called ConnectionString. Click the elipses beside it (...) and tick the 'map to key in configuration file' option.

In the box, type the name of the key you added earlier in web.config, this would be 'ConnectionString' using AdamRoof's example above.

Job done

Cheers,

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 
dnjacks: look at my post in thread855-845600, it's pretty similar to what you're looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top