简体   繁体   English

监视MySQL表以查找C#程序中的更改?

[英]Monitor MySQL table for changes within a C# program?

Is it possible to monitor a mysql table for changes within ac# application? 是否可以监视mysql表以查找ac#application中的更改? I basically want an event to be raised when data is inserted into the table. 我基本上希望在将数据插入表中时引发事件。 The only thing I can think of now is to query the table every 100ms or so. 我现在唯一能想到的是每100ms左右查询一次表。

If both the application and database server are on the same machine you might be able to set up a trigger in MySQL which writes out to a log file AFTER INSERT, UPDATE and then create a FileSystemWatcher to watch that log file. 如果应用程序和数据库服务器都在同一台机器上,那么您可以在MySQL中设置一个触发器,该触发器会在INSERT,UPDATE之后写入日志文件,然后创建一个FileSystemWatcher来监视该日志文件。 FileSystemWatcher will fire events when the file is changed that your application can react to. 当文件被更改时, FileSystemWatcher将触发您的应用程序可以响应的事件。

The trigger might look something like this: 触发器可能看起来像这样:

create trigger MyTable_Monitor
after insert, update on MyTable
for each row
begin
select * from new into outfile "path/to/table.log"
end

One problem I see with the above code is that the outfile cannot be appended to (best I can tell) so you might have problems if there are multiple queries executed in one call (or even multiple queries executed simultaneously by different clients). 我在上面的代码中看到的一个问题是outfile无法附加到(最好我可以告诉),因此如果在一次调用中执行多个查询(或者甚至是不同客户端同时执行多个查询),则可能会遇到问题。 Any suggestions for improvement are welcome. 欢迎任何改进建议。

Perhaps add Insert, Update, Delete triggers to the table to log row in a new table with two columns.. 也许可以向表中添加Insert,Update,Delete触发器来记录具有两列的新表中的行。

  1. uid of row changed 行的uid改变了
  2. type of change insert/update/delete 更改类型插入/更新/删除

Then you access this table and it tells you the specifically which rows were inserted, updated, deleted. 然后,您访问此表,它会告诉您具体插入,更新,删除哪些行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM