C# / SQL Listener to tell me if a row has been inserted into a table

前端 未结 7 1132
傲寒
傲寒 2020-12-15 10:44

Can someone post sample code or tell me how to set up a listener to notify me (trigger an event) if a new row is inserted into a SQL Server database?

I don\'t want

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 11:22

    I also recommend trigger that registers inserts somewhere in db (and you must poll database anyway, which is not so expensive if the trigger modifies a special table with a single row). But if you have IDENTITY on your primary key, you can monitor the value of current identity of the table:

    SELECT IDENT_CURRENT('TableName')
    

    It's a hack, however, but it's fast and you don't need to modify your database (of course, it works only for insert operations). However you may skip insert operations if between polls identity value was modified explicitly and was set exactly to the value it was at the moment of previous polling (which is not very probable though).

提交回复
热议问题