"Turn" Delphi Multi-Threaded Learning (9): Multi-threaded database query (ADO)

Source: Internet
Author: User

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}

  1. Unit Main;
  2. Interface
  3. Uses
  4. Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, DB, ADODB, Stdctrls;
  6. Type
  7. TForm2 = Class (Tform)
  8. Combobox1:tcombobox;
  9. Combobox2:tcombobox;
  10. Combobox3:tcombobox;
  11. Listbox1:tlistbox;
  12. Listbox2:tlistbox;
  13. Listbox3:tlistbox;
  14. Button1:tbutton;
  15. Adoconnection1:tadoconnection;
  16. Adoquery1:tadoquery;
  17. Label1:tlabel;
  18. Label2:tlabel;
  19. Label3:tlabel;
  20. procedure formcreate (sender:tobject);
  21. procedure Button1Click (sender:tobject);
  22. Private
  23. {Private declarations}
  24. Public
  25. {public declarations}
  26. end;
  27. Var
  28. Form2:tform2;
  29. Implementation
  30. Uses adothread;
  31. {$R *.DFM}
  32. Procedure TForm2.  Button1Click (Sender:tobject);
  33. Const
  34. sql_const=' Select saledate from orders where Custno =%d ';
  35. Var
  36. C1,c2,c3:integer;
  37. S1,S2,S3:string;
  38. Begin
  39. //Get three selection box customer's code
  40. C1:=integer (ComboBox1. Items. Objects[combobox1.  ItemIndex]);
  41. C2:=integer (ComboBox2. Items. Objects[combobox2.  ItemIndex]);
  42. C3:=integer (ComboBox3. Items. Objects[combobox3.  ItemIndex]);
  43. //Generate SQL query statements
  44. S1:=format (Sql_const,[c1]);
  45. S2:=format (SQL_CONST,[C2]);
  46. S3:=format (SQL_CONST,[C3]);
  47. //Three threads simultaneous query
  48. Tadothread.  Create (S1,LISTBOX1,LABEL1);
  49. Tadothread.  Create (S2,LISTBOX2,LABEL2);
  50. Tadothread.  Create (S3,LISTBOX3,LABEL3);
  51. End
  52. Procedure TForm2.  Formcreate (Sender:tobject);
  53. Var
  54. strSQL:string;
  55. Begin
  56. strsql:=' SELECT custno,company from customer ';
  57. ADOQuery1.  Close;
  58. ADOQuery1. SQL.  Clear;
  59. ADOQuery1. SQL.  ADD (strSQL);
  60. ADOQuery1.  Open;
  61. ComboBox1.  Clear;
  62. ComboBox2.  Clear;
  63. ComboBox3.  Clear;
  64. //Fill in the ComboBox with customer company and related Custno
  65. While not ADOQuery1. Eof do
  66. begin
  67. ComboBox1. AddItem (ADOQuery1. fields[1].asstring,
  68. TObject (ADOQuery1. fields[0].  Asinteger));
  69. ADOQuery1.  Next;
  70. end;
  71. ComboBox2. Items. Assign (ComboBox1.  Items);
  72. ComboBox3. Items. Assign (ComboBox1.  Items);
  73. //default check First
  74. ComboBox1.  ItemIndex: = 0;
  75. ComboBox2.  ItemIndex: = 0;
  76. ComboBox3.  ItemIndex: = 0;
  77. End
  78. End. {ADO querying multithreaded units}
  79. Unit Adothread;
  80. Interface
  81. Uses
  82. Classes,stdctrls,adodb;
  83. Type
  84. Tadothread = Class (TThread)
  85. Private
  86. {Private declarations}
  87. Flistbox:tlistbox;
  88. Flabel:tlabel;
  89. connstring:widestring;
  90. Fsqlstring:string;
  91. procedure Updatecount;
  92. protected
  93. procedure Execute; override;
  94. Public
  95. Constructor Create (SQL:string;lb:tlistbox;  Lab:tlabel);
  96. end;
  97. Implementation
  98. Uses Main,sysutils,activex;
  99. {Tadothread}
  100. Constructor Tadothread. Create (SQL: string; Lb:tlistbox;  Lab:tlabel);
  101. Begin
  102. Connstring:=form2. ADOConnection1.  ConnectionString;
  103. flistbox:=lb;
  104. Flabel:=lab;
  105. Fsqlstring:=sql;
  106. Inherited Create (False);
  107. End
  108. Procedure Tadothread.  Execute;
  109. Var
  110. Qry:tadoquery;
  111. I:integer;
  112. Begin
  113. {Place thread code here}
  114. Freeonterminate:=true;
  115. CoInitialize (nil); //must be called (requires uses ActiveX)
  116. Qry:=tadoquery.  Create (nil);
  117. Try
  118. Qry.   connectionstring:=connstring; //must have a connection of their own
  119. Qry.  Close;
  120. Qry. SQL.  Clear;
  121. Qry. SQL.  ADD (fsqlstring);
  122. Qry.  Open;
  123. Flistbox.  Clear;
  124. for I: = 0 to + do//In order to perform a long repetition of the data set 101 times
  125. begin
  126. While not Qry. Eof and not Terminated do
  127. begin
  128. Flistbox. AddItem (Qry.  fields[0].asstring,nil);
  129. //If you do not call synchronize, you will receive a canvas Does not allow Drawing
  130. Synchronize (Updatecount);
  131. Qry.  Next;
  132. end;
  133. Qry.  First;
  134. Flistbox.  AddItem (' ******* ',nil);
  135. end;
  136. finally
  137. Qry.  Free;
  138. end;
  139. CoUninitialize;
  140. End
  141. Procedure Tadothread.  Updatecount;
  142. Begin
  143. Flabel. Caption:=inttostr (Flistbox. Items.  Count);
  144. End
  145. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.