Original: http://www.cnblogs.com/djcsch2001/articles/2382559.html
There are 3 problems with ADO multithreaded database queries:
1. CoInitialize is not called (CoInitialize is not called), so you must call CoInitialize and CoUninitialize before using any Dbgo object. Failure to invoke CoInitialize results in an exception of "CoInitialize is not called".
2. Canvas is not allowed to paint (canvas does not allow drawing); Therefore, the main thread must be notified through the synchronize process to access any control on the master form.
3. You cannot use the main ADO connection (main tadoconnection cannot be used!); Therefore, threads cannot use tadoconnection objects in the main thread, and each thread must create its own database connection.
Delphi2007 after installation there is a Shared/data file in the X:/program files/common files/codegear Dbdemos.mdb directory, which is used as an example of testing. The Customer table in Dbdemos.mdb holds the client information, and the order information is saved in the Orders table.
The test program flow is basically this: put the tadoconnection and Tquery controls on the main form, and at startup this tquery isolate the customer code CUSTNO and company name from the Customers table, put it in three combox boxes, Select the customer company name in three list boxes, establish three threads according to the customer code corresponding to the company name, and query the sales date in the Orders table Saledate respectively into the listbox.
{main form code}
- Unit Main;
- Interface
- Uses
- Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
- Dialogs, DB, ADODB, Stdctrls;
- Type
- TForm2 = Class (Tform)
- Combobox1:tcombobox;
- Combobox2:tcombobox;
- Combobox3:tcombobox;
- Listbox1:tlistbox;
- Listbox2:tlistbox;
- Listbox3:tlistbox;
- Button1:tbutton;
- Adoconnection1:tadoconnection;
- Adoquery1:tadoquery;
- Label1:tlabel;
- Label2:tlabel;
- Label3:tlabel;
- procedure formcreate (sender:tobject);
- procedure Button1Click (sender:tobject);
- Private
- {Private declarations}
- Public
- {public declarations}
- end;
- Var
- Form2:tform2;
- Implementation
- Uses adothread;
- {$R *.DFM}
- Procedure TForm2. Button1Click (Sender:tobject);
- Const
- sql_const=' Select saledate from orders where Custno =%d ';
- Var
- C1,c2,c3:integer;
- S1,S2,S3:string;
- Begin
- //Get three selection box customer's code
- C1:=integer (ComboBox1. Items. Objects[combobox1. ItemIndex]);
- C2:=integer (ComboBox2. Items. Objects[combobox2. ItemIndex]);
- C3:=integer (ComboBox3. Items. Objects[combobox3. ItemIndex]);
- //Generate SQL query statements
- S1:=format (Sql_const,[c1]);
- S2:=format (SQL_CONST,[C2]);
- S3:=format (SQL_CONST,[C3]);
- //Three threads simultaneous query
- Tadothread. Create (S1,LISTBOX1,LABEL1);
- Tadothread. Create (S2,LISTBOX2,LABEL2);
- Tadothread. Create (S3,LISTBOX3,LABEL3);
- End
- Procedure TForm2. Formcreate (Sender:tobject);
- Var
- strSQL:string;
- Begin
- strsql:=' SELECT custno,company from customer ';
- ADOQuery1. Close;
- ADOQuery1. SQL. Clear;
- ADOQuery1. SQL. ADD (strSQL);
- ADOQuery1. Open;
- ComboBox1. Clear;
- ComboBox2. Clear;
- ComboBox3. Clear;
- //Fill in the ComboBox with customer company and related Custno
- While not ADOQuery1. Eof do
- begin
- ComboBox1. AddItem (ADOQuery1. fields[1].asstring,
- TObject (ADOQuery1. fields[0]. Asinteger));
- ADOQuery1. Next;
- end;
- ComboBox2. Items. Assign (ComboBox1. Items);
- ComboBox3. Items. Assign (ComboBox1. Items);
- //default check First
- ComboBox1. ItemIndex: = 0;
- ComboBox2. ItemIndex: = 0;
- ComboBox3. ItemIndex: = 0;
- End
- End. {ADO querying multithreaded units}
- Unit Adothread;
- Interface
- Uses
- Classes,stdctrls,adodb;
- Type
- Tadothread = Class (TThread)
- Private
- {Private declarations}
- Flistbox:tlistbox;
- Flabel:tlabel;
- connstring:widestring;
- Fsqlstring:string;
- procedure Updatecount;
- protected
- procedure Execute; override;
- Public
- Constructor Create (SQL:string;lb:tlistbox; Lab:tlabel);
- end;
- Implementation
- Uses Main,sysutils,activex;
- {Tadothread}
- Constructor Tadothread. Create (SQL: string; Lb:tlistbox; Lab:tlabel);
- Begin
- Connstring:=form2. ADOConnection1. ConnectionString;
- flistbox:=lb;
- Flabel:=lab;
- Fsqlstring:=sql;
- Inherited Create (False);
- End
- Procedure Tadothread. Execute;
- Var
- Qry:tadoquery;
- I:integer;
- Begin
- {Place thread code here}
- Freeonterminate:=true;
- CoInitialize (nil); //must be called (requires uses ActiveX)
- Qry:=tadoquery. Create (nil);
- Try
- Qry. connectionstring:=connstring; //must have a connection of their own
- Qry. Close;
- Qry. SQL. Clear;
- Qry. SQL. ADD (fsqlstring);
- Qry. Open;
- Flistbox. Clear;
- for I: = 0 to + do//In order to perform a long repetition of the data set 101 times
- begin
- While not Qry. Eof and not Terminated do
- begin
- Flistbox. AddItem (Qry. fields[0].asstring,nil);
- //If you do not call synchronize, you will receive a canvas Does not allow Drawing
- Synchronize (Updatecount);
- Qry. Next;
- end;
- Qry. First;
- Flistbox. AddItem (' ******* ',nil);
- end;
- finally
- Qry. Free;
- end;
- CoUninitialize;
- End
- Procedure Tadothread. Updatecount;
- Begin
- Flabel. Caption:=inttostr (Flistbox. Items. Count);
- End
- End.
The results of the program run as follows:
You can see three threads executing at the same time. The first 32 thread conditions are the same as the results of a query.
"Turn" Delphi Multi-Threaded Learning (9): Multi-threaded database query (ADO)