CHAPTER 21 ADO.NET PART I: THE CONNECTED LAYER
private static void DisplayTable(DataTable dt)
{
// Print out the column names.
for (int curCol = 0; curCol < dt.Columns.Count; curCol++)
{
Console.Write(dt.Columns[curCol].ColumnName + "\t");
}
Console.WriteLine("\n----------------------------------");
// Print the DataTable.
for (int curRow = 0; curRow < dt.Rows.Count; curRow++)
{
for (int curCol = 0; curCol < dt.Columns.Count; curCol++)
{
Console.Write(dt.Rows[curRow][curCol].ToString() + "\t");
}
Console.WriteLine();
}
}
If you would prefer to call the GetAllInventoryAsList() method of InventoryDAL, you could
implement a method named ListInventoryViaList(), like so:
private static void ListInventoryViaList(InventoryDAL invDAL)
{
// Get the list of inventory.
List record = invDAL.GetAllInventoryAsList();
}
foreach (NewCar c in record)
{
Console.WriteLine("CarID: {0}, Make: {1}, Color: {2}, PetName: {3}",
c.CarID, c.Make, c.Color, c.PetName);
}
Implementing the DeleteCar() Method
Deleting an existing automobile is as simple as asking the user for the ID of the car and passing this to
the DeleteCar() method of the InventoryDAL type, as seen here:
private static void DeleteCar(InventoryDAL invDAL)
{
// Get ID of car to delete.
Console.Write("Enter ID of Car to delete: ");
int id = int.Parse(Console.ReadLine());
// Just in case you have a referential integrity
// violation!
try
{
invDAL.DeleteCar(id);
}
catch(Exception ex)
850