You can add an extra level of data integrity when creating a new table by placing the code to create a table inside a transaction. If the transaction is aborted, the table entry in the database dictionary file is removed, and the table data and index files are deleted from disk.
When a table is created inside a transaction, and until the transaction is committed or aborted, the newly created table must be open with OPEN_MODE.EXCLUSIVE_OPEN mode, otherwise the table open operation will fail. After the transaction is committed the table can be open in non exclusive mode.
The code fragment below creates a new table under transaction control. Again no error checking code is included in the example:
// create a new table object
CTTable ATable = new CTTable(ADatabase);
// begin a transaction
ATable.Begin();
try
{
// add a field
ATable.AddField("Field1", FIELD_TYPE.INTEGER, 4);
// add another field
ATable.AddField("Field1", FIELD_TYPE.CHARS, 30);
// create the table
ATable.Create("MyTable", CREATE_MODE.NORMAL_CREATE);
// commit the transaction
ATable.Commit();
}
catch (CTException err)
{
// abort the transaction
ATable.Abort();
Console.Write("Create table failed with error {0}\n", err.GetErrorCode());
}
It is important to note that if you open a table that was created inside the current transaction, i.e., the transaction has not yet been committed or aborted, you must open the table in exclusive mode by specifying the OPEN_MODE.EXCLUSIVE_OPEN mode to CTTable.Open().