Previous Topic

Next Topic

Manage

The manage step 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
      //

      static void Add_CustomerMaster_Records()
      {
         String[] data = {
            "('1000','92867','CA','1','Bryan Williams','2999 Regency','Orange')",
            "('1001','61434','CT','1','Michael Jordan','13 Main','Harford')",
            "('1002','73677','GA','1','Joshua Brown','4356 Cambridge','Atlanta')",
            "('1003','10034','MO','1','Keyon Dooling','19771 Park Avenue','Columbia')"
         };

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

         try
         {
            // add one record at time to table
            for (int i = 0; i < data.Length; i++)
            {
               cmd.CommandText = "INSERT INTO custmast VALUES " + data[i];
               cmd.ExecuteNonQuery();
            }
         }
         catch (CtreeSqlException e)
         {
            Handle_Exception(e);
         }
         catch (Exception e)
         {
            Handle_Exception(e);
         }
      }


      //
      // Add_ItemMaster_Records()
      //
      // This function adds records to table ItemMaster from an
      // array of strings
      //

      static void Add_ItemMaster_Records()
      {
         String[] data = {
            "(10,19.95,'1','Hammer')",
            "(3,  9.99,'2','Wrench')",
            "(4, 16.59,'3','Saw')",
            "(1,  3.98,'4','Pliers')"
         };

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

         try
         {
            // add one record at time to table
            for (int i = 0; i < data.Length; i++)
            {
               cmd.CommandText = "INSERT INTO itemmast VALUES " + data[i];
               cmd.ExecuteNonQuery();
            }
         }
         catch (CtreeSqlException e)
         {
            Handle_Exception(e);
         }
         catch (Exception 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.
      //

      static void Add_Transactions()
      {
         String[,] orders = {
            { "09/01/2002", "09/05/2002", "1", "1001" },
            { "09/02/2002", "09/06/2002", "2", "9999" },  // bad customer number
            { "09/22/2002", "09/26/2002", "3", "1003" }
         };

         String[,] items = {
            { "1", "1", "2", "1" },
            { "1", "2", "1", "2" },
            { "2", "1", "1", "3" },
            { "2", "2", "3", "4" },
            { "3", "1", "2", "3" },
            { "3", "2", "2", "99"}  // bad item number
         };

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

         for (int i = 0, j = 0; i < orders.GetLength(0); i++)
         {
            try
            {
               cmd.Transaction = conn.BeginTransaction();
               cmd.CommandText = "INSERT INTO custordr VALUES (" +
                  "'" + orders[i, 0] + "', " +
                  "'" + orders[i, 1] + "', " +
                  "'" + orders[i, 2] + "', " +
                  "'" + orders[i, 3] + "')";
               cmd.ExecuteNonQuery();
            }
            catch (CtreeSqlException e)
            {
               Handle_Exception(e);
            }
            catch (Exception e)
            {
               Handle_Exception(e);
            }

            // process order items
            while (items[j, 0].CompareTo(orders[i, 2]) == 0)
            {
               try
               {
                  // add item record
                  cmd.CommandText = "INSERT INTO ordritem VALUES (" +
                     items[j, 1] + ", " +
                     items[j, 2] + ", " +
                     "'" + items[j, 0] + "', " +
                     "'" + items[j, 3] + "')";
                  cmd.ExecuteNonQuery();
               }
               catch (CtreeSqlException e)
               {
                  Handle_Exception(e);
               }
               catch (Exception e)
               {
                  Handle_Exception(e);
               }

               // bump to next item
               j++;

               // exit the while loop on last item
               if (j >= items.GetLength(0))
                  break;
            }

            try
            {
               cmd.Transaction.Commit();
            }
            catch (CtreeSqlException e)
            {
               Handle_Exception(e);
               cmd.Transaction.Rollback();
            }
            catch (Exception e)
            {
               Handle_Exception(e);
            }
         }
      }


      //
      // Display_CustomerOrders()
      //
      // This function displays the contents of CustomerOrders table.
      //

      static void Display_CustomerOrders()
      {
         Console.WriteLine("\n\tCustomerOrders Table...");

         try
         {
            // execute a query statement
            cmd.CommandText = "SELECT * FROM custordr";
            rdr = (CtreeSqlDataReader)cmd.ExecuteReader();

            // fetch and display each individual record
            while (rdr.Read())
            {
               Console.WriteLine("\t   {0}   {1}", rdr.GetString(2), rdr.GetString(3));
            }
            rdr.Close();
         }
         catch (CtreeSqlException e)
         {
            Handle_Exception(e);
         }
         catch (Exception e)
         {
            Handle_Exception(e);
         }
      }


      //
      // Display_OrderItems()
      //
      // This function displays the contents of OrderItems table.
      //

      static void Display_OrderItems()
      {
         Console.WriteLine("\n\tOrderItems Table...");

         try
         {
            // execute a query statement
            cmd.CommandText = "SELECT * FROM ordritem";
            rdr = (CtreeSqlDataReader)cmd.ExecuteReader();

            // fetch and display each individual record
            while (rdr.Read())
            {
               Console.WriteLine("\t   {0}   {1}", rdr.GetString(2), rdr.GetString(3));
            }
            rdr.Close();
         }
         catch (CtreeSqlException e)
         {
            Handle_Exception(e);
         }
         catch (Exception e)
         {
            Handle_Exception(e);
         }
      }