其中datatype用紫色标出
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DBTables, ADODB, DB;
type
TForm1 = class(TForm)
Table1: TTable;
ADOTable1: TADOTable;
ADOCommand1: TADOCommand;
Database1: TDatabase;
ComboBox1: TComboBox;
ListBox1: TListBox;
Panel1: TPanel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
ADOConnection1: TADOConnection;
Panel2: TPanel;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
session.GetAliasNames(combobox1.Items);
bitbtn1.Enabled:=false;
bitbtn2.Enabled:=false;
bitbtn3.Enabled:=false;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
session.GetTableNames(combobox1.Items[combobox1.itemindex],'.db',false,false,listbox1.Items);
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
bitbtn1.Enabled:=true;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
I:integer;
tablelist:tstringlist;
str:string;
flddef:tfielddef;
begin
table1.DatabaseName:=combobox1.Items[combobox1.Itemindex];
//获取数据库名称
table1.TableName:=listbox1.Items[listbox1.itemindex];
//取得要连接的表名
adotable1.TableName:=table1.TableName;
memo1.Lines.Clear;
//清除memo1控件中的内容
tablelist:=tstringlist.Create;
//生成tstringlist对象
adoconnection1.GetTableNames(tablelist);
//生成SQL语句
if tablelist.IndexOf(adotable1.TableName)<0 then
memo1.Lines.Add('create table'+adotable1.TableName+'(')
else
memo1.Lines.Add('create table'+adotable1.TableName+'new(');
table1.FieldDefs.Update;
for i:=0 to table1.FieldDefs.Count-1 do
begin
flddef:=table1.FieldDefs[i];
str:=''+table1.FieldDefs[i].Name+''+datatype(flddef);
if i=table1.FieldDefs.Count-1 then
str:=str+');'
else
str:=str+',';
memo1.Lines.Add(str);
end;
//设置按钮的可用状态
bitbtn2.Enabled:=true;
bitbtn3.Enabled:=true;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
adocommand1.CommandText:=memo1.Text;
//获取SQL语句
adocommand1.Execute;
//执行SQL语句
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
var
I:integer;
begin
table1.Open;
//打开表
adotable1.Open;
try
//依次读取表中的数据
while not table1.Eof do
begin
adotable1.Insert;
for i:=0 to table1.fielddefs.count-1 do
begin
with table1.Fields[i] do
adotable1.FieldByName(table1.FieldDefs[i].Name).Value:=value;
adotable1.Post;
table1.Next;
end;
end;
finally
table1.Close;
//关闭表
adotable1.Close;
end;
end;
end.