Free mag vol1 | Page 927

CHAPTER 22  ADO.NET PART II: THE DISCONNECTED LAYER } // 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 run your program now, you’ll see the following output (your time stamp and GUID value will differ, of course): ***** Fun with DataSets ***** DataSet is named: Car Inventory Key = TimeStamp, Value = 1/22/2012 6:41:09 AM Key = DataSetID, Value = 11c533ed-d1aa-4c82-96d4-b0f88893ab21 Key = Company, Value = Mikko's Hot Tub Super Store => Inventory Table: CarID Make Color PetName ---------------------------------0 BMW Black Hamlet 1 Saab Red Sea Breeze Processing DataTable Data Using DataTableReader Objects Given your work in Chapter 21, you should notice that the manner in which you process data using the connected layer (e.g., data reader objects) and the disconnected layer (e.g., DataSet objects) is quite different. Working with a data reader typically involves establishing a while loop, calling the Read() method, and using an indexer to pluck out the name/value pairs. On the other hand, DataSet processing typically involves a series of iteration constructs to drill into the data within the tables, rows, and columns (remember that DataReader requires an open database connection so that it can read the data from the actual database). DataTables support a method named CreateDataReader(). This method allows you to obtain the data within a DataTable using a data reader–like navigation scheme (the data reader will now read data from the in-memory DataTable, not from the actual database, so there’s no database connection involved here). The major benefit of this approach is that you now use a single model to process data, regardless of which layer of ADO.NET you use to obtain it. Assume you have authored a new method in your Program class named PrintTable(), as follows: static void PrintTable(DataTable dt) { // Get the DataTableReader type. DataTableReader dtReader = dt.CreateDataReader(); 873