Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

.net - Monitor MySQL table for changes within a C# program?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...