Search This Blog

Saturday 1 September 2012

Data Base connection in ado.net

A SqlConnection is an object, just like any other C# object.  Most of the time, you just declare and instantiate the SqlConnection.
Ado.net  will contain following steps.

  1. Instantiate the SqlConnection.
  2. Open the connection.
  3. Pass the connection to other ADO.NET objects.
  4. Perform database operations with the other ADO.NET objects.
  5. Close the connection.


using (SqlConnection connection = new SqlConnection(connectionString))
       {
           // Create the Command and Parameter objects.
           SqlCommand command = new  SqlCommand("Employee_Insert" 
, connection);
           command.Parameters.AddWithValue("@CityName", txtCity.Text);
           command.Parameters.AddWithValue("@Name", txtName.Text);
           command.CommandType = CommandType.StoredProcedure; 
           try{
               connection.Open();
               command.ExecuteNonQuery();
           }
           catch (SqlException ex){
               throw ex;
           }
           catch (Exception ex){
               throw ex;
           }
           finally{
               connection.Close();
           }
       } 
Database connection in ado.net
Database connection in ado.net
 

No comments:

Post a Comment