Read Connection String from Web.Config

Read connection string from web.config

Add System.Configuration as a reference.

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

Can't read connection string from web.config

You will have to migrate the config to the new file appsettings.json
https://learn.microsoft.com/en-us/aspnet/core/migration/configuration?view=aspnetcore-2.1

How to get Connection String From the Web.config

Use ConfigurationManager.ConnectionStrings collection to read connection string from web.config.

Get Connection String from Web.config in asp.net

Using the ConfigurationManager.ConnectionStrings is about the only proper way, to use it properly with sanity check you can have such code:

public DBGateway()
{
ConnectionStringSettings mySetting = ConfigurationManager.ConnectionStrings["test"];
if (mySetting == null || string.IsNullOrEmpty(mySetting.ConnectionString))
throw new Exception("Fatal error: missing connecting string in web.config file");
conString = mySetting.ConnectionString;
}

This will throw useful error in case the connection string is missing, instead of cryptic "null object" error.

Worth to mention that the ConnectionStringSettings class is overriding the ToString() method:

public override string ToString()
{
return this.ConnectionString;
}

So it means that using ConfigurationManager.ConnectionStrings["test"].ToString() is the same like ConfigurationManager.ConnectionStrings["test"].ConnectionString however you still better perform sanity check and personally it looks cleaner to use the actual property and not depend on the class to give it.

Reading connection string defined in web.config from Windows service

According to your description, I suggest you could try to use System.Xmllibrary to read the config file and use select method to select the connection string node in the config file.

More details, you could refer to below codes:

        XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"The path");
XmlNodeList xnodes = xdoc.SelectSingleNode("/configuration/connectionStrings").ChildNodes;
foreach (XmlNode item in xnodes)
{
if (item.Attributes[0].Value == "ConnectionString")
{
Console.WriteLine(item.Attributes[1].Value);
}
}

Result:

Sample Image

How to access the default connection string from web.config in c#


var dataConfig = (Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings)System.Configuration.ConfigurationManager.GetSection(
"dataConfiguration");

string connectionString = ConfigurationManager.ConnectionStrings[dataConfig.DefaultDatabase].ConnectionString;

That's what I can tell from typical usage of custom config sections and DatabaseSettings class.

EDIT: I have tested it in LINQPad with your exact app.config, got result:

Data Source=mydatasource;Max Pool Size=100;Pooling=true; Initial Catalog=MyDB;User ID=Myuser;Password=Password;Connection Timeout=60

Read connection string from web.config file when deploy to IIS7

The web.config has a dedicated section for connection strings, and the code you are using is using the configuration manager to access the connection strings section in the web.config. The problem you have is that you haven't put the connection string in the connection strings section, you have put it in the app settings. Move the connection string out of app settings and put into the connection strings section, or change your code to read it from app settings. Now storing a connection string in app settings was the way it was done in .NET 2.0, but since then (.NET 3.5) there has been a dedicated section made for connection strings.

please refer here

e.g.

<connectionStrings>
<add name="ConnString" connectionString="xxxwhateveritisxxx" />
</connectionStrings>

or code to read it from app settings (if you must have it in app settings, although I wouldn't recommend):

string connectionString = ConfigurationManager.AppSettings["ConnString"];

Read connection string from web config

That's not right. It's stored in your ConnectionStrings not your AppSettings. See the MSDN for How to: Read Connection Strings for more information.



Related Topics



Leave a reply



Submit