On row updating and updated event : SQL Events : Database C# Source Code


Custom Search

C# Source Code » Database » SQL Events »

 

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());
      }
  }

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» SQL Events