为什么我的ADo多线程不能并行插入数据(260分)

  • 主题发起人 主题发起人 jimmyl
  • 开始时间 开始时间
J

jimmyl

Unregistered / Unconfirmed
GUEST, unregistred user!
数据库是access,有一个线程单元
unit unDbThread;
interface
uses
Classes, AdoDB, UnConst, SysUtils, ActiveX;

type
TDbThread = class(TThread)
private
FAdoConnDB: TADOConnection;
FAdoQuery: TADOQuery;
FSqlText: string;
FDbOperType: TDbOperType;
FReturnDataSet: Boolean;
FThreadId: Integer;
procedure SetSqlText(ASQL: string);
procedure SqlRunning;
{ Private declarations }
protected
procedure Execute;
override;
public
constructor Create(AThreadID: Integer;
AdoConn: TAdoConnection;AdoQuery: TAdoQuery;
AConnStr: WideString;
AOperType: TDbOperType;
AReturnDataSet: Boolean);
destructor Destroy;
override;
property SqlText: string read FSqlText write SetSqlText;
end;

implementation
uses UnMain;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TDbThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TDbThread }
constructor TDbThread.Create(AThreadID: Integer;
AdoConn: TAdoConnection;AdoQuery: TAdoQuery;
AConnStr: WideString;
AOperType: TDbOperType ;
AReturnDataSet: Boolean);
begin
inherited Create(True);
FreeOnTerminate := True;
FDbOperType := AOperType;
FReturnDataSet := AReturnDataSet;
FSqlText := '';
FAdoConnDb := ADoConn;
FAdoQuery := AdoQuery;
{
FAdoConnDB := TAdoConnection.Create(nil);
FAdoQuery := TAdoQuery.Create(nil);
FAdoQuery.Connection := FAdoConnDB;
FAdoConnDB.ConnectionString := AConnStr;
}
FThreadID := AThreadID;
try
//FAdoConnDB.Connected := True;
except
//Terminate;
end;

end;

destructor TDbThread.Destroy;
begin
FAdoConnDB.Close;
FAdoQuery.Close;
FAdoQuery.Free;
FAdoConnDB.Free;

inherited Destroy;
end;

procedure TDbThread.SqlRunning;
var
I: Integer;
begin
try
for I := 0 to 0do
begin
FAdoQuery.SQL.Text := Format(SqlText,[FormatDateTime('hh:nn:ss',Now), FThreadID]);
case FDbOperType of
do
Open: FAdoQuery.Open;
do
ExecSQL: FAdoQuery.ExecSQL;
end;
FAdoQuery.Close;
end;
except
on E: Exceptiondo
WriteSystemLog(Format('%',[E.Message]));
end;
end;

procedure TDbThread.Execute;
var
I: Integer;
begin
//CoInitialize(nil);
I := 0;
while (I <=200)do
begin
Synchronize(SqlRunning);
Inc(I);
sleep(1);
end;
try
//Synchronize(SqlRunning);
finally
//CoUnInitialize;
end;

{
while not terminateddo
begin
sleep(100);
end;
}
terminate;
{ Place thread code here }
end;

procedure TDbThread.SetSqlText(ASQL: string);
begin
FSqlText := ASQl;
FAdoQuery.SQL.Clear;
FAdoQuery.SQL.Add(ASQL);
end;

end.

测试函数
procedure test;
var
str: string;
DbThread: TDbThread;
MyAdoQuery: array of TAdoQuery;
MyAdoConnection: array of TAdoConnection;
begin
if dmDB.ConnectDB then
begin
dmDb.TruncateTable('user');
SetLength(MyAdoQuery, 21);
SetLength(MyAdoConnection, 21);
for I := 0 to 20do
begin
str :='insert into user(user_name, gender, birthday, address, phone, comment) ' +
' values(''test'',''M'', ''%s'', ''test'',''%d'','' '')';
MyAdoConnection := TAdoConnection.Create(nil);
MyAdoQuery := TAdoQuery.Create(nil);
MyAdoConnection.ConnectionString := dmDb.AdoConnDB.ConnectionString;
MyAdoQuery.Connection := MyAdoCOnnection;
MyAdoConnection.Connected := true;

DbThread := TDbThread.Create(I, MyAdoConnection, MyAdoQuery, dmDb.AdoConnDB.ConnectionString,do
ExecSQL, False);
DbThread.SqlText := str;
DbThread.Resume;
end;
ShowMsgDlg('ok');
end;
end;
方法:采用一个线程使用一对AdoConnection, AdoQuery的方式
现象:主窗体执行test函数后,创建线程,界面就像死循环一样,等一会儿恢复正常,
我的想法是后台执行线程,主界面还能正常使用,请问该怎么改?线程单元写的有什么问题?
 
access不支持多用户
 
对数据库的修改请放在主线程
 
用SQL Server 2000数据库
 
后退
顶部