unit U_AdoTable;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB;
type
TF_AdoTable = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
SaveButton: TButton;
ComboBox1: TComboBox;
ShowButton: TButton;
ADOTable1: TADOTable;
Label1: TLabel;
ADOConnection1: TADOConnection;
procedure SaveButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ShowButtonClick(Sender: TObject);
private
public
{ Public declarations }
end;
var
F_AdoTable: TF_AdoTable;
DataFileName,BaseFileName: string;
implementation
{$R *.dfm}
procedure TF_AdoTable.FormCreate(Sender: TObject);
var
SL: TStrings;
index: Integer;
begin
Combobox1.Items.Clear;
SL := TStringList.Create;
try
//读取数据库中所包含的数据表的表名
ADOConnection1.GetTableNames(SL, False);
for index := 0 to (SL.Count - 1) do begin
Combobox1.Items.add(SL[index]);
end;
finally
SL.Free;
end;
Combobox1.ItemIndex :=0;
end;
procedure TF_AdoTable.SaveButtonClick(Sender: TObject);
begin
if ADOTable1.Active then
begin
//将记录集中的数据以xml的格式存储到指定文件中
DataFileName := ExtractFilePath(Paramstr(0))+BaseFileName;
ADOTable1.SaveToFile(DataFileName, pfXML);
end
else
MessageDlg('数据表尚未打开,无法存储!', mtInformation,
[mbOk], 0);
end;
procedure TF_AdoTable.ShowButtonClick(Sender: TObject);
begin
self.Caption :=Combobox1.Items[Combobox1.itemindex]+'表';
ADOTable1.Close;
ADOTable1.TableName :=Combobox1.Items[Combobox1.itemindex];
ADOTable1.Open;
//设置保存数据表的文件的名称
BaseFileName := Combobox1.Items[Combobox1.itemindex]+'表'+'.XML';
end;
end.
应用TADOConnection和TADOQuery组件
最新推荐文章于 2024-10-12 16:49:49 发布
本博客介绍了一个使用 Delphi 实现的 ADO Table 组件应用案例,该组件可以实现从数据库读取表数据并展示在界面上,同时支持选择不同的数据表并将其数据以 XML 格式导出到文件。
914

被折叠的 条评论
为什么被折叠?



