There is a table (Sql Server), in it several lines. If I add another row to the table, how can I put an if (добавилась строка) condition if (добавилась строка) ?

 static void Main(string[] args) { using(SqlDataAdapter adapter = new SqlDataAdapter("SELECT s.id as shedule_id, s.name_ru as shedule_name_ru, s.sendType as shedule_sendType, c1.cod as typsendtype , s.typPeriod, c2.cod as time_name_ru, s.metaAlg, m.cod as meta_cod, s.dt_send, s.dt_beg as shedule_dt_beg, s.dt_send_daytime as shedule_dt_send_daytime, s.dt_period_end as shedule_dt_period_end , s.dt_upd as shedule_dt_upd, s.dt_create as shedule_dt_crt FROM Shedule s inner join ClBaseItem c1 on s.sendType=c1.id inner join ClBaseItem c2 on s.typPeriod=c2.id inner join MetaAlg m on s.metaAlg=m.id", cn)) using(DataTable dt = new DataTable()) { adapter.Fill(dt); if () // тут надо поставить условие MyTimer NewTimerRecord = new MyTimer(); NewTimerRecord.Enabled = true; NewTimerRecord.Interval = 300000; NewTimerRecord.Elapsed += RecordTime; NewTimerRecord.AutoReset = true; } static void RecordTime(object sender, System.Timers.ElapsedEventArgs e) { ((MyTimer) sender).Stop(); var s_id = ((MyTimer) sender).s_id.ToString(); var alg = ((MyTimer) sender).alg.ToString(); var date = DateTime.Now; string sql = string.Format("Insert Into Alg_stack" + "(shedule_id,metaAlg,datetime) Values( @s_id,@alg, @date)"); using(SqlConnection cn = new SqlConnection()) { cn.ConnectionString = @"Data Source=192.168.1.156;Initial Catalog=ihd_aktobe;User ID=sa;Password=Pa$$w0rd"; cn.Open(); using(SqlCommand cmd = new SqlCommand(sql, cn)) { cmd.Parameters.AddWithValue("@s_id", s_id); cmd.Parameters.AddWithValue("@alg", alg); cmd.Parameters.AddWithValue("@date", date); cmd.ExecuteNonQuery(); } cn.Close(); } } 

1 answer 1

At the level of Sql Server trigger to insert. In the following example, an email is sent to the specified user (MaryM) when the table Customer changes.

  IF OBJECT_ID ('Sales.reminder2','TR') IS NOT NULL DROP TRIGGER Sales.reminder2; GO CREATE TRIGGER reminder2 ON Sales.Customer AFTER INSERT, UPDATE, DELETE AS EXEC msdb.dbo.sp_send_dbmail @profile_name = 'AdventureWorks2012 Administrator', @recipients = 'danw@Adventure-Works.com', @body = 'Don''t forget to print a report for the sales force.', @subject = 'Reminder'; GO 

Or, after inserting, once again, drop the query to update the data set.