Is there anything in C# that can be used as database Trigger

喜欢而已 提交于 2020-01-21 14:39:08

问题


I have ERP database "A" has only read permission, where i cant create trigger on the table. A is made for ERP system (Unknown Program for me ). I have another Database "B" that is private to my application this application work on both databases. i want to reflect A's changes(for any insert/Update/Delete) instantly to B. Is there any Functionality in c# that can work exactly as trigger works in database???


回答1:


You have few solutions, best one depends on which kind of database you have to support.

Generic solution, changes in A database aren't allowed

If you can't change master database and this must work with every kind of database then you have only one option: polling.

You shouldn't check too often (so forget to do it more or less instantly) to save network traffic and it's better to do in in different ways for insert/update/delete. What you can do depends on how database is structured, for example:

Insert: to catch an insert you may simply check for highest row ID (assuming what you need to monitor has an integer column used as key).
Update: for updates you may check a timestamp column (if it's present).
Delete: this may be more tricky to detect, a first check would be count number of rows, if it's changed and no insert occured then you detected a delete else just subtract the number of inserts.

Generic solution, changes in A database are allowed

If you can change the original database you can decrease network traffic (and complexity) using triggers on database side, when a trigger is fired just put a record in an internal log table (just few columns: one for the change type, one for affected table, one for affected record).

You will need to poll only on this table (using a simple query to check if number of rows increased). Because action (insert/update/delete) is stored in the table you just need to switch on that column to execute proper action.

This has a big disadvantage (in my point of view): it puts logic related to your application inside the master database. This may be terrible or not but it depends on many many factors.

SQL Server/Vendor specific

If you're application is tied to Microsoft SQL Server you can use SqlDependency class to track changes made. It works for SS only but I think there may be implementations for other databases. Disadvantage is that this will always bee specific to a specific vendor (so if A database will change host...you'll have to change your code too).

From MSDN:

SqlDependency was designed to be used in ASP.NET or middle-tier services where there is a relatively small number of servers having dependencies active against the database. It was not designed for use in client applications, where hundreds or thousands of client computers would have SqlDependency objects set up for a single database server.

Anyway if you're using SQL Server you have other options, just follow links in MSDN documentation.

Addendum: if you need a more fine control you may check TraceServer and Object:Altered (and friends) classes. This is even more tied to Microsoft SQL Server but it should be usable on a more wide context (and you may keep your applications unaware of these things).




回答2:


You may find useful, depending on your DBMS:

Change Data Capture (MS SQL)

http://msdn.microsoft.com/en-us/library/bb522489%28v=SQL.100%29.aspx

Database Change Notification (Oracle)

http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_dcn.htm http://www.oracle.com/technetwork/issue-archive/2006/06-mar/o26odpnet-093584.html

Unfortunately, there's no SQL92 solution on data change notification




回答3:


Yes There is excellent post are here please check this out..

http://devzone.advantagedatabase.com/dz/webhelp/advantage9.1/mergedprojects/devguide/part1point5/creating_triggers_in_c_with_visual_studio_net.htm

If this post solve your question then mark as answered..

Thanks



来源:https://stackoverflow.com/questions/17807513/is-there-anything-in-c-sharp-that-can-be-used-as-database-trigger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!