【转】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、画布不允许绘画(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中。
{主窗体代码}
- 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
- //取得三个选择框客户的编码
- c1:=Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
- c2:=Integer(ComboBox2.Items.Objects[ComboBox2.ItemIndex]);
- c3:=Integer(ComboBox3.Items.Objects[ComboBox3.ItemIndex]);
- //生成SQL 查询语句
- s1:=Format(SQL_CONST,[c1]);
- s2:=Format(SQL_CONST,[c2]);
- s3:=Format(SQL_CONST,[c3]);
- //三个线程同时查询
- 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;
- //将客户Company和相关CustNo填到ComboBox中
- 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);
- // 默认选中第一个
- ComboBox1.ItemIndex := 0;
- ComboBox2.ItemIndex := 0;
- ComboBox3.ItemIndex := 0;
- end;
- end.{ADO查询多线程单元}
- 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); //必须调用(需Uses ActiveX)
- Qry:=TADOQuery.Create(nil);
- try
- Qry.ConnectionString:=ConnString; //必须有自己的连接
- Qry.Close;
- Qry.SQL.Clear;
- Qry.SQL.Add(FSQLString);
- Qry.Open;
- FListBox.Clear;
- for i := 0 to 100 do //为了执行久点重复历遍数据集101次
- begin
- while not Qry.Eof And not Terminated do
- begin
- FListBox.AddItem(Qry.Fields[0].asstring,nil);
- //如果不调用Synchronize,会出现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.
程序运行结果如下:
可以看到三个线程同时执行。第一第三两个线程条件一样,查询的结果也一样。
【转】Delphi多线程学习(9):多线程数据库查询(ADO)的更多相关文章
- C#多线程学习(一) 多线程的相关概念(转)
什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程?线程是程序中的一个执行流,每个线程都有自己的专有寄 ...
- C#多线程学习(一) 多线程的相关概念
什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程?线程是程序中的一个执行流,每个线程都有自己的专有寄 ...
- [转载]C#多线程学习(一) 多线程的相关概念
原文地址:http://www.cnblogs.com/xugang/archive/2008/04/06/1138856.html 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的 ...
- C#多线程学习(四) 多线程的自动管理(线程池)
在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPo ...
- MySQL学习基础之一 — 数据库查询
廖大神的练手神器:在线SQL: https://www.liaoxuefeng.com/wiki/1177760294764384/1179611432985088 运行MySQL等实际的数据库软件, ...
- Java学习笔记 DbUtils数据库查询和log4j日志输出 使用
DbUtils使用 QueryRunner DbUtils中定义了一个数据库操作类QueryRunner,所有的数据库操作CRUD都是通过此类来完成. 此类是线程安全的 方法名 对应sql语句 exc ...
- C++并发与多线程学习笔记--多线程数据共享问题
创建和等待多个线程 数据和共享问题分析 只读的数据 有读有写 其他案例 共享数据的保护案例代码 创建和等待多个线程 服务端后台开发就需要多个线程执行不同的任务.不同的线程执行不同任务,并返回执行结果. ...
- 学习练习 java数据库查询小题
10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) 11. 查询每门课的平均成绩. 12.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数. 13.查询分数大于7 ...
- C#多线程学习(五) 多线程的自动管理(定时器)
Timer类:设置一个定时器,定时执行用户指定的函数. 定时器启动后,系统将自动建立一个新的线程,执行用户指定的函数. 初始化一个Timer对象: Timer timer ...
- 【转】C#多线程学习
C#多线程学习(一) 多线程的相关概念 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程?线程是程序 ...
随机推荐
- 给编译好的DLL增加签名
两个步骤,记录如下,主要用在silverlight中引用的dll要签名时: "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm ...
- 运算符重载 C++ 编程思想
class Integer{ int i; public: Integer(int ii) : i(ii) {} const Integer operator+(const Integer& ...
- 动软Model 模板 生成可空类型字段
动软代码 生成可空类型 <#@ template language="c#" HostSpecific="True" #> <#@ outpu ...
- SQL Server 2008 备份改进版
1.Add compressing function with 7-Zip 2.With tool win.rar code so you can change it if you want USE ...
- 数往知来C#之 String 集合 深拷与浅拷 序列化<五>
C# 基础常用string方法篇 复习. 1.引用类型与值类型 -->文件的复制与快捷方式的复制 2.垃圾回收 3.静态与非静态 -->如何定义静态成员与静态类 --> ...
- PHP相关图书推荐
PHP和MySQL Web开发(原书第4版) 作 者 [澳] Luke Welling,[澳] Luke Welling 著:武欣 等 译 出 版 社 机械工业出版社 出版时间 2009-0 ...
- SandDock 应用示例
直接上代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...
- ASP.NET MVC3 系列教程 - 部署你的WEB应用到IIS 6.0
I:ASP.NET MVC3 部署的前期工作 1.确认部署的服务器操作系统环境 首先我们确认服务器的操作系统版本 可以从系统命令行工具里输入: systeminfo 获取相关操作系统信息例如 然后再确 ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- (转载)Java里快如闪电的线程间通讯
转自(http://www.infoq.com/cn/articles/High-Performance-Java-Inter-Thread-Communications) 这个故事源自一个很简单的想 ...