user3480008
user3480008

Reputation: 11

How to set connection string for whole application in c# windows Application?

datadirectory is not working for connection string in c# windows application in my project. I already tried this but its not work.

Upvotes: 0

Views: 69

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

you can add it in app.config:

<connectionStrings>
  <add name="Dbconnection" 
       connectionString="Server=localhost; Database=OnlineShopping ; Integrated Security=True" ;
       providerName="System.Data.SqlClient" />
</connectionStrings>

in code Add reference to add System.Configuration and read like this:-

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

or in web.config, you can add it appSettings:

<appSettings>
   <add key="ApplicationTitle" value="Sample Console Application" />
   <add key="ConnectionString"
       value="Server=localhost;Database=Northwind;Integrated
              Security=false;User Id=sa;Password=;" />
</appSettings>

and read in code like this:

 ConfigurationSettings.AppSettings["ConnectionString"];

Upvotes: 3

Related Questions