Delphi多线程学习(9):多线程数据库查询(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中。
{主窗体代码}
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:=Integ
相关文档:
Delphi之Tchart控件使用心得
最近在学习delphi,做一个软件练手,用到Tchart控件,有些粗略的使用心得,供delphi初学者参考。
TChart是delphi里面一个标准的图形显示控件。它可以静态设计(at design time)也可以动态生成。可下载Steema TeeChart 7,没有源代码,能用就好!
&nb ......
现有一系统不定时在同一目录下生成一批txt文件,内容大概如下;
20100112_164910,6,81406392,808,113,,
为方便统计,需要将这些txt文件中的数据读入数据表中.
现有两种方法:
一是直接将数据读入到数据表中的某一列中,再用sql 语句拆开成几列:
  ......
需要用到的一个函数:
LONG SetWindowLong(
HWND hWnd,
int nIndex,
LONG dwNewLong
);
其中nIndex GWL_EXSTYLE Retrieves the extended window styles.
dwNewLong WS_EX_TOOLWINDOW Creates a tool window; that is, a window intended to ......
一个线程的优先级取决于创建线程的进程的优先级(priority class)和线程本身相对的优先级(relative priority)。
1、进程优先级:
不同操作系统 优先级数目不同。Windows2000以上的版本列表如下:
Idle:(标志:IDLE_PRIORITY_CLASS,值为$40);
Below Normal:(标志:BEL ......