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()
{
int quantity;
double price, total;
string itemnumb, custnumb, ordrnumb, custname;
bool isOrderFound, isItemFound;
Console.WriteLine("MANAGE");
// populate the tables with data
Add_CustomerMaster_Records();
Add_CustomerOrders_Records();
Add_OrderItems_Records();
Add_ItemMaster_Records();
// perform a query:
// list customer name and total amount per order
// name total
// @@@@@@@@@@@@@ $xx.xx
// for each order in the CustomerOrders table
// fetch order number
// fetch customer number
// fetch name from CustomerMaster table based on customer number
// for each order item in OrderItems table
// fetch item quantity
// fetch item number
// fetch item price from ItemMaster table based on item number
// next
// next
Console.WriteLine("\n\tQuery Results");
try
{
// get the first order
isOrderFound = recordCustOrdr.First();
while (isOrderFound) // for each order in the CustomerOrders table
{
// fetch order number
ordrnumb = recordCustOrdr.GetFieldAsString(2);
// fetch customer number
custnumb = recordCustOrdr.GetFieldAsString(3);
// fetch name from CustomerMaster table based on customer number
recordCustMast.Clear();
recordCustMast.SetFieldAsString(0, custnumb);
if (!recordCustMast.Find(FIND_MODE.EQ))
continue; // not possible in our canned example
custname = recordCustMast.GetFieldAsString(4);
// fetch item price from OrderItems table
recordOrdrItem.Clear();
recordOrdrItem.SetFieldAsString(2, ordrnumb);
// define a recordset to scan only items applicable to this order
recordOrdrItem.RecordSetOn(6);
isItemFound = recordOrdrItem.First();
total = 0;
while (isItemFound) // for each order item in OrderItems table
{
// fetch item quantity
quantity = recordOrdrItem.GetFieldAsSigned(1);
// fetch item number
itemnumb = recordOrdrItem.GetFieldAsString(3);
// fetch item price from ItemMaster table based on item number
recordItemMast.Clear();
recordItemMast.SetFieldAsString(2, itemnumb);
recordItemMast.Find(FIND_MODE.EQ);
price = recordItemMast.GetFieldAsFloat(1);
// calculate order total
total += (price * quantity);
isItemFound = recordOrdrItem.Next();
}
recordOrdrItem.RecordSetOff();
// output data to stdout
Console.WriteLine("\t\t{0,-20}{1,-8}", custname, total);
// read next order
if (!recordCustOrdr.Next())
isOrderFound = false;
}
}
catch (CTException E)
{
Handle_Exception(E);
}
}
//
// 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
{
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();
}
}
catch(CTException E)
{
Handle_Exception(E);
}
}
//
// Add_CustomerOrders_Records()
//
// This function adds records to table CustomerOrders from an
// array of strings
//
public struct ORDER_DATA
{
// struct members
public string orderdate, promisedate, ordernum, customernum;
// struct constructor
public ORDER_DATA(string orderdate, string promisedate, string ordernum, string customernum)
{
this.orderdate = orderdate;
this.promisedate = promisedate;
this.ordernum = ordernum;
this.customernum = customernum;
}
};
static void Add_CustomerOrders_Records()
{
ORDER_DATA[] data = new ORDER_DATA[2];
data[0] = new ORDER_DATA("09/01/2002", "09/05/2002", "1", "1001");
data[1] = new ORDER_DATA("09/02/2002", "09/06/2002", "2", "1002");
int nRecords = data.Length;
CTDate orderdate = new CTDate();
CTDate promisedate = new CTDate();
Delete_Records(recordCustOrdr);
Console.WriteLine("\tAdd records in table CustomerOrders...");
try
{
for (int i = 0; i < nRecords; i++)
{
recordCustOrdr.Clear();
orderdate.StringToDate(data[i].orderdate, DATE_TYPE.MDCY_DATE);
promisedate.StringToDate(data[i].promisedate, DATE_TYPE.MDCY_DATE);
// populate record buffer with data
recordCustOrdr.SetFieldAsDate(0, orderdate);
recordCustOrdr.SetFieldAsDate(1, promisedate);
recordCustOrdr.SetFieldAsString(2, data[i].ordernum);
recordCustOrdr.SetFieldAsString(3, data[i].customernum);
// add record
recordCustOrdr.Write();
}
}
catch(CTException E)
{
Handle_Exception(E);
}
}
//
// Add_OrderItems_Records()
//
// This function adds records to table OrderItems from an
// array of strings
//
public struct ORDERITEM_DATA
{
// struct members
public int sequencenum, quantity;
public string ordernum, itemnum;
// struct constructor
public ORDERITEM_DATA(int sequencenum, int quantity, string ordernum, string itemnum)
{
this.sequencenum = sequencenum;
this.quantity = quantity;
this.ordernum = ordernum;
this.itemnum = itemnum;
}
};
static void Add_OrderItems_Records()
{
ORDERITEM_DATA[] data = new ORDERITEM_DATA[4];
data[0] = new ORDERITEM_DATA(1, 2, "1", "1");
data[1] = new ORDERITEM_DATA(2, 1, "1", "2");
data[2] = new ORDERITEM_DATA(3, 1, "1", "3");
data[3] = new ORDERITEM_DATA(1, 3, "2", "3");
int nRecords = data.Length;
Delete_Records(recordOrdrItem);
Console.WriteLine("\tAdd records in table OrderItems...");
try
{
for (int i = 0; i < nRecords; i++)
{
recordOrdrItem.Clear();
// populate record buffer with data
recordOrdrItem.SetFieldAsSigned(0, data[i].sequencenum);
recordOrdrItem.SetFieldAsSigned(1, data[i].quantity);
recordOrdrItem.SetFieldAsString(2, data[i].ordernum);
recordOrdrItem.SetFieldAsString(3, data[i].itemnum);
// add record
recordOrdrItem.Write();
}
}
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
{
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();
}
}
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
{
// read first record
found = record.First();
while (found) // while records are found
{
// delete record
record.Delete();
// read next record
found = record.Next();
}
}
catch (CTException E)
{
Handle_Exception(E);
}
}