0

I am new to asp.net and C#. I am using a connection string in each and every page to connect to database and get results (the connection string is shown in the below code). Is there any way I can make the connection string centralized? So that I don't have to call it in each and every page?

Can anyone help me to solve this problem? please..

con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=ewadb;Integrated Security=SSPI");

//data = new SqlDataAdapter("SELECT * FROM deals", con);
//dset = new DataSet();
//data.Fill(dset, "deal");
2

5 Answers 5

3

You can put the connection string in the web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <connectionStrings>
    <add name="default" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=ewadb;Integrated Security=SSPI" />
 </connectionStrings>
</configuration>

You can then get the value with the ConfigurationManager class:

ConfigurationManager.ConnectionStrings["default"].ConnectionString;

Architecturally speaking, your pages shouldn't be handling data access directly.

See here: http://en.wikipedia.org/wiki/Multitier_architecture for overview of n-tier architecture.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. Robh.. but when i try to pass the connection string in to newsqlconnection function it gives an error saying "instance failure".. con = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ConnectionString);
Whoops. Remove the double slash from connection string. I'll edit answer now.
2

You should place it in the web.config. This means you can change it easily and call it consistently from anywhere. Please see the following for an intro.

http://msdn.microsoft.com/en-us/library/ms178411.aspx

Also a simple explaination:

http://www.connectionstrings.com/store-connection-string-in-webconfig/

Comments

0

1) Use web.config file for store your connection string as this

<appSettings>
    <add key="connString_MyDB" value="Server=localhost;Integrated Security=false;Database=WORLD;Asynchronous Processing=True;user=sa;password=123;"/>
</appSettings>

2) to retrieve the connection string call as this in your class

  Private connString As String = System.Configuration.ConfigurationManager.AppSettings.Get("connString_MyDB").ToString

1 Comment

thanks amila. but i need the value and pass it in to a new sqlconnection function. like this.. con = new sqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("connection")); is there any way i can pass the connection string in to the function..
0

Place the connection string into configuration section of your web.config file.

<configuration>
 <connectionStrings>
    <add name="default" connectionString="your connection string" />
 </connectionStrings>
</configuration>

Use below code to access connection string in page.

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

Comments

0

You can do that like below:

Web.Config:

Get the connection string in code: sqlConnection con = new sqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("connect‌​ion"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.