Q
qrsgroup
Unregistered / Unconfirmed
GUEST, unregistred user!
多线程中使用ado插入数据
我现在同时创建2个线程,第一个读取1.txt,第二个读取2.txt
这样就线程异常结束
报错----“无法更新;当前被锁定”
代码如下:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ADODB, activeX, DB;
type
TAddDbThread = class(TThread)
private
FileName: string;
protected
procedure Execute; override;
published
constructor create(savename:string);
destructor Destroy;
end;
implementation
uses IMCode, Unit1;
procedure TAddDbThread.Execute;
var
StrList,TS: TStringList;
CountNum,Num:LongInt;
StrValue: string;
I: Integer;
FieldName:String;
Sqlstr:String;
ADOQuery:TADOQuery;
ADOConnection:TADOConnection;
begin
try
StrList:= TStringList.Create;
StrList.Delimiter:= ',';
filename := filename + '.txt';
TS := TStringList.Create;
TS.LoadFromFile(filename);
CountNum := TS.Count;
CoInitialize(nil);
ADOQuery := TADOQuery.Create(nil);
ADOConnection := TADOConnection.Create(nil);
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ZTDB.mdb;Persist Security Info=False';
ADOConnection.Connected := true;
ADOQuery.Connection := ADOConnection;
form1.Memo1.Lines.Add('线程'+ filename +'启动');
for Num := 0 to CountNum-1 do
begin
StrValue := TS.Strings[Num];
StrList.DelimitedText:= StrValue;
FieldName:= #39 + SpellCode(StrList.Strings[1],1,100) + #39;
Sqlstr := 'insert into tfinfo (';
SqlStr := Sqlstr + fieldstr;
SqlStr := Sqlstr + ') values(';
SqlStr := Sqlstr + #39 + StrList.Strings[0] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[1] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[2] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[3] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[4] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[5] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[6] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[7] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[8] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[9] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[10] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[11] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[12] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[13] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[14] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[15] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[16] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[17] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[18] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[19] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[20] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[21] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[22] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[23] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[24] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[25] + #39 + ',';
SqlStr := Sqlstr + FieldName;
SqlStr := Sqlstr + ')';
ADOQuery.close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text := Sqlstr;
ADOQuery.ExecSQL;
end;
TS.Free;
StrList.Free;
adoquery.Close;
form1.Memo1.Lines.Add('线程'+ filename +'正常结束');
except
form1.Memo1.Lines.Add('线程'+ filename +'异常结束');
end;
end;
constructor TAddDbThread.create(SaveName: string);
begin
inherited create(false);
FreeOnTerminate := true;
FileName := SaveName;
end;
destructor TAddDbThread.Destroy;
begin
inherited destroy;
end;
end.
我现在同时创建2个线程,第一个读取1.txt,第二个读取2.txt
这样就线程异常结束
报错----“无法更新;当前被锁定”
代码如下:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ADODB, activeX, DB;
type
TAddDbThread = class(TThread)
private
FileName: string;
protected
procedure Execute; override;
published
constructor create(savename:string);
destructor Destroy;
end;
implementation
uses IMCode, Unit1;
procedure TAddDbThread.Execute;
var
StrList,TS: TStringList;
CountNum,Num:LongInt;
StrValue: string;
I: Integer;
FieldName:String;
Sqlstr:String;
ADOQuery:TADOQuery;
ADOConnection:TADOConnection;
begin
try
StrList:= TStringList.Create;
StrList.Delimiter:= ',';
filename := filename + '.txt';
TS := TStringList.Create;
TS.LoadFromFile(filename);
CountNum := TS.Count;
CoInitialize(nil);
ADOQuery := TADOQuery.Create(nil);
ADOConnection := TADOConnection.Create(nil);
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ZTDB.mdb;Persist Security Info=False';
ADOConnection.Connected := true;
ADOQuery.Connection := ADOConnection;
form1.Memo1.Lines.Add('线程'+ filename +'启动');
for Num := 0 to CountNum-1 do
begin
StrValue := TS.Strings[Num];
StrList.DelimitedText:= StrValue;
FieldName:= #39 + SpellCode(StrList.Strings[1],1,100) + #39;
Sqlstr := 'insert into tfinfo (';
SqlStr := Sqlstr + fieldstr;
SqlStr := Sqlstr + ') values(';
SqlStr := Sqlstr + #39 + StrList.Strings[0] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[1] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[2] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[3] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[4] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[5] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[6] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[7] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[8] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[9] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[10] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[11] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[12] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[13] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[14] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[15] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[16] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[17] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[18] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[19] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[20] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[21] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[22] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[23] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[24] + #39 + ',';
SqlStr := Sqlstr + #39 + StrList.Strings[25] + #39 + ',';
SqlStr := Sqlstr + FieldName;
SqlStr := Sqlstr + ')';
ADOQuery.close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text := Sqlstr;
ADOQuery.ExecSQL;
end;
TS.Free;
StrList.Free;
adoquery.Close;
form1.Memo1.Lines.Add('线程'+ filename +'正常结束');
except
form1.Memo1.Lines.Add('线程'+ filename +'异常结束');
end;
end;
constructor TAddDbThread.create(SaveName: string);
begin
inherited create(false);
FreeOnTerminate := true;
FileName := SaveName;
end;
destructor TAddDbThread.Destroy;
begin
inherited destroy;
end;
end.