Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching 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 fetch this connection string using the following C# code snippet, I get the value null. I am not able to obtain the connection string. Is there anything wrong in the syntax?

First attempt:

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

Second attempt:

string result = ConfigurationSettings.AppSettings["SqlConnectionString"];
like image 792
mahesh Avatar asked Dec 23 '11 05:12

mahesh


People also ask

What is connection string in app?

Applications use connection strings to identify the server instance and database to connect to and to determine what driver, login, etc. to use to connect to the SQL Server instance. Typically, the connection string will be stored in a configuration file somewhere within the application or web server.

Where are connection strings stored?

Connection strings in configuration files are typically stored inside the <connectionStrings> element in the app. config for a Windows application, or the web. config file for an ASP.NET application.

How can create connection string in Appsettings JSON?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new Connection String entry is added.


2 Answers

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>

Second, add a reference to System.Configuration to your project and insert a using System.Configuration in the source.

Then, you can use ConfigurationManager.AppSettings to access your config setting:

string result = ConfigurationSettings.AppSettings["SqlConnectionString"];
like image 187
shenhengbin Avatar answered Oct 06 '22 23:10

shenhengbin


For a non-web project, and with app.config set up as in the OP here's what I usually do, since the config file changes names when the app is compiled (to yourapp.exe.config):

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

Then to reference the s'th connection string:

ExeConfig().ConnectionStrings.ConnectionStrings[s].ConnectionString
like image 39
RickNZ Avatar answered Oct 07 '22 00:10

RickNZ