Connection String and Connection Class File

Connection String
 
Include the following lines in the configuration section of web.config file. 
  1. <appSettings>  
  2.    <add key="con" value="Server = My Server name; Initial Catalog= My Database name; User ID = abcd; Password = *****;"/>  
  3. </appSettings>  
Connection Class File
 
Add a class file by right clicking on the project -- Add -- Class. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Configuration;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. namespace WebApplication1 {  
  9.     public class code {  
  10.         SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"].ToString());  
  11.         public DataSet select(string s) {  
  12.             DataSet ds = new DataSet();  
  13.             SqlDataAdapter da = new SqlDataAdapter(s, con);  
  14.             da.Fill(ds);  
  15.             return ds;  
  16.         }  
  17.         public int insert(string s) {  
  18.             int i;  
  19.             SqlCommand cmd = new SqlCommand(s, con);  
  20.             con.Open();  
  21.             i = cmd.ExecuteNonQuery();  
  22.             con.Close();  
  23.             return i;  
  24.         }  
  25.     }  
  26. }   
Note: In the webform, just create an object for the connection class file, thus reducing the complexity of the code........!!