Click here to Skip to main content
16,022,417 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to Declare a connection string and connection object globally in C#.Net?
Posted

Hi you can declare constring and connection object globally as below

First you need to add key in the app.config file as shown below

<br />
<br />
   <appSettings><br />
<br />
<add key="DbConnectionString" value="database=pubs;user Id=sa;password=sa"/><br />
<br />
    </appSettings><br />


then in the C# code behind file include the namespace
using System.Configuration;

then read the connection string from app.config as below

static readonly string DB_CONN_STRING = ConfigurationManager.AppSettings["DbConnectionString"];
 
Share this answer
 
Buy a book already ? If you have no idea how to do any of these things, then you have no need of a connection string, you need to learn C# before you worry about SQL.
 
Share this answer
 
Comments
Toli Cuturicu 20-Jul-10 10:45am    
Reason for my vote of 3
You probably live in a country where the average income allows people to buy books
You can find ways to declare a connection string here[^].

For a windows application, you can place the connection string 'globally' in the App.Config file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900