具体错误是Project xxx.exe raised exception class EAccessViolation with message' Access violation at address .................Write of address 0000030. Process stoped Use step or .....很常见的错误提示,另外我连接的是SQL server 2000
看看tclientdataset的fetchondemand是否为true
应该可以把,我还没试过,估计可能需要调用CoInitialize()
我写了如下代码进行测试,没有发生任何问题。 unit Unit2;interfaceuses Classes, DBClient;type TrdABC = class(TThread) private { Private declarations } fCDS:TClientDataset; protected procedure Execute; override; end;implementation uses ActiveX, DB; { 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 TrdABC.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ TrdABC } procedure TrdABC.Execute; begin FreeOnTerminate:=true; CoInitialize(nil); try fCDS:=TClientDataset.Create(nil); try with FCDS.FieldDefs.AddFieldDef do begin DataType:=ftInteger; Name:='ID'; end; fCDS.CreateDataSet; { Place thread code here } finally fCDS.Free; end; finally CoUnInitialize; end; end;end.
to Miracle()看了你的例子, 发现对我没什么用, 因为你只是在线程中建立了本地的数据集 而我是要从无端数据源取数据集,不用CoInitialize也可以,ClientDataSet并没用到Ole接口 , 我想不用初始化的.我的程序中也可以线程中操作本地数据集, 但不能在线程中发送CommandText到服务器.我要求的是 procedure TrdABC.Execute; begin FreeOnTerminate:=true; fCDS:=TClientDataset.Create(nil); try with FCDS do begin Close(); CommandText := 'SELECT * FROM .....'; Open(); //线程中执行这里会出错 end; finally fCDS.Free; end;
我在主线程中调用Execute是可以正常执行的.有些控件是不支持线程安全的, 我只是想知道有没有可能在线程中使用TClientDataSet.
unit Unit2;interfaceuses
Classes, DBClient;type
TrdABC = class(TThread)
private
{ Private declarations }
fCDS:TClientDataset;
protected
procedure Execute; override;
end;implementation
uses
ActiveX, DB;
{ 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 TrdABC.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TrdABC }
procedure TrdABC.Execute;
begin
FreeOnTerminate:=true;
CoInitialize(nil);
try
fCDS:=TClientDataset.Create(nil);
try
with FCDS.FieldDefs.AddFieldDef do
begin
DataType:=ftInteger;
Name:='ID';
end;
fCDS.CreateDataSet;
{ Place thread code here }
finally
fCDS.Free;
end;
finally
CoUnInitialize;
end;
end;end.
而我是要从无端数据源取数据集,不用CoInitialize也可以,ClientDataSet并没用到Ole接口 , 我想不用初始化的.我的程序中也可以线程中操作本地数据集, 但不能在线程中发送CommandText到服务器.我要求的是
procedure TrdABC.Execute;
begin
FreeOnTerminate:=true;
fCDS:=TClientDataset.Create(nil);
try
with FCDS do
begin
Close();
CommandText := 'SELECT * FROM .....';
Open(); //线程中执行这里会出错
end;
finally
fCDS.Free;
end;
FreeOnTerminate:=true;
fCDS:=TClientDataset.Create(nil);
try
with FCDS do
begin
Close();
CommandText := 'SELECT * FROM .....';
Open(); //线程中执行这里会出错
end;
finally
fCDS.Free;
end;
看来是没办法了我会给分的
TO :CROB
>>'''ClientDataSet并没用到Ole接口
不会吧.你自己看源码,ClientDataSet是离不开COM 接口的.关于类似的问题, 其实早有贴子已经讨论得比较成熟了.
可以参见
http://www.csdn.net/expert/topic/177/177973.shtm
超过一个线程,
CoInitializeEx 不是最好要用..而是必须要用.