[delphi技术]Delphi多线程数据库查询(ADO)

ADO多线程数据库查询通常会出现3个问题:

1、CoInitialize 没有调用(CoInitialize was not called);所以,在使用任何dbGo对象前,必须手 调用CoInitialize和CoUninitialize。调用CoInitialize失败会产生"CoInitialize was not called"例外。

2、画布不允许绘画(Canvas does not allow drawing);所以,必须通过Synchronize过程来通知主线程访问主窗体上的任何控件。

3、不能使用主ADO连接(Main TADoConnection cannot be used!);所以,线程中不能使用主线程中TADOConnection对象,每个线程必须创建自己的数据库连接。

Delphi2007安装后在X:\Program Files\Common Files\CodeGear Shared\Data目录下有一个dbdemos.mdb文件,用来作为测试的例子。dbdemos.mdb中的customer表保存了客户信息,orders表中保存了订单信息。

测试程序流程大致是这样的:在主窗体上放TADOConnection和TQuery控件,启动时这个TQuery从Customer表中查出客户编码CustNo和公司名称Company,放到三个Combox框中,分别在三个列表框中选定客户公司名称,按照公司名称所对应的客户代码建立三个线程同时在orders表中查询销售日期SaleDate分别填入ListBox中。

 1 {主窗体代码}
 2 unit Main;
 3 interface
 4   uses Windows, Messages, SysUtils, Variants,
 5     Classes, Graphics, Controls, Forms, 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 { Private declarations }
23   public { Public declarations }
24   end;
25 var Form2: TForm2;
26 implementation
27 uses
28   ADOThread;
29   {$R *.dfm}
30
31 procedure TForm2.Button1Click(Sender: TObject);
32 const
33   SQL_CONST=‘Select SaleDate from orders where CustNo = %d‘;
34   var c1,c2,c3:Integer; s1,s2,s3:string;
35 begin //取得三个选择框客户的编码
36   c1:=Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
37   c2:=Integer(ComboBox2.Items.Objects[ComboBox2.ItemIndex]);
38   c3:=Integer(ComboBox3.Items.Objects[ComboBox3.ItemIndex]); //生成SQL 查询语句
39   s1:=Format(SQL_CONST,[c1]);
40   s2:=Format(SQL_CONST,[c2]);
41   s3:=Format(SQL_CONST,[c3]); //三个线程同时查询
42   TADOThread.Create(s1,ListBox1,Label1);
43   TADOThread.Create(s2,ListBox2,Label2);
44   TADOThread.Create(s3,ListBox3,Label3);
45 end;
46
47 procedure TForm2.FormCreate(Sender: TObject);
48 var
49   strSQL:string;
50 begin
51   strSQL:=‘SELECT CustNo,Company FROM customer‘;
52   ADOQuery1.Close;
53   ADOQuery1.SQL.Clear;
54   ADOQuery1.SQL.Add(strSQL);
55   ADOQuery1.Open;
56   ComboBox1.Clear;
57   ComboBox2.Clear;
58   ComboBox3.Clear; //将客户Company和相关CustNo填到ComboBox中
59   while not ADOQuery1.Eof do
60   begin
61     ComboBox1.AddItem(ADOQuery1.Fields[1].asString, TObject(ADOQuery1.Fields[0].AsInteger));
62     ADOQuery1.Next;
63   end;
64   ComboBox2.Items.Assign(ComboBox1.Items);
65   ComboBox3.Items.Assign(ComboBox1.Items); // 默认选中第一个
66   ComboBox1.ItemIndex := 0;
67   ComboBox2.ItemIndex := 0;
68   ComboBox3.ItemIndex := 0;
69 end;
70 end.
 1 {ADO查询多线程单元}
 2 unit ADOThread;
 3 interface
 4 uses
 5   Classes,StdCtrls,ADODB;
 6 type TADOThread = class(TThread)
 7   private { Private declarations }
 8   FListBox:TListBox;
 9   FLabel:TLabel;
10   ConnString:WideString;
11   FSQLString:string;
12   procedure UpdateCount;
13   protected procedure Execute; override;
14   public constructor Create(SQL:string;LB:TListBox;Lab:TLabel);
15 end;
16
17 implementation
18 uses
19   Main,SysUtils,ActiveX;
20   { TADOThread }
21
22 constructor TADOThread.Create(SQL: string; LB: TListBox;Lab:TLabel);
23 begin
24   ConnString:=Form2.ADOConnection1.ConnectionString;
25   FListBox:=LB;
26   FLabel:=Lab;
27   FSQLString:=SQL;
28   Inherited Create(False);
29 end;
30
31 procedure TADOThread.Execute;
32 var
33   Qry:TADOQuery;
34   i:Integer;
35 begin { Place thread code here }
36   FreeOnTerminate:=True;
37   CoInitialize(nil); //必须调用(需Uses ActiveX)
38   Qry:=TADOQuery.Create(nil);
39   try
40     Qry.ConnectionString:=ConnString; //必须有自己的连接
41     Qry.Close;
42     Qry.SQL.Clear;
43     Qry.SQL.Add(FSQLString);
44     Qry.Open;
45     FListBox.Clear;
46     for i := 0 to 100 do //为了执行久点重复历遍数据集101次
47     begin
48       while not Qry.Eof And not Terminated do
49       begin
50         FListBox.AddItem(Qry.Fields[0].asstring,nil); //如果不调用Synchronize,会出现Canvas Does NOT Allow Drawing
51         Synchronize(UpdateCount);
52         Qry.Next;
53       end;
54       Qry.First;
55       FListBox.AddItem(‘*******‘,nil);
56     end;
57   finally
58     Qry.Free;
59   end;
60   CoUninitialize;
61 end;
62
63 procedure TADOThread.UpdateCount;
64 begin
65   FLabel.Caption:=IntToStr(FListBox.Items.Count);
66 end;
67 end.

程序运行结果可以看到三个线程同时执行。第一第三两个线程条件一样,查询的结果也一样。

时间: 2024-08-04 12:05:14

[delphi技术]Delphi多线程数据库查询(ADO)的相关文章

Delphi多线程数据库查询(ADO)

ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用(CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoInitialize和CoUninitialize.调用CoInitialize失败会产生"CoInitialize was not called"例外. 2.画布不允许绘画(Canvas does not allow drawing):所以,必须通过Synchronize过程来通知主线程访问主窗体

【转】Delphi多线程学习(9):多线程数据库查询(ADO)

原文:http://www.cnblogs.com/djcsch2001/articles/2382559.html ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用(CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoInitialize和CoUninitialize.调用CoInitialize失败会产生"CoInitialize was not called"例外. 2.画布不允许绘画(Can

阿庆SQL智能查询分析器,使用delphi开发的一个数据库查询分析管理工具.分享给大家

为方便自己工作,使用delphi开发的一个数据库查询分析管理工具.分享给大家,具体以下特点: 1.由于使用ADO连接,理论支持SQL Server.Access.MySQL.Oracle等所有数据库 2.支持SQL关键词自动提示 3.支持表名自动提示 4.支持表字段自动提示 5.支持SQ关键词.表名.表字段不同颜色显示 6.支持SQL语句注释(包括ACCESS) 7.支持选择部分文字执行SQL语句 8.查询结果支持增加.修改.编辑 9.绿色程序无附加文件,只有一个文件即可运行,文件大小只有400

ADO多线程数据库查询

ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用 (CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoInitialize和CoUninitialize.调用CoInitialize失败会产生"CoInitialize was not called"例外. 2.画布不允许绘画 (Canvas does not allow drawing):所以,必须通过Synchronize过程来通知主线程访问主

[delphi技术]Delphi MSComm 实时串口通讯

Delphi  MSComm 实时串口通讯 MSComm控件具有丰富的与串口通信密切相关的属性,提供了对串口进行的多种操作,进而使串行通信变得十分简便.MSComm的控件属性较多,常用的属性如下:1).CommPort:设置或返回串行端口号,缺省为1.2).Setting:设置或返回串口通信参数,格式为“波特率,奇偶校验位,数据位,停止位”.例如:MSComm1.Setting:=9600,n,8,13).PortOpen:打开或关闭串行端口,格式为:MSComm1.PortOpen:={Tru

[delphi技术]Delphi资源文件(全面分析于使用)

Delphi资源文件(全面分析之位图.光标.图标.AVI.JPEG.Wave) 几乎每个Windows应用程序都使用图标.图片.光标等资源.资源是程序的一部分,但是它是不可执行代码.下面我们就详细介绍资 源文件在Delphi5中建立和使用方法. 1.把资源放到Exe文件的优点  由于定位资源比在磁盘中定位文件花费时间少,所以应用程序执行会更快.多种资源可以放在一个文件中,减少了图标.图片.光标 等文件数量.保存资源文件时不要和工程名相同,因为Delphi创建工程时会自动创建一个和工程名相同的资源

[delphi技术]delphi源代码--后延函数

说明: 1)TTtimer控件 TTtimer控件的实质是调用WindowsAPI定时函数SetTimer和KillTimer来实现的,并简化了对WM_TIMER消息的处理过程.通过设置OnTimer事件和Interval属性,我们可以很方便的产生一些简单的定时事件. 2)Sleep函数 Sleep函数用来使程序的执行延时给定的时间值.Sleep的调用形式为Sleep(milliseconds),暂停当前的进程milliseconds毫秒.Sleep的实现方法其实也是调用Windows API的

[delphi技术]Delphi常见图象格式转换技术

TJPEGScale = (jsFullSize, jsHalf, jsQuarter, jsEighth);//图片大小(全部,1/2,1/4,1/8)TBitmap.pixelFormat:=pf8bit;//图片位数(8,24) 给大家提供几个常用的图象格式转换方法和其转换函数希望可以对你有帮助1. ICO图标转换BMP格式2. 32x32 BMP格式图象转换为 ICO格式3.转换BMP->JPEG文件格式4.JPEG 转换为BMP函数5.Bmp转换为JPEG文件格式函数---------

[delphi技术] (SQL DBE、ADO连接)+(Firebird火鸟+DbExpress)+(VF DBF数据库)+(DB Paradox)

DBE 连接SQL Server显然用ADO或DBEXPRESS更有优势,起码连接起来比较方便. BDE的话可以用如下方法:(以下以Delphi7为例,其它版本的DELPHI请自己摸索一下,不过基本相差不大) 1.启动Delphi,选择菜单项 Database->Explorer,在左侧Database列表里面右键,选择New,在弹出的驱动对话框里面选择MSSQL,确定,然后在左侧可以更改名字,如:TEST,然后在选中TEST在右侧选项里面添入ServerName(服务器名) UserName(