28

I know this might be a very basic question, but maybe thats why I'm having problems finding the answer. Right now I'm creating database connections in my source files by doing something like this:

SqlConnection con = new SqlConnection("Data Source=...Password=...);
SqlCommand cmd = new SqlCommand(String.Format("SELECT * FROM Table;"), con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

But this means that if I choose to change databases it will be a major pain. Do you guys know how to use the connection string from a web.config file instead?

Thank you!

1
  • 3
    Just out of curiosity, are you closing / disposing your connections / commands / readers? If you're not using the 'using' keyword, start! Commented Mar 15, 2011 at 19:39

6 Answers 6

48
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);

Make sure that your website has a reference to System.Configuration or it won't work.

The documentation can be found as How to: Read Connection Strings from the Web.config File, with code sample

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

2 Comments

How about for OLEDB connection?
You should be able to find that info at connection strings.com.
7

You can try

var conString = System.Configuration.
                ConfigurationManager.ConnectionStrings["connectionStringName"];
string strConnString = conString.ConnectionString;
SqlConnection con = new SqlConnection(strConnString);

1 Comment

+1: Note that the OP may have to manually add the System.Configuration reference to his project.
5

In your app.config (or web.config):

<configuration>
       <connectionStrings>
           <add name="MainConnectString" connectionString="yourConnectionString" providerName="providerName" />
       </connectionStrings>
</configuration>

And in code:

string connectionString = ConfigurationManager.ConnectionStrings["MainConnectString"];

Comments

2

along with StackOverflowException's response, make sure to add using System.Configuration; to your code behind page

Comments

1

You can use EnterpriseLibrary.

using Microsoft.Practices.EnterpriseLibrary.Data;

Then you create a method to get connection string:

 public static string GetConnectionString()
    {
        Database YourData = DatabaseFactory.CreateDatabase("someconnectionname");
        return YourData .ConnectionString;
    }

In your web.config you will have following:

 <connectionStrings>   
   <add name="someconnectionname" connectionstring=..... />
 </connectionString>

Comments

1

If you get "cannot implicitly convert type 'system.configuration.connectionstringsettings' to 'string'", do:

string connectionString = ConfigurationManager.ConnectionStrings["MainConnectString"].ConnectionString;

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.