procedure TForm1.Button1Click(Sender: TObject);
begin
Customers.InsertRecord([Edit1.Text, Edit2.Text, Null, Edit3.Text]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Customers:= TTable.Create(Form1);
with Customers do
begin
DatabaseName := 'DBDEMOS';
TableType := ttParadox;
TableName := 'MyCustInfo';
ReadOnly:= False;
// 不要覆盖现有的表.
if (not Customers.Exists) then CreateMyTable
else
begin
if (Customers.Exists AND
(MessageDlg('MyCustInfo table already exists. Do you want to rebuild it?', mtConfirmation, [mbYes, mbNo], 0) = mrYes)) then
begin
Customers.Close;
Customers.DeleteTable;
CreateMyTable;
end;
end;
end;
DS2.DataSet:= Customers;
DBGrid2.DataSource.DataSet:= Customers;
Customers.Active:= True;
end;
procedure CreateMyTable();
var
i: Integer;
begin
//Table 组件不能处于激活状态,然后定义表中的字段和类型。
with Customers do
begin
with FieldDefs do
begin
Clear;
with AddFieldDef do
begin
Name := 'Field1';
DataType := ftInteger;
Required := True;
end;
with AddFieldDef do
begin
Name := 'Field2';
DataType := ftString;
Size := 30;
end;
with AddFieldDef do
begin
Name := 'Field3';
DataType := ftString;
Size := 30;
end;
with AddFieldDef do
begin
Name := 'Field4';
DataType := ftString;
Size := 30;
end;
end;
// 清除原有索引.
// 接着重新定义索引.
with IndexDefs do
begin
Clear;
// 第一索引没有Name
// 这是一个主键
with AddIndexDef do
begin
Name := '';
Fields := 'Field1';
Options := [ixPrimary];
end;
with AddIndexDef do
begin
Name := 'Fld2Indx';
Fields := 'Field2';
Options := [ixCaseInsensitive];
end;
end;
// 调用CreateTable方法来创建表.
CreateTable;
Customers.Active:= True;
for i := 1 to 20 do
Customers.AppendRecord([i*111, i*222, i*333, i*444]);
end;
end;