线程中动态生成ADODataSet和ADOConnection,但出错:'can not perform thisoperartor on a closed d

  • 主题发起人 yecloudy
  • 开始时间
Y

yecloudy

Unregistered / Unconfirmed
GUEST, unregistred user!
线程中动态生成ADODataSet和ADOConnection,但出错:'can not perform thisoperartor on a closed dataset‘ ,请救命啊,在线等。(0分)<br />下面是在线程中动态生成ADODataSet和ADOConnection,但出错:'can not perform this
operartor on a closed dataset ',请救命啊,在线等。
unit InsData;

interface

uses
Classes, ADODB,ActiveX,StrUtils,SysUtils;

type
TInsertDataThread = class(TThread)
private
{ Private declarations }
protected
ADODataSet: TADODataSet;
// ADOCommand: TADOCommand;
ADOConnection: TADOConnection;
Ip,Name,Size,Time,DirName,TxtName:Ansistring;
F:TextFile;
procedure Execute; override;
procedure AnalyzeDir(line:AnsiString);
procedure AnalyzeTxt(line:AnsiString);
public
constructor create(Sfilename:Ansistring);
Destructor Destroy;override;
end;

implementation

constructor TInsertDatathread.create(SFileName:AnsiString);
begin
inherited Create(true);
freeonterminate:=true;
CoInitialize(Nil);
ADOConnection := TADOConnection.Create( nil );
ADODataSet:= TADODataSet.Create(nil);
with AdoConnection do
begin
Provider := 'Provider=SQLOLEDB.1';
LoginPrompt := False;
KeepConnection := True;
ConnectionString := 'Provider=SQLOLEDB.1;Password=。。;Persist Security Info=True;User ID=。。;Initial Catalog=ftp;Data Source=。。';
end;
with AdoDataSet do
begin
Connection:=AdoConnection;
CacheSize:=1000;
locktype:=ltBatchOptimistic;

end;
ip:=SFileName;
Resume;
end;

Destructor TInsertDataThread.Destroy;
begin
ADOConnection.Free;
ADOConnection:=nil;
ADODataSet.Free;
ADODataSet:=nil;
CoUninitialize;
end;

procedure TInsertDataThread.Execute;
var
FileItem:string;
begin
DirName:=ip+'dir.txt';
txtName:=ip+'txt.txt';
if FIleExists(Dirname) then
begin
Assignfile(F,DirName);
Reset(F);
try
begin
while not Eof(f) do
begin
Readln(F,FileItem);
with ADODataSet do
begin
CommandText:='insert into allml(name,time,ip)'
+'values :)name,:time,:ip)';
Parameters.ParamByName('name').Value :=name;
Parameters.ParamByName('time').Value :=time;
Parameters.ParamByName('ip').Value :=ip;

end;
end;
end;
finally
closefile(f);
end;
end
else if FIleExists(txtname) then
begin
Assignfile(F,txtName);
Reset(F);
try
begin
while not Eof(f) do
begin
Readln(F,FileItem);
AnalyzeTxt(FileItem);
with ADODataSet do
begin
CommandText:='insert into allfile(name,time,size,ip)'
+'values :)name,:time,:size,:ip)';
Parameters.ParamByName('name').Value :=name;
Parameters.ParamByName('time').Value :=time;
Parameters.ParamByName('size').Value :=size;
Parameters.ParamByName('ip').Value :=ip;
//execute
end;
end;
end;

finally
closefile(f);
end;
end;

AdoDataSet.UpdateBatch(arall);
end;
 
帮忙啊。谢谢
 
AdoDataSet.UpdateBatch(arall)应该是对已打开的数据集进行修改后再进行成批更新吧?
没有看到你打开数据集。这是UpdateBatch在D6help中的Note:
To use batch updating, the CursorType property of the
dataset component must be either ctKeySet (the default)
or ctStatic and the LockType property must be ltBatchOptimistic.
 
请问
我只想insert
如何打开数据集呢
 
不能在关闭的数据集中执行
 
如何打开数据集呢?
 
adodataset.open//打开
.loadfromfile//打开
 
肯定出错,你搞错概念了。
要这样:

try
begin
-------
ADODataSet.close;
CommandText:='select * from allfile where 0=1';
ADODataSet.open;
----------------------
while not Eof(f) do
begin
Readln(F,FileItem);
AnalyzeTxt(FileItem);
with ADODataSet do
begin
--------------
insert;
FieldByName('name').asString:=name;
FieldByName......
............
post;
-----------------
// CommandText:='insert into allfile(name,time,size,ip)'
// x +'values :)name,:time,:size,:ip)';
// Parameters.ParamByName('name').Value :=name;
// Parameters.ParamByName('time').Value :=time;
// Parameters.ParamByName('size').Value :=size;
// Parameters.ParamByName('ip').Value :=ip;
//execute
end;
end;
end;

finally
closefile(f);
end;
end;

AdoDataSet.UpdateBatch(arall);

 
嘿嘿,你的问题,我刚刚在前两天遇到,问题出在ADOConnection上。

普通情况非线程使用的时候,你启动AdoDataSet.open,不管AdoConnection.connected为真为假,都能自动变成真。
而在线程中好象不行,必须手动设置AdoConnection.connected=True;

希望你满意。
 
同意楼上,
 
unit InsData;

interface

uses
Classes, ADODB,ActiveX,StrUtils,SysUtils;

type
TInsertDataThread = class(TThread)
private
{ Private declarations }
protected
ADODataSet: TADODataSet;
ADOCommand: TADOCommand;
ADOConnection: TADOConnection;
Ip,sName,Size,Time,DirName,TxtName:Ansistring;
F:TextFile;
procedure Execute; override;
procedure AnalyzeDir(line:AnsiString);
procedure AnalyzeTxt(line:AnsiString);
public
constructor create(Sfilename:Ansistring);
Destructor Destroy;override;
end;

implementation

constructor TInsertDatathread.create(SFileName:AnsiString);
begin
inherited Create(true);
freeonterminate:=true;
CoInitialize(Nil);
ADOConnection := TADOConnection.Create( nil );
ADODataSet:= TADODataSet.Create(nil);
ADOCommand:=TADOCommand.Create(nil);
with AdoConnection do
begin
Provider := 'Provider=SQLOLEDB.1';
LoginPrompt := False;
KeepConnection := True;
ConnectionString := 'Provider=SQLOLEDB.1;Password=;Persist Security Info=;User ID=;Initial Catalog=;Data Source=';
connected:=True;
end;

with AdoDataSet do
begin
Connection:=AdoConnection;
CacheSize:=1000;
locktype:=ltBatchOptimistic;

end;
{
with AdoCommand do
begin
Connection:=AdoConnection;
prepared:=true;
end;
}
ip:=SFileName;
Resume;
end;

Destructor TInsertDataThread.Destroy;
begin
ADOCommand.Free;
ADOCommand:=nil;
ADOConnection.Free;
ADOConnection:=nil;
ADODataSet.Free;
ADODataSet:=nil;
CoUninitialize;
end;

procedure TInsertDataThread.Execute;
var
FileItem:string;
begin
DirName:=ip+'dir.txt';
txtName:=ip+'txt.txt';
if FIleExists(Dirname) then
begin
Assignfile(F,DirName);
Reset(F);
ADODataSet.close;
ADODataSet.CommandText:='select * from allml where 0=1';
ADODataSet.open;//出错,‘标记没有储存’?

try
begin
while not Eof(f) do
begin
Readln(F,FileItem);
AnalyzeDir(FileItem);
with ADODataSet do
begin
insert;
FieldByName('ip').asString:=ip;
FieldByName('name').asString:=sname;
FieldByName('time').asString:=time;
post;
end;
{
begin
CommandText:='insert into allml(ip,name,time)'
+'values :)ip,:sname,:time)';
Parameters.ParamByName('ip').Value :=ip;
Parameters.ParamByName('sname').Value :=sname;
Parameters.ParamByName('time').Value :=time;
// Execute;
end;
}
end;
AdoDataSet.UpdateBatch(arall);
end;
finally
closefile(f);
end;
end;
if FIleExists(txtname) then
begin
Assignfile(F,txtName);
Reset(F);
ADODataSet.close;
ADODataSet.CommandText:='select * from allfile where 0=1';
ADODataSet.open;
try
begin
while not Eof(f) do
begin
Readln(F,FileItem);
AnalyzeTxt(FileItem);
with ADODataSet do
begin
insert;
FieldByName('name').asString:=sname;
FieldByName('size').asString:=size;
FieldByName('time').asString:=time;
FieldByName('ip').asString:=ip;
post;
end;
{
begin
CommandText:='insert into allfile(name,size,time,ip)'
+'values :)sname,:size,:time,:ip)';
Parameters.ParamByName('sname').Value :=sname;
Parameters.ParamByName('size').Value :=size;
Parameters.ParamByName('time').Value :=time;
Parameters.ParamByName('ip').Value :=ip;
// Execute;
end;
}
end;
end;

finally
closefile(f);
end;
end;

AdoDataSet.UpdateBatch(arall);
end;
 
顶部