[DUG] SqlDependency

Neven MacEwan neven at mwk.co.nz
Tue Apr 3 21:55:03 NZST 2012


David

I think the most 'elegant' solution would be to script write a set of 
DML triggers that wrote to a log and you could poll that, AFAIK the 
notifications system in 2008 only works on DDL events. At least if you 
did it with a set of triggers then you wouldn't need a timestamp column 
and  you wouldn't need to alter your polling code on each DB Change

a quick script to gen the code would be something like

DECLARE
     @SQL      VARCHAR(MAX),
     @Template VARCHAR(MAX) = 'CREATE TRIGGER TR_#Tablename# ON 
#Tablename# FOR INSERT, UPDATE AS
BEGIN
     DECLARE
         @I               INTEGER,
         @RowCount        INTEGER = @@ROWCOUNT,
         @ErrorNumber     INTEGER,
         @Action          VARCHAR(10)

     IF @ROWCOUNT = 0 RETURN
     SET NOCOUNT ON;

     /* Get The Task */
     SET @I = (SELECT COUNT(*) FROM inserted) - (SELECT COUNT(*) FROM 
deleted)
     SET @Action =
         CASE
             WHEN @I = 0 THEN ''Update''
             WHEN @I > 0 THEN ''INSERT''
         ELSE ''Delete''
     END

     INSERT INTO ChangeLog(TableName, Action, When)
     SELECT ''#Tablename#'', @Action, GETDATE()
END
go
'

DECLARE C1 CURSOR LOCAL STATIC FOR
     SELECT REPLACE(@Template, '#Tablename#', a.Name) FROM sysobjects a 
WHERE xtype = 'u'
OPEN C1
FETCH NEXT FROM C1 INTO @SQL
WHILE @@FETCH_STATUS = 0 BEGIN
     -- do work here
     PRINT @SQL
     FETCH NEXT FROM C1 INTO @SQL
END
CLOSE C1
DEALLOCATE C1

HTH Neven
> Yes, thanks. That is the solution at the moment, I thought there may be a more elegant one :-)
> Crosstalk looks to be a brilliant solution for a number of issues here (mixed .net and native).
>
> ________________________________
>
> From: delphi-bounces at listserver.123.net.nz on behalf of Xander van der Merwe
> Sent: Tue 3/04/2012 9:51 a.m.
> To: 'NZ Borland Developers Group - Delphi List'
> Subject: Re: [DUG] SqlDependency
>
>
>
> I very simple (but reliable) alternative approach might be to periodically (say every 30 seconds or whatever makes sense for your app) fire a simple query like "select max(DateUpdated) from MyTable" (assuming you have a DateUpdated column) and if the result returned is greater than the last value you have, you just do a refresh on that particular table/query in the UI.
>
>
>
> Regards
>
>
>
> From: delphi-bounces at listserver.123.net.nz [mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of David O'Brien
> Sent: Tuesday, 3 April 2012 8:47 a.m.
> To: NZ Borland Developers Group - Delphi List
> Subject: Re: [DUG] SqlDependency
>
>
>
> Thanks.
>
>
>
> From: delphi-bounces at listserver.123.net.nz [mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Jolyon Smith
> Sent: Tuesday, 3 April 2012 8:08 a.m.
> To: NZ Borland Developers Group - Delphi List
> Subject: Re: [DUG] SqlDependency
>
>
>
> As far as I can tell, SQLDepedency is just a high level wrapper around SQL Server Query Notifications - you could (if someone hasn't done it already) create such a high level wrapper yourself, or just build what you need on the core services themselves.  Instead of Google'ing "SQLDependency", try instead "SQL Server Query Notification".
>
> These articles in particular may be useful:
>
> http://msdn.microsoft.com/en-us/library/ms130764.aspx
>
> http://www.simple-talk.com/sql/t-sql-programming/using-and-monitoring-sql-2005-query-notification/
>
> You will note that there is quite a bit of infrastructure required in the DB itself to get this stuff up and running and there are some quite specific conditions that have to be met by the query itself - it's not just a case of "notify me about this query".  At the very least you need a queue and Service Broker to be running.  Whether you are in a position to put that infrastructure in place may determine whether or not this approach is even viable in your case.
>
>
> hth
>
>
>
>
>
> On 3 April 2012 02:33, Jackson Gomes<jfdmg at hotmail.com>  wrote:
>
> you may try CrossTalk, which allows you to import .NET classes and Libs and use them on Delphi
>
>
>
> http://www.atozed.com/crosstalk/index.en.aspx
>
> ________________________________
>
> Date: Mon, 2 Apr 2012 11:49:25 +1200
> From: Dave at iccs.co.nz
> To: delphi at listserver.123.net.nz
> Subject: [DUG] SqlDependency
>
>
>
> I would like to get SQL Server (2008 R2) to notify my app when data in a table changes.
>
> I'm looking for something like the SqlDependency class in .net, but for an XE2 VCL (Pascal) app.
>
> Does anyone know of a component, class, or way to replicate this (Not .net)?
>
>
>
> Cheers,
>
> Dave.
>
>
>
> _______________________________________________ NZ Borland Developers Group - Delphi mailing list Post: delphi at listserver.123.net.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
>
>
>
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20120403/9a47661d/attachment-0001.html 


More information about the Delphi mailing list