Getting data from SqlDependency

后端 未结 7 645
别那么骄傲
别那么骄傲 2021-01-02 06:25

I\'ve got a table and a SqlDependency that is waiting for new inserts.

OnChange fires as I need, but I don\'t understand if it\'s possible to get the row which caus

7条回答
  •  盖世英雄少女心
    2021-01-02 06:41

    You can use Temp tables. first of all you need to create a temp table with all the fields you need to keep under investigation. something like:

    CREATE TABLE ##TempTab(
        [field1] [varchar](50) NULL,
        [field2] [varchar](50) NULL
    }
    

    Please note that kind of tables created within external cose are automatically dropped since the creator program quits so you don't need to drop it on formClosing... Now, after setting up sqlDepency stuffs you have to fill up you temp table, it's something like a snapshot of the starting scenario. Then, every time the onChange event is fired you just need to compare your temp table with updated situation. it could be something like:

        select * from ##temptable left outer join mytable
    ON ##temptable.field1=myTable.field1 AND ##temptable.field2=myTable.field2
    WHERE myTable.field2 is null
    

    this will give you all rows has just been deleted (or chagend with old values). On the other side:

    select * from mytable left outer join ##temptable
        ON ##temptable.field1=myTable.field1 AND ##temptable.field2=myTable.field2
        WHERE ##temptable.field2 is null
    

    will give you all rows has just been added (or changed with new values). After this compare you just need to update your temp table with new values (faster way is to delete everything and insert all values) Of course, if your programm will be run simultaneously by different users, you'll need to handle userid within temp table.

提交回复
热议问题