Product Documentation

c-treeDB API for VB.Net

Previous Topic

Next Topic

Define

Define() establishes specific data definitions. This involves defining columns/fields and creating the tables/files with optional indexes.

Below is the code for Define():

'

' Define()

'

' Open the table, if it exists. Otherwise create and open the table

'

Sub Define()

Console.WriteLine("DEFINE")

Create_CustomerMaster_Table()

End Sub

'

' Create_CustomerMaster_Table()

'

' Open table CustomerMaster, if it exists. Otherwise create it

' along with its indexes and open it

'

Sub Create_CustomerMaster_Table()

Dim do_create As Boolean = False

Dim field1 As CTField

Dim index1 As CTIndex

' define table CustomerMaster

Console.WriteLine(ControlChars.Tab + "table CustomerMaster")

Try

MyTable.Open("custmast", OPEN_MODE.NORMAL_OPEN)

Catch

' table does not exist

do_create = True

End Try

If (do_create) Then

' define table fields

Console.WriteLine(ControlChars.Tab + "Add fields...")

Try

field1 = MyTable.AddField("cm_custnumb", FIELD_TYPE.FSTRING, 4)

MyTable.AddField("cm_custzipc", FIELD_TYPE.FSTRING, 9)

MyTable.AddField("cm_custstat", FIELD_TYPE.FSTRING, 2)

MyTable.AddField("cm_custratg", FIELD_TYPE.FSTRING, 1)

MyTable.AddField("cm_custname", FIELD_TYPE.VSTRING, 47)

MyTable.AddField("cm_custaddr", FIELD_TYPE.VSTRING, 47)

MyTable.AddField("cm_custcity", FIELD_TYPE.VSTRING, 47)

' define index

index1 = MyTable.AddIndex("cm_custnumb_idx", KEY_TYPE.FIXED_INDEX, False, False)

index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG)

' create table

Console.WriteLine(ControlChars.Tab + "Create table...")

MyTable.Create("custmast", CREATE_MODE.NORMAL_CREATE)

' open table

Console.WriteLine(ControlChars.Tab + "Open table...")

MyTable.Open("custmast", OPEN_MODE.NORMAL_OPEN)

Catch E As CTException

Handle_Exception(E)

End Try

Else

Check_Table_Mode(MyTable)

' confirm the index exists, if not then add the index

'

' Me scenario arises out of the fact that Me table was created in tutorial 1

' without indexes. The index is now created by the call to ctdbAlterTable

do_create = False

Try

MyTable.GetIndex("cm_custnumb_idx")

Catch

do_create = True

End Try

If (do_create) Then

Try

field1 = MyTable.GetField("cm_custnumb")

index1 = MyTable.AddIndex("cm_custnumb_idx", KEY_TYPE.FIXED_INDEX, False, False)

index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG)

MyTable.Alter(ALTER_TABLE.NORMAL)

Catch E As CTException

Handle_Exception(E)

End Try

End If

End If

End Sub

'

' Check_Table_Mode()

'

' Check if existing table has transaction processing flag enabled.

' If a table is under transaction processing control, modify the

' table mode to disable transaction processing

'

Sub Check_Table_Mode(ByVal table As CTTable)

Try

' get table create mode

Dim mode As CREATE_MODE = table.GetCreateMode()

' check if table is under transaction processing control

If ((mode And CREATE_MODE.TRNLOG_CREATE) <> 0) Then

' change file mode to disable transaction processing

mode = mode Xor CREATE_MODE.TRNLOG_CREATE

table.UpdateCreateMode(mode)

End If

Catch E As CTException

Handle_Exception(E)

End Try

End Sub

TOCIndex