Extract connection string from appconfig file in C #

I have the following connection string declared in my app.config file:

  <connectionStrings> <add name="SqlConnectionString" connectionString="Data Source=xxx.xx.xx.xx;Initial Catalog=xxxxx;User Id=xx;Password=xxx;" providerName="System.Data.SqlClient" /> </connectionStrings> 

When I try to extract this connection string using the following C # code snippet, I get null . I can not get the connection string. Is there something wrong in the syntax?

First try:

 var settings = ConfigurationManager.ConnectionStrings["SqlConnectionString"]; string result = settings.ConnectionString; 

Second attempt:

 string result = ConfigurationSettings.AppSettings["SqlConnectionString"]; 
+7
source share
10 answers

For a non-web project and using app.config, as in OP, here is what I usually do, since the configuration file changes its name when compiling the application (to yourapp.exe.config):

  public static Configuration ExeConfig() { Assembly service = Assembly.GetAssembly(typeof(YourClass)); return ConfigurationManager.OpenExeConfiguration(service.Location); } 

Then, to reference the string of the s-th connection:

 ExeConfig().ConnectionStrings.ConnectionStrings[s].ConnectionString 
+6
source

First, you must add the appSettings tag

  <connectionStrings> <appSettings> <add name="SqlConnectionString" connectionString="Data Source=xxx.xx.xx.xx;Initial Catalog=xxxxx;User Id=xx;Password=xxx;" providerName="System.Data.SqlClient" /> </appSettings> </connectionStrings> 

Secondly, add a link to System.Configuration in your project and paste using System.Configuration into your source code.

Then you can use ConfigurationManager.AppSettings to access the configuration settings:

 string result = ConfigurationSettings.AppSettings["SqlConnectionString"]; 
+10
source

I have the same problem, but after much research, I found that the reason the code throws a link exception is because if you use the name connectionString, it will return null and when you try to call ToString () or the ConnectionString property of the property ConfigurationManager.ConnectionStrings , which is excluded during the call, so return the correct connection string, you should index it in ConnectionStringSettings using the following code.

 ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[/* index */]; string conString = conSettings.ConnectionString; 
+2
source

Another possible cause of the failure may be incorrect name resolution using the .NET platform.

If the application name is longer than 8.3 (for example, GreatStuff.exe ), it can be launched using its short 8.3-equivalent name (in my example GREATS~1.EXE ), and then .NET will search for the app.config file simply by adding .config to the short name. And it will not succeed.

+1
source

One thing I noticed when compiling a WinForm application in VS 2013:

I add the connection lines of my settings file - which updates the app.config file

When I expect:

 var connectionString = ConfigurationManager.ConnectionStrings["theConnectionStringName"].ConnectionString; 

I need to write:

 var connectionString = ConfigurationManager.ConnectionStrings["MyAppNameSpace.Properties.Settings.theConnectionStringName"].ConnectionString; 

Everything else works as usual

+1
source

I do this to get the connection string "OracleDbContext" ...

 ConfigurationManager.ConnectionStrings["OracleDbContext"].ConnectionString; 

my web.config looks like this:

  <connectionStrings> <add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=oracle_user; Password=oracle_user_password; Data Source=oracle" /> </connectionStrings> 
+1
source
 <configuration> <appSettings> <add key="ConnectionString" value="blah blah blah"/> </appSettings> </configuration> 

Will the configuration described above work with API / code like:

 string str = ConfigurationSettings.AppSettings["SqlConnectionString"]; 
0
source

The first syntax is definitely valid.

A possible reason is that the configuration file is not loaded properly in the application. I want to ensure what type of project it is. If this is a WinForm application or a console application, you need to install it in app.config. It will be compiled into .config and loaded when the application starts. If this is a web application, I am afraid that you cannot add node to app.config since it will not be loaded. You should put it in web.config. In addition, if you want to put the settings in the configuration file with the .net library (DLL), the configuration file will not be loaded in any case. The configuration file will only follow the main application.

0
source

Same problem. Based on the comments of Merlyn Morgan-Graham and Edward Zhu, I rebuilt my project, which used app.config to create the file myapp.exe.config with the connection string. Initially, the first syntax worked.

Another problem: I tried to use the project properties dialog to change the connection string, and added "project.My.Mysettings" to the connection string name. This caused a malfunction.

0
source

There is another way to do this for a console application:

  <configuration> <appSettings> <add key="SqlConString" value="some connection string"/> </appSettings> 

to read it

 using System.Configuration; 

and then use

 string CONSTRING; AppSettingsReader ap = new AppSettingsReader(); CONSTRING = ap.GetValue("SqlConString",typeof(string)).ToString(); 
0
source

All Articles