On row updating and updated event : SQL Events « Database ADO.net « C# / C Sharp






On row updating and updated event


using System;
using System.Data;
using System.Data.SqlClient;

   class SqlDemo {
      static void Main(){
         string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";

         SqlConnection cn = new SqlConnection(connString);
         try
         {
            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Employee", cn);
     
            SqlCommandBuilder cb = new SqlCommandBuilder(da);

            DataSet ds = new DataSet();
            da.Fill(ds, 0, 1, "Employee");

            da.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating);
            da.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);

            DataTable dt = ds.Tables["Employee"];
            dt.Rows[0][1] = "T";

            da.Update(ds, "Employee");

            da.RowUpdating -= new SqlRowUpdatingEventHandler(OnRowUpdating);
            da.RowUpdated -= new SqlRowUpdatedEventHandler(OnRowUpdated);
 
         } catch (SqlException ex) {
            Console.WriteLine(ex.Message);
         }
         finally
         {
            cn.Close();
         }

      }


      static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e) 
      {
         Console.WriteLine("OnRowUpdating event");
         if (e.Status != UpdateStatus.Continue) 
            Console.WriteLine("RowStatus = " + e.Status.ToString());
      } 

      static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e) 
      {
         Console.WriteLine("OnRowUpdating event");
         if (e.Status != UpdateStatus.Continue) 
            Console.WriteLine("RowStatus = " + e.Status.ToString());
      }
  }

           
       








Related examples in the same category

1.How to use the StateChange event
2.How to use the InfoMessage event
3.Register two SqlConnection change events