示例#1
0
        //
        // 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
                        recordOrdrItem.GetFieldValue(1, out quantity);
                        // 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);
                        recordItemMast.GetFieldValue(1, out price);

                        // 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);
            }
        }