开发者

How do I write a line to a text file whenever data is inserted into a database?

If I insert to database like thi开发者_开发问答s:

insert into MyTbl (A,B) values ('1','2')

I need to insert into a text file like this:

SW.writeline('1','2').......

I need to make sure that the text file will always be updated when data is inserted. What is the best and fastest way to accomplish this?


Are you sure you want to write to a text file or are you just trying to get logging into another database?

Use a trigger to insert the text you want to write into a separate logging table. Then have a periodic task which reads the logging table and does whatever you need with it, be it writing to a text file or copying to another database.


If you're using a SqlCommand, you can subscribe to the StatementCompleted event. However, the event args won't show you what you just inserted.

Another approach could be to just have your sw.WriteLine(..) fire off immediately after your insert command. You could always wrap a transaction around it to make sure to two stay in sync.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜