Read connection string from web.config
Asked Answered
C

12

274

How can I read a connection string from a web.config file into a public class contained within a class library?

I've tried:

WebConfigurationManager

ConfigurationManager

But these classes are not recognized within my class library.

Crossfade answered 26/5, 2011 at 6:5 Comment(0)
I
188

Add System.Configuration as a reference.

For some bizarre reason it's not included by default.

Inclusive answered 26/5, 2011 at 6:7 Comment(0)
B
545

You need to add a reference to System.Configuration and then use:

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;
Bombe answered 26/5, 2011 at 6:8 Comment(1)
MSDN, System.configuration. It needs System.Configuration.dll.Pediform
I
188

Add System.Configuration as a reference.

For some bizarre reason it's not included by default.

Inclusive answered 26/5, 2011 at 6:7 Comment(0)
O
67

C#

// Add a using directive at the top of your code file    
using System.Configuration;

// Within the code body set your variable    
string cs = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

VB

' Add an Imports statement at the top of your code file    
Imports System.Configuration

' Within the code body set your variable    
Dim cs as String = ConfigurationManager.ConnectionStrings("connectionStringName").ConnectionString
Og answered 18/6, 2013 at 16:48 Comment(1)
"Add a reference at the top of your code file" => that's a using directive, not a reference!Stilla
Q
27

Add System.Configuration as a reference then:

 using System.Configuration;

 ...

 string conn = 
    ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
Quarles answered 8/8, 2011 at 13:31 Comment(0)
C
20

I guess you need to add a reference to the System.Configuration assembly if that have not already been added.

Also, you may need to insert the following line at the top of your code file:

using System.Configuration;
Corby answered 26/5, 2011 at 6:7 Comment(0)
H
17
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;  

C#

string constring = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constring))

BELOW WEB.CONFIG FILE CODE

<connectionStrings>
    <add name="ABCD" connectionString="Data Source=DESKTOP-SU3NKUU\MSSQLSERVER2016;Initial Catalog=TESTKISWRMIP;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

In the above Code ABCD is the Connection Name

Haftarah answered 24/8, 2017 at 7:47 Comment(1)
Addition: Besides the indexer that accepts the name of the connection string, it is also allowed to use integer indices - which is useful if you want to read all connection strings in a for loop (for (int i = 0; i < numOfConnections; i++) { var conn = ConfigurationManager.ConnectionStrings[i]; ... }) and make them selectable in a combobox. With var numOfConnections = ConfigurationManager.ConnectionStrings.Count; you can determine how many connection strings exist. In this example conn.Name contains the name of the connection.Uam
C
15

In VB : This should work

ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString

In C# it would be (as per comment of Ala)

ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString
Calondra answered 26/5, 2011 at 6:11 Comment(3)
Those parentheses need to be brackets.Vestige
@CharlesBurns,Thanks, I wrote in VB by mistake, in C# sure it should be ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionStringCalondra
Ahh, I didn't even realize that was VB. I thought it was a typo. In a way, my mistake too.Vestige
F
11

You have to invoke this class on the top of your page or class :

using System.Configuration;

Then you can use this Method that returns the connection string to be ready to passed to the sqlconnection object to continue your work as follows:

    private string ReturnConnectionString()
    {
       // Put the name the Sqlconnection from WebConfig..
        return ConfigurationManager.ConnectionStrings["DBWebConfigString"].ConnectionString;
    }

Just to make a clear clarification this is the value in the web Config:

  <add name="DBWebConfigString" connectionString="....." />   </connectionStrings>
Flouncing answered 13/12, 2015 at 22:59 Comment(1)
In Web project is better to use WebConfigurationManager in System.Web.Configuration.Radiolocation
I
9
using System.Configuration;


string conn = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();
Idolla answered 29/1, 2013 at 7:21 Comment(0)
P
4
using System.Configuration;


string connString = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();

Remember don't Use ConnectionStrings[index] because you might of Global machine Config and Portability

Pimiento answered 14/8, 2013 at 11:16 Comment(0)
M
2

First add this:

using System.Configuration;
Muckworm answered 18/8, 2012 at 21:54 Comment(0)
F
1

Everybody seems to be suggesting that adding

using System.Configuration;

which is true.

But might I suggest that you think about installing ReSharper's Visual Studio extension?

With it installed, instead of seeing an error that a class isn't defined, you'll see a prompt that tells you which assembly it is in, asking you if you want it to add the needed using statement.

Feudality answered 26/8, 2015 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.