Manage() provides data management functionality for your application and/or process.
Below is the code for Manage():
//
// Manage()
//
// This function performs record adds and updates using locking
//
static void Manage()
{
Console.WriteLine("MANAGE");
// populate the table with data
Add_CustomerMaster_Records();
// display contents of table
Display_Records();
// update a record under locking control
Update_CustomerMaster_Record();
// display again after update and effects of lock
Display_Records();
}
//
// Add_CustomerMaster_Records()
//
// This function adds records to table CustomerMaster from an
// array of strings
//
public struct DATA_RECORD
{
// struct members
public string number, zipcode, state, rating, name, address, city;
// struct constructor
public DATA_RECORD(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()
{
DATA_RECORD[] data = new DATA_RECORD[4];
data[0] = new DATA_RECORD("1000", "92867", "CA", "1", "Bryan Williams", "2999 Regency", "Orange");
data[1] = new DATA_RECORD("1001", "61434", "CT", "1", "Michael Jordan", "13 Main", "Harford");
data[2] = new DATA_RECORD("1002", "73677", "GA", "1", "Joshua Brown", "4356 Cambridge", "Atlanta");
data[3] = new DATA_RECORD("1003", "10034", "MO", "1", "Keyon Dooling", "19771 Park Avenue", "Columbia");
int nRecords = data.Length;
Delete_Records(MyRecord);
Console.WriteLine("\tAdd records...");
try
{
for (int i = 0; i < nRecords; i++)
{
MyRecord.Clear();
// populate record buffer with data
MyRecord.SetFieldAsString(0, data[i].number);
MyRecord.SetFieldAsString(1, data[i].zipcode);
MyRecord.SetFieldAsString(2, data[i].state);
MyRecord.SetFieldAsString(3, data[i].rating);
MyRecord.SetFieldAsString(4, data[i].name);
MyRecord.SetFieldAsString(5, data[i].address);
MyRecord.SetFieldAsString(6, data[i].city);
// add record
MyRecord.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
{
// enable session-wide lock flag
MySession.Lock(LOCK_MODE.WRITE_BLOCK_LOCK);
// read first record
found = record.First();
while (found) // while records are found
{
// delete record
record.Delete();
// read next record
found = record.Next();
}
// reset session-wide locks
MySession.Unlock();
}
catch (CTException E)
{
Handle_Exception(E);
}
}
//
// Display_Records()
//
// This function displays the contents of a table. First() and Next()
// fetch the record. Then each field is parsed and displayed
//
static void Display_Records()
{
bool found;
string custnumb;
string custname;
Console.Write("\tDisplay records...");
try
{
// read first record
found = MyRecord.First();
while (found)
{
custnumb = MyRecord.GetFieldAsString(0);
custname = MyRecord.GetFieldAsString(4);
Console.WriteLine("\n\t\t{0,-8}{1,-20}", custnumb, custname);
// read next record
found = MyRecord.Next();
}
}
catch (CTException E)
{
Handle_Exception(E);
}
}
//
// Update_CustomerMaster_Records()
//
// Update one record under locking control to demonstrate the effects
// of locking
//
static void Update_CustomerMaster_Record()
{
Console.WriteLine("\tUpdate Record...");
try
{
// enable session-wide lock flag
MySession.Lock(LOCK_MODE.WRITE_BLOCK_LOCK);
MyRecord.Clear();
MyRecord.SetFieldAsString(0, "1003");
// find record by customer number
if (MyRecord.Find(FIND_MODE.EQ))
{
MyRecord.SetFieldAsString(4, "KEYON DOOLING");
// rewrite record
MyRecord.Write();
Console.WriteLine("\tPress <ENTER> key to unlock");
Console.ReadLine();
}
// reset session-wide locks
MySession.Unlock();
}
catch(CTException E)
{
Handle_Exception(E);
}
}