Watch for a table new records in sql database

前端 未结 6 1329
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 11:36

I am using EF in a windows application and I want my application to do some tasks when a new record inserted in a certain table \"these new records will be inserted by a we

6条回答
  •  情书的邮戳
    2020-12-01 11:48

    See Change Data Capture, specifically sys.fn_cdc_get_max_lsn. That will create a place to look for changes.

    Then, write a stored procedure to poll that view. Have it capture the previous value, and loop looking for a change with a WAITFOR delay appropriate to your data arrival rate.

    When the procedure notices the highest LSN has changed, have it do something. It could select the row(s). It could also just print a message. Then it returns to its polling station (as it were).

    Printing a message might sound odd and, not knowing the first thing about EF (and being severely allergic to such things) I don't know if it will help you here. But in principle it should because the underlying communication is present.

    From your application, invoke the procedure. When the T-SQL PRINT (orRAISERROR) statement is executed by the procedure, a message will be sent to the client. Where it goes in EF or how you'd handle it, I can't say, but it should go somewhere useful because in ordinary circumstances it would be displayed to the user. In your case, your application will be looking for that particular message number or text, and react accordingly. (Other messages, naturally, you'd want to handle normally.)

    This approach is very cheap and very simple. It uses almost no server resources, sends messages to the client only when changes occur, uses no extra tables, relies on almost no user-written code, and can be verified as running by looking at sysprocesses.

提交回复
热议问题