Creating a Table

Example 1

Executing the example requires that the repository contains a database with the DB identifier.

Sub Main;

Var

MB: IMetabase;

CrInfo: IMetabaseObjectCreateInfo;

Table: ITable;

MObj: IMetabaseObject;

f: ITableField;

i: ITableIndex;

Begin

MB := MetabaseClass.Active;

CrInfo := MB.CreateCreateInfo;

CrInfo.ClassID := MetabaseObjectClass.KE_CLASS_TABLE;

CrInfo.Id := "NewTable";

CrInfo.Name: = "New table";

CrInfo.Parent := MB.Root;

MObj := MB.CreateObject(CrInfo).Edit;

Table := MObj As ITable;

Table.Database := Mb.ItemById("BD").Bind As IDatabase;

//Table field

f := Table.Fields.Add;

f.DataType := DbDataType.String;

f.Id := "Text_Field";

f. Name: = "Text field";

f.Size := 50;

//Index by created field

i := Table.Indexes.Add;

i.Fields.Add(f);

i.Primary := True;

Table.NativeName := "NewTable";

MObj.Save;

End Sub Main;

After executing the example, a new table is created in repository root. The table contains one text field and an index by this field. Table data is saved in the scheme, to which the DB database is set up to.

Example 2

Executing the example requires that the repository contains a database with the DB identifier. The database is set up for connection to database server, in which the table with the Table_1 physical name is contained.

Sub Main;

Var

Mb: IMetabase;

CrInfo: IMetabaseObjectCreateInfo;

MObj: IMetabaseObject;

Table: ITable;

Begin

Mb := MetabaseClass.Active;

CrInfo := MB.CreateCreateInfo;

CrInfo.ClassID := MetabaseObjectClass.KE_CLASS_EXTERNTABLE;

CrInfo.Id := "NewAttachTable";

CrInfo.Name := "NewAttachTable";

CrInfo.Parent := Mb.Root;

MObj := MB.CreateObject(CrInfo).Edit;

Table := MObj As ITable;

Table.Database := Mb.ItemById("BD").Bind As IDatabase;

Table.AttachTable("Table_1", True);

MObj.Save;

End Sub Main;

After executing the example, an external table is created in the repository root. The table refers to the table with the Table_1 physical name, stored on database server.

See also:

Examples