Lucas_Santos
Lucas_Santos

Reputation: 4740

How can I access the connection string in my Web.Config file through a Windows Application

I have my Entity Connection String set in my Web.Config file. In the same solution, I have a Windows Application, and I want to access the Connection String of my Entity. How can I access the connection string that exist in my Web.Config file? Or I have to create a specific one in my Windows Application ?

Upvotes: 0

Views: 263

Answers (3)

Jimmy Hoffa
Jimmy Hoffa

Reputation: 5967

If you're unfamiliar with adding a config file to a windows app, you must select an "Application Configuration File" from add new items, it will create a file named App.Config which will at compile time become YourProgramsName.exe.config but internally it works mostly just like a web.config with an appsettings section and connectionstrings sections and all the other normal sections.

The best way to share this data between Web and Win config files would be:

Create a separate file that looks like:

  <connectionStrings>
    <add name="Name" 
     providerName="System.Data.ProviderName" 
     connectionString="Valid Connection String;" />
  </connectionStrings>

and reference it from both config files with:

<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <connectionStrings configSource="connections.config"/>
</configuration>

Then just add the connections.config (add existing item by link) to each project.

More reading: http://msdn.microsoft.com/en-us/library/ms254494.aspx

Upvotes: 3

KF2
KF2

Reputation: 10171

use this code:

string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Upvotes: 0

jrummell
jrummell

Reputation: 43097

Put the connection string your windows application's App.config.

Upvotes: 0

Related Questions