主题:急...用ADO连接,如何在dbGrid中保存查询出的结果??
很愁76
[专家分:10] 发布于 2006-12-12 14:12:00
请大家帮忙呀
回复列表 (共5个回复)
沙发
wealthy [专家分:1840] 发布于 2006-12-12 14:22:00
保存到txt中
Function EduceText(var Sg:TStringGrid):Boolean;
var
dlgExport: TSaveDialog;
FileName,ss,CellText,sFormat: string;
TxtFile:TextFile;
iRow,iCol:Integer;
begin
Result:=False;
try
//获得文件名及路径
dlgExport:=TSaveDialog.Create(nil);
dlgExport.Filter := '文本文件(*.txt)|*.txt';
if not dlgExport.Execute then
Exit;
FileName := dlgExport.FileName;
if not ((ExtractFileExt(FileName) = '') or (LowerCase(ExtractFileExt(FileName)) <> '.txt')) then
Exit;
FileName := FileName + '.txt';
//导出
try
AssignFile(TxtFile, FileName);
Rewrite(TxtFile);
if IOResult <> 0 then
raise Exception.Create('创建文件' + FileName + '失败');
ss := '当前共有数据' + IntTostr(Sg.RowCount) + '行';
Writeln(TxtFile, ss);
//保存文本数据
for iRow := 0 to Sg.RowCount - 1 do
begin
CellText := '';
for iCol := 0 to Sg.ColCount - 1 do
begin
sFormat := '%:-' + IntTostr(Sg.ColWidths[iCol] div 10) + 's';
CellText := CellText + ' ' + Format(sFormat, [Sg.Cells[iCol, iRow]]);
end;
ss := IntTostr(iRow) + ' ' + CellText;
Writeln(TxtFile, ss)
end;
Result:=True;
finally
CloseFile(TxtFile);
end;
finally
dlgExport.Free;
end;
end;
板凳
wealthy [专家分:1840] 发布于 2006-12-12 14:24:00
如果你要保存到数据表中,则要另个建立一个数据表(或临时数据表,但临时数据表占内存)
Query1.Active :=false;
Query1.SQL.Clear ;
Query1.SQL.Add('select rklsh,spbh from spwzzkls where rklsh in (select rklsh from spwzzkls group by rklsh having count(rklsh)>1) order by rklsh');
Query1.open;
while not Query1.Eof do
begin
Query2.Active :=false;
Query2.SQL.Clear ;
Query2.SQL.Add('update spwzzkls set rklsh=:rklsh where rklsh=:lsh and spbh=:spbh') ;
Query2.ParamByName('rklsh').AsString := '01'+inttostr(index);
Query2.ParamByName ('lsh').AsString :=Query1.fieldbyname ('rklsh').AsString ;
Query2.ParamByName ('spbh').AsString :=Query1.fieldbyname('spbh').AsString ;
Query2.Prepare ;
Query2.ExecSQL ;
Query1.Next ;
end;
3 楼
很愁76 [专家分:10] 发布于 2006-12-12 15:07:00
谢谢你。
我只是想把查询到的结果 在窗体中用DBGrid显示出来。
如何为DBGrid的Item赋值,我不会写,情况 嘿嘿,老弟见笑了。
with not eof do ////将查询到的记录写入DBGrid中
begin
??????????????
DBGrid1.Refresh;
end;
请再指点一下,多谢了!!!
4 楼
wealthy [专家分:1840] 发布于 2006-12-12 16:39:00
ADOQuery1.SQL.Add('select * from TreeTest where FParentID=''1''');//1是查询内容
5 楼
很愁76 [专家分:10] 发布于 2006-12-13 10:17:00
我学到怎么写了,
DBGrid.columns[0].Fieldname:='Id';
每次都是你热心回贴,太感谢你了...
[em2]
我来回复