如何动态建立TTable ?(50分)

  • 主题发起人 主题发起人 wtiebo
  • 开始时间 开始时间
W

wtiebo

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在程序运行时动态建立TTable,如何才能不出错?
 
动态建立 TTable 与动态建立其他控件并无实质差别, 不知道你的"不能出错"是什么
意思?
 
举个例子给你:

var t:TTable;
begin
t:=TTable.Create(self);
t.DatabaseName:='test';
t.TableName:='test.DB';
t.Open;
:
:
t.Free;
end;
 
你该看看以前的大富豪问题
现抄录两段该你(忘了是抄自那个问题 )
《中国计算机报》上发现一篇文章《在Delphi代码中定义数据库》,帮了我的大忙。部分代码如下:
procedure tform1.create_tbl;
var tbl: ttable;
begin
tbl:=ttable.create(self); // 如没有声明中的"tform1.",Delphi不认这个self with tbl do
begin datebasename:=;dbdemos';
tablename:='my_tbl';
with fielddefs do
begin clear;
add('No',ftInteger,0,false);
add('Name',ftString,0,false);
... end;
with indexdefs
do
begin clear; add('primary','no',[ixPrimary,ixUnique]); ...
end;
CreateTable;
end;
end;

另一段为:
How to Create a Table at Runtime
I was a VB programmer, until my recent shift to Delphi 2.0. How can I create a database in code?

It depends on the type of database you want to build. However, I can show you how to do it with a Paradox table. Conceivably, it stands to reason that since the TTable is database-independent and if you've got the right settings in the BDE, you should be able to create a table with the TTable component in any database. This is not necessarily true. SQL tables are normally created using the SQL call CREATE TABLE. And each server has its own conventions for creating tables and defining fields. So it's important to note this if you're working with a SQL database. The problem is that SQL databases support different data types that aren't necessarily available in the standard BDE set. For instance, MS SQL server's NUMERIC data format is not necessarily a FLOAT as it's defined in the BDE. So your best bet would probably be to create SQL tables using SQL calls.

What you have to do is declare a TTable variable, create an instance, then with the TTable's FieldDefs property, add field definitions. Finally, you'll make a call to CreateTable, and your table will be created. Here's some example code:

{ "Add" is the operative function here.
Add(const Name: string; DataType: TFieldType; Size: Word; Required: Boolean);
}
procedure CreateATable(DBName, //Alias or path
TblName : String); //Table Name to Create
var
tbl : TTable;
begin
tbl := TTable.Create(Application);
with tbl do begin
Active := False;
DatabaseName := DBName;
TableName := TblName;
TableType := ttParadox;
with FieldDefs do begin
Clear;
Add('LastName', ftString, 30, False);
Add('FirstName', ftString, 30, False);
Add('Address1', ftString, 40, False);
Add('Address2', ftString, 40, False);
Add('City', ftString, 30, False);
Add('ST', ftString, 2, False);
Add('Zip', ftString, 10, False);
end;

{Add a Primary Key to the table}
with IndexDefs do begin
Clear;
Add('Field1Index', 'LastName;FirstName', [ixPrimary, ixUnique]);
end;

CreateTable; {Make the table}
end;
end;
The procedure above makes a simple contact table, first by defining the fields to be included in the table, then creating a primary key. As you can see, it's a pretty straightforward procedure. One thing you can do is to change the TableType property setting to a variable that's passed as a parameter to the procedure so you can create DBase or even ASCII tables. Here's snippet of how you'd accomplish that:

procedure CreateATable(DBName, //Alias or path
TblName : String); //Table Name to Create
TblType : TTableType); //ttDefault, ttParadox, ttDBase, ttASCII
var
tbl : TTable;
begin
tbl := TTable.Create(Application);
with tbl do begin
Active := False;
DatabaseName := DBName;
TableName := TblName;
TableType := TblType;
with FieldDefs do begin
Clear;
Add('LastName', ftString, 30, False);
Add('FirstName', ftString, 30, False);
Add('Address1', ftString, 40, False);
Add('Address2', ftString, 40, False);
Add('City', ftString, 30, False);
Add('ST', ftString, 2, False);
Add('Zip', ftString, 10, False);
end;

{Add a Primary Key to the table}
with IndexDefs do begin
Clear;
Add('Field1Index', 'LastName;FirstName', [ixPrimary, ixUnique]);
end;

CreateTable; {Make the table}
end;
end;
Pretty simple, right? One thing you should note is that the TableType property is only used for desktop databases. It doesn't apply to SQL tables.

Oh well, that's it in a nutshell. Have fun!

Copyright © 1995, 1996, 1997 Brendan V. Delumpa All Rights Reserved





 
多人接受答案了。
 
后退
顶部