M
MingWord
Unregistered / Unconfirmed
GUEST, unregistred user!
D7+ADO+SQL
我用线程进行数据库查询,但是所得的数据一闪而过,可能是线程释放了的关系,TADOConnection和TADOQuery是动态创建和连接的.查询之后将Query连上DataSource,但是ShowMessage作为停留,发现数据已经查询出来了,但是当关闭了ShowMessage,数据跟着消失.怎么解决好?
unit UThread;
interface
uses
Classes,ADODB,DB,ActiveX,Dialogs;
type
SelectData = class(TThread)
private
{ Private declarations }
Quer:TADOQuery;
Conn:TADOConnection;
Sour: TDataSource;
Conn1,Conn2:String;
SQL:String;
procedure ConnSource;
protected
procedure Execute;
override;
public
constructor Cteate(Suspendec:Boolean;NeedSQL:String;DASource:TDataSource);
destructor Destroy;override;
end;
implementation
procedure SelectData.ConnSource;
begin
Sour.DataSet:=Quer;
//ShowMessage('成功');//弹出这个窗口时,数据已经显示,但之后就消失.
end;
constructor selectdata.Cteate(Suspendec:Boolean;NeedSQL:String;DASource:TDataSource);
begin
CoInitialize(nil);
Inherited create(Suspended);
FreeOnTerminate:=True;
try
Conn:=TADOConnection.Create(nil);
// Quer:=Query;
Quer:=TADOQuery.Create(nil);
Sour:=DASource;
Quer.Connection:=Conn;
Conn1:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Wonder;Data Source=.;Use Procedure for Prepare=1;';
Conn2:='Auto Translate=True;Packet Size=4096;Workstation ID=NEWPOWER-DGI211;Use Encryption for Data=False;Tag with column collation when possible=False';
With Conndo
begin
Loginprompt:=False;
KeepConnection:=True;
ConnectionString:=Conn1+conn2;
end;
SQL:=NeedSQL;
except
ShowMessage('数据连接出错');
end;
end;
destructor SelectData.Destroy;
begin
Conn.Free;
Quer.Free;
Sour.Free;
CoUnInitialize;
inherited Destroy;
end;
procedure SelectData.Execute;
var
i:integer;
begin
try
Conn.Open;
Quer.Close;
Quer.SQL.Clear;
Quer.SQL.Text:=SQL;
Quer.Open;
Synchronize(ConnSource);
except
ShowMessage('数据库连接错误');
end;
{ Place thread code here }
end;
end.
调用.
function ShowFive(AHandle:THandle;Form:TForm;TypeID:String;OutNum:Integer):Longint;
var
SQL1:String;
begin
frmFive:=TfrmFive.Create(Form);
frmFive.FHandle:=AHandle;
NowTypeID:=TypeID;
NowOutNum:=OutNum;
result:=Longint(frmFive);
frmFive.Show;
SQL1:='Select * From vshowfivec where typeid='+NowTypeID;
SelectData.Cteate(false,SQL1,frmFive.DAShowFive);
end;
我用线程进行数据库查询,但是所得的数据一闪而过,可能是线程释放了的关系,TADOConnection和TADOQuery是动态创建和连接的.查询之后将Query连上DataSource,但是ShowMessage作为停留,发现数据已经查询出来了,但是当关闭了ShowMessage,数据跟着消失.怎么解决好?
unit UThread;
interface
uses
Classes,ADODB,DB,ActiveX,Dialogs;
type
SelectData = class(TThread)
private
{ Private declarations }
Quer:TADOQuery;
Conn:TADOConnection;
Sour: TDataSource;
Conn1,Conn2:String;
SQL:String;
procedure ConnSource;
protected
procedure Execute;
override;
public
constructor Cteate(Suspendec:Boolean;NeedSQL:String;DASource:TDataSource);
destructor Destroy;override;
end;
implementation
procedure SelectData.ConnSource;
begin
Sour.DataSet:=Quer;
//ShowMessage('成功');//弹出这个窗口时,数据已经显示,但之后就消失.
end;
constructor selectdata.Cteate(Suspendec:Boolean;NeedSQL:String;DASource:TDataSource);
begin
CoInitialize(nil);
Inherited create(Suspended);
FreeOnTerminate:=True;
try
Conn:=TADOConnection.Create(nil);
// Quer:=Query;
Quer:=TADOQuery.Create(nil);
Sour:=DASource;
Quer.Connection:=Conn;
Conn1:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Wonder;Data Source=.;Use Procedure for Prepare=1;';
Conn2:='Auto Translate=True;Packet Size=4096;Workstation ID=NEWPOWER-DGI211;Use Encryption for Data=False;Tag with column collation when possible=False';
With Conndo
begin
Loginprompt:=False;
KeepConnection:=True;
ConnectionString:=Conn1+conn2;
end;
SQL:=NeedSQL;
except
ShowMessage('数据连接出错');
end;
end;
destructor SelectData.Destroy;
begin
Conn.Free;
Quer.Free;
Sour.Free;
CoUnInitialize;
inherited Destroy;
end;
procedure SelectData.Execute;
var
i:integer;
begin
try
Conn.Open;
Quer.Close;
Quer.SQL.Clear;
Quer.SQL.Text:=SQL;
Quer.Open;
Synchronize(ConnSource);
except
ShowMessage('数据库连接错误');
end;
{ Place thread code here }
end;
end.
调用.
function ShowFive(AHandle:THandle;Form:TForm;TypeID:String;OutNum:Integer):Longint;
var
SQL1:String;
begin
frmFive:=TfrmFive.Create(Form);
frmFive.FHandle:=AHandle;
NowTypeID:=TypeID;
NowOutNum:=OutNum;
result:=Longint(frmFive);
frmFive.Show;
SQL1:='Select * From vshowfivec where typeid='+NowTypeID;
SelectData.Cteate(false,SQL1,frmFive.DAShowFive);
end;