How to add & access Connectionstring in .net


In app.config or web.config add the following code


 <configuration>  
  <connectionStrings>  
   <add name="Conn" connectionString ="Data Source=.\sqlexpress;Initial Catalog=SAM;User Id=sa;  
   Password=abc;integrated Security=true"/>  
  </connectionStrings>  
 </configuration>  


In codebehind file (.cs file) add the follwing line to access the connection string from config file

 string constring = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString();  


Note: for ConfigurationManager class add assembly refrence to refrence folder
Read More »