监控MySQL的表中的C#程序的变化?程序、MySQL

2023-09-03 02:35:04 作者:絠倖識祢ζ

是否可以监控在C#应用程序更改MySQL表?我基本上要当数据被插入到表被引发的事件。我唯一​​能想到的,现在是查询表每100ms左右。

Is it possible to monitor a mysql table for changes within a c# 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.

推荐答案

如果应用程序和数据库服务器在同一台机器上,你也许可以建立在MySQL中的触发而写出到一个日志文件中插入后,更新,然后创建一个 FileSystemWatcher的观看该日志文件。 FileSystemWatcher的将火灾事件。

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. FileSystemWatcher will fire events when the file is changed that your application can react to.

触发可能是这个样子:

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

一个问题,我与上述code看到的是,OUTFILE不能被附加到(尽我可以告诉),所以你可能有问题,如果有被同时执行在一个呼叫中执行多个查询(甚至多个查询不同的客户端)。任何改进的建议是值得欢迎的。

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). Any suggestions for improvement are welcome.