0

I already have a connection string in my config file

<connectionStrings>
    <add
        name="ConnectionString"
        connectionString="Data Source=*;Initial Catalog=*;User ID=*;Password=*"
        providerName=".NET Framework Data Provider for SQL Server" />

Currently I am just adding another connection string in my .cs file:

SqlConnection myConnection =
    new SqlConnection("user id=*;password=*;server=localhost;");

I want to use config file string to connect to a database in the .cs file without adding another string. How can I do that?

using (SqlConnection cn =
    new SqlConnection(
        ConfigurationManager.ConnectionStrings["MyDbConn"].ToString()))

This was the code I found, is there any shorter way?

1
  • 1
    No, no shorter way unless you wrap the retrieval of the connection string in a read-only property in a separate static class or something, giving you MyConnectionStrings.ConnectionString; or however you decide to name it. Commented Nov 18, 2011 at 3:05

4 Answers 4

4

Using ConnectionStrings property of System.Web.Configuration.WebConfigurationManager to get connection string in config file

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

3 Comments

@Bhrugesh Patel: If you have only one connection string, then use WebConfigurationManager.ConnectionStrings[0].ConnectionString
I have actually two.. One is to localhost, another is to actual MS SQL server.. But I will remove localhost once I am done testing
Then just use name instead of index which you've found in accepted anwser :-)
2

Not really, but you could wrap it in its own little helper function:

private static string GetConnectionString(string name)
{
    return WebConfigurationManager.ConnectionStrings[name].ConnectionString;
}

using (var cn = new SqlConnection(GetConnectionString("MyDbConn"))) { ... }

1 Comment

Thanks :) Connection up and running
2

Just use System.Configuration.ConfigurationManager.ConnectionStrings to get access to the connection string defined in your config file.

 var myconnectionString = System.Configuration.ConfigurationManager
      .ConnectionStrings["ConnectionString"].ConnectionString;

Comments

0

You can create a static helper class, named Config or something similar and give it a property ConnectionString, that will return the long "WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString" thing. That is the approach I usually go with in my projects and it's actually very easy to use.

public static class Config
{
   public static string ConnectionString
   {
      get
      {
         return WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString;
      }
   }
}

This approach is the most useful when more classes need to acccess the connection string and this way you have a centralized access to it and easy to change solution.

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.