Product Documentation

c-treeDB API for C#

Previous Topic

Next Topic

Manage

Manage() provides data management functionality for your application and/or process.

Below is the code for Manage():

//

// Manage()

//

// Populates table and perform a simple query

//

static void Manage()

{

Console.WriteLine("MANAGE");

// populate the tables with data

Add_CustomerMaster_Records();

Add_ItemMaster_Records();

Add_Transactions();

// display the orders and their items

Display_CustomerOrders();

Display_OrderItems();

}

//

// Add_CustomerMaster_Records()

//

// This function adds records to table CustomerMaster from an

// array of strings

//

public struct CUSTOMER_DATA

{

// struct members

public string number, zipcode, state, rating, name, address, city;

// struct constructor

public CUSTOMER_DATA(string number, string zipcode, string state, string rating, string name, string address, string city)

{

this.number = number;

this.zipcode = zipcode;

this.state = state;

this.rating = rating;

this.name = name;

this.address = address;

this.city = city;

}

};

static void Add_CustomerMaster_Records()

{

CUSTOMER_DATA[] data = new CUSTOMER_DATA[4];

data[0] = new CUSTOMER_DATA("1000", "92867", "CA", "1", "Bryan Williams", "2999 Regency", "Orange");

data[1] = new CUSTOMER_DATA("1001", "61434", "CT", "1", "Michael Jordan", "13 Main", "Harford");

data[2] = new CUSTOMER_DATA("1002", "73677", "GA", "1", "Joshua Brown", "4356 Cambridge", "Atlanta");

data[3] = new CUSTOMER_DATA("1003", "10034", "MO", "1", "Keyon Dooling", "19771 Park Avenue", "Columbia");

int nRecords = data.Length;

Delete_Records(recordCustMast);

Console.WriteLine("\tAdd records in table CustomerMaster...");

try

{

// start a transaction

MySession.Begin();

for (int i = 0; i < nRecords; i++)

{

recordCustMast.Clear();

// populate record buffer with data

recordCustMast.SetFieldAsString(0, data[i].number);

recordCustMast.SetFieldAsString(1, data[i].zipcode);

recordCustMast.SetFieldAsString(2, data[i].state);

recordCustMast.SetFieldAsString(3, data[i].rating);

recordCustMast.SetFieldAsString(4, data[i].name);

recordCustMast.SetFieldAsString(5, data[i].address);

recordCustMast.SetFieldAsString(6, data[i].city);

// add record

recordCustMast.Write();

}

// commit transaction

MySession.Commit();

}

catch (CTException E)

{

Handle_Exception(E);

}

}

//

// Add_ItemMaster_Records()

//

// This function adds records to table ItemMaster from an

// array of strings

//

public struct ITEM_DATA

{

// struct members

public int weight;

public CTMoney price;

public string itemnum, description;

// struct constructor

public ITEM_DATA(int weight, CTMoney price, string itemnum, string description)

{

this.weight = weight;

this.price = price;

this.itemnum = itemnum;

this.description = description;

}

};

static void Add_ItemMaster_Records()

{

ITEM_DATA[] data = new ITEM_DATA[4];

data[0] = new ITEM_DATA(10, 1995, "1", "Hammer");

data[1] = new ITEM_DATA(3, 999, "2", "Wrench");

data[2] = new ITEM_DATA(4, 1659, "3", "Saw");

data[3] = new ITEM_DATA(1, 398, "4", "Pliers");

int nRecords = data.Length;

Delete_Records(recordItemMast);

Console.WriteLine("\tAdd records in table ItemMaster...");

try

{

// start a transaction

MySession.Begin();

for (int i = 0; i < nRecords; i++)

{

recordItemMast.Clear();

// populate record buffer with data

recordItemMast.SetFieldAsSigned(0, data[i].weight);

recordItemMast.SetFieldAsMoney(1, data[i].price);

recordItemMast.SetFieldAsString(2, data[i].itemnum);

recordItemMast.SetFieldAsString(3, data[i].description);

// add record

recordItemMast.Write();

}

// commit transaction

MySession.Commit();

}

catch (CTException E)

{

Handle_Exception(E);

}

}

//

// Delete_Records()

//

// This function deletes all the records in the table

//

static void Delete_Records(CTRecord record)

{

bool found;

Console.WriteLine("\tDelete records...");

try

{

// write lock required for transaction updates

record.Lock(LOCK_MODE.WRITE_LOCK);

// start a transaction

record.Begin();

// read first record

found = record.First();

while (found) // while records are found

{

// delete record

record.Delete();

// read next record

found = record.Next();

}

// commit transaction

record.Commit();

// free locks

record.Unlock();

}

catch (CTException E)

{

Handle_Exception(E);

}

}

//

// Add_Transactions()

//

// Add an Order and associated Items "as a transaction" to their

// respective tables. A transaction is committed or aborted if the

// customer number on the order is confirmed valid. Likewise each

// item in the order is verified to be a valid item. SavePoints are

// established as an order is processed, allowing a transaction to

// rollback to the previously verified item

//

public struct ORDER_DATA

{

// struct members

public string orderdate, promdate, ordernum, custnum;

// struct constructor

public ORDER_DATA(string orderdate, string promdate, string ordernum, string custnum)

{

this.orderdate = orderdate;

this.promdate = promdate;

this.ordernum = ordernum;

this.custnum = custnum;

}

};

public struct ORDERITEM_DATA

{

// struct members

public string ordernum;

public int seqnumber, quantity;

public string itemnum;

// struct constructor

public ORDERITEM_DATA(string ordernum, int seqnumber, int quantity, string itemnum)

{

this.ordernum = ordernum;

this.seqnumber = seqnumber;

this.quantity = quantity;

this.itemnum = itemnum;

}

};

static void Add_Transactions()

{

ORDER_DATA[] orders = new ORDER_DATA[3];

orders[0] = new ORDER_DATA("09/01/2002", "09/05/2002", "1", "1001");

orders[1] = new ORDER_DATA("09/02/2002", "09/06/2002", "2", "9999"); // bad customer number

orders[2] = new ORDER_DATA("09/22/2002", "09/26/2002", "3", "1003");

int nOrders = orders.Length;

ORDERITEM_DATA[] items = new ORDERITEM_DATA[6];

items[0] = new ORDERITEM_DATA("1", 1, 2, "1");

items[1] = new ORDERITEM_DATA("1", 2, 1, "2");

items[2] = new ORDERITEM_DATA("2", 1, 1, "3");

items[3] = new ORDERITEM_DATA("2", 2, 3, "4");

items[4] = new ORDERITEM_DATA("3", 1, 2, "3");

items[5] = new ORDERITEM_DATA("3", 2, 2, "99"); // bad item number

int nItems = items.Length;

CTDate orderdate = new CTDate();

CTDate promdate = new CTDate();

int savepoint;

int j = 0;

Delete_Records(recordCustOrdr);

Delete_Records(recordOrdrItem);

Console.WriteLine("\tAdd transaction records...");

// process orders

for (int i = 0; i < nOrders; i++)

{

// start a transaction

MySession.Begin();

try

{

recordCustOrdr.Clear();

// populate record buffer with order data

orderdate.StringToDate(orders[i].orderdate, DATE_TYPE.MDCY_DATE);

promdate.StringToDate(orders[i].promdate, DATE_TYPE.MDCY_DATE);

recordCustOrdr.SetFieldAsDate(0, orderdate);

recordCustOrdr.SetFieldAsDate(1, promdate);

recordCustOrdr.SetFieldAsString(2, orders[i].ordernum);

recordCustOrdr.SetFieldAsString(3, orders[i].custnum);

// add order record

recordCustOrdr.Write();

}

catch (CTException E)

{

Handle_Exception(E);

}

// set transaction savepoint

savepoint = recordCustOrdr.SetSavePoint();

// process order items

while (items[j].ordernum == orders[i].ordernum)

{

try

{

recordOrdrItem.Clear();

// populate record buffer with order item data

recordOrdrItem.SetFieldAsSigned(0, items[j].seqnumber);

recordOrdrItem.SetFieldAsSigned(1, items[j].quantity);

recordOrdrItem.SetFieldAsString(2, items[j].ordernum);

recordOrdrItem.SetFieldAsString(3, items[j].itemnum);

// add order item record

recordOrdrItem.Write();

// check that item exists in ItemMaster table

recordItemMast.Clear();

recordItemMast.SetFieldAsString(2, items[j].itemnum);

if (!recordItemMast.Find(FIND_MODE.EQ))

// if not found, restore back to previous savepoint

recordItemMast.RestoreSavePoint(savepoint);

else

// set transaction savepoint

savepoint = recordItemMast.SetSavePoint();

}

catch (CTException E)

{

Handle_Exception(E);

}

// bump to next item

j++;

// exit the while loop on last item

if (j >= nItems)

break;

}

// check that customer exists in CustomerMaster table

recordCustMast.Clear();

recordCustMast.SetFieldAsString(0, orders[i].custnum);

// commit or abort the transaction

if (!recordCustMast.Find(FIND_MODE.EQ))

MySession.Abort();

else

MySession.Commit();

}

}

//

// Display_CustomerOrders()

//

// This function displays the contents of a table. ctdbFirstRecord() and

// ctdbNextRecord() fetch the record. Then each field is parsed and displayed

//

static void Display_CustomerOrders()

{

string custnumb;

string ordrnumb;

Console.WriteLine("\tCustomerOrder table...");

try

{

// read first record

if (recordCustOrdr.First())

{

do

{

ordrnumb = recordCustOrdr.GetFieldAsString(2);

custnumb = recordCustOrdr.GetFieldAsString(3);

// display data

Console.WriteLine("\t {0} {1}", ordrnumb, custnumb);

}

// read next record until end of file

while (recordCustOrdr.Next());

}

}

catch (CTException E)

{

Handle_Exception(E);

}

}

//

// Display_OrderItems()

//

// This function displays the contents of a table. ctdbFirstRecord() and

// ctdbNextRecord() fetch the record. Then each field is parsed and displayed

//

static void Display_OrderItems()

{

string itemnumb;

string ordrnumb;

Console.WriteLine("\n\tOrderItems Table...");

try

{

// read first record

if (recordOrdrItem.First())

{

do

{

// get field data from record buffer

ordrnumb = recordOrdrItem.GetFieldAsString(2);

itemnumb = recordOrdrItem.GetFieldAsString(3);

// display data

Console.WriteLine("\t {0} {1}", ordrnumb, itemnumb);

}

// read next record until end of file

while (recordOrdrItem.Next());

}

}

catch (CTException E)

{

Handle_Exception(E);

}

}

TOCIndex