Saturday, 13 August 2011

steps to create an SQL connection

step1:

              Add the SQL Client namespace i.e,
                     
          using System.Data.SqlClient;


Step2:

              Create a SqlConnection and specifying the connection string
                  
                  
       SqlConnection newConn = new SqlConnection(
                              "user id=usrname;" +
                              "password=password;" + 
                              "server=serverurl;" +
                              "Trusted_Connection=yes;" +
                              "database=database; " +
                              "connection timeout=30");

        
           

Step3:
        This is the last step to get connected
      
                    SqlConnection.Open()
            
                  
         try
           {
             newConn.Open();
           }
         catch(Exception e)
           {
             Console.WriteLine(e.ToString());
           }

        SqlConnection.Open()does not return an error but throws an exception so must be kept  in a   try/catch brace. rather than having the program explode in front of the user.