Example of TMSQuery TDBGrid and thread

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

Example of TMSQuery TDBGrid and thread

Post by brace » Mon 15 Feb 2010 15:35

May you post or describe an example on how to update a dataaware conponent (like a TDBGrid) using a TMSquery executed in a thread?

Basically a simple example in which i click a button, a query is run (for example select * from sysprocesses from sql server master db) and when the query returns the DBGrid is updated?

Thanks.

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 16 Feb 2010 09:13

When TMSQuery is executed in a separated thread you can use TDBGrid the same way as TMSQuery is executed in the main thread. TDBGrid gets data from TMSQuery in the same way in both cases.

brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

Post by brace » Tue 16 Feb 2010 12:02

Is it safe to do the following?

Query is a TMSQuery placed on the form.
a DBGrid (I am using a cxGrid from DevExpress by the way) datasource a TMSquery are connected as usual.

constructor TQThread.Create(Query: TMSQuery);
begin
inherited Create(True);
FQuery := Query;
FreeOnTerminate := True;
Resume;
end;

procedure TQThread.Execute;
begin
FQuery.Open;
end;

procedure TForm5.btnThreadClick(Sender: TObject);
begin
TQThread.Create(Query1);
end;

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 16 Feb 2010 14:59

Your code is correct. SDAC can work in multithreaded applications, so it is thread safe.

Alternatively you can implement similar functionality using the NonBlocking mode or FetchAll=False mode. You can find more detailed information about this in the SDAC help.

brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

Post by brace » Tue 16 Feb 2010 17:34

I tried with

TMSConnection.Options.MultipleActiveResultsSet := True

TMSQuery.Options.NonBlocking := True

but as i open the query the application "hangs", I cannot for example type in a memo. While I can do this with the above thread approach.

Am I doing something wrong?

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Wed 17 Feb 2010 08:34

Please make sure that you use the SQL Native Client provider. For this set the TMSConnection.Options.Provider property to the prNativeClient value.
Also you can use FetchAll=False mode.

brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

Post by brace » Wed 17 Feb 2010 09:44

But with FetchAll=False I don't have all the records in the grid.

I tried as you suggest, this is from dfm, anyway as I open the TMSQuery I cannot do any other thing (while I can do it when I use threads explicitly as described above).

object MSConnection1: TMSConnection
Options.MultipleActiveResultSets = True
Options.Provider = prNativeClient
Options.NativeClientVerison = ncAuto // this is not published in the object inspector
end
object MSQuery1: TMSQuery
Connection = MSConnection1
Options.NonBlocking = True
end

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Wed 17 Feb 2010 10:01

Ok, use threads.

brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

Post by brace » Wed 17 Feb 2010 13:15

So the non blocking feature is not a substitute od threads.

Post Reply