Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get connection string in console application [closed]

I've created an Console application project then add new App.conf file into my project. In my configuration file, I copied connection string that I've created by adding entity framework as below:

<connectionStrings>
    <add name="DBEntities" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=MY-LAPTOP;Initial Catalog=TestDB;User ID=test;Password=123123;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

On the program.cs I want to using ConfigurationManager class to retain the connection string but always raise me errors.

String connString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

Please suggest me solutions. Thanks in advance.

like image 529
nvtthang Avatar asked May 16 '11 07:05

nvtthang


People also ask

Where u will store connection string?

Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file. Child elements include add, clear, and remove.

Where to store connection string in asp net?

In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings. json , an environment variable, the user secret store, or another configuration source.


3 Answers

Your connection string is called DBEntities

Use this line in your code instead:

String connString = ConfigurationManager.ConnectionStrings["DBEntities"].ConnectionString;
like image 101
Kevin McKelvin Avatar answered Nov 06 '22 01:11

Kevin McKelvin


This is only a guess since you didn't include the actual exception in your question:

Add a reference to System.Configuration to your project.

like image 31
jgauffin Avatar answered Nov 06 '22 01:11

jgauffin


Also, your name-attribute's value is DBEntities, but you are trying to access ApplicationServices in your code.

like image 35
Jacob Avatar answered Nov 06 '22 02:11

Jacob