CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
}
// The DataTableReader works just like the DataReader.
while (dtReader.Read())
{
for (int i = 0; i < dtReader.FieldCount; i++)
{
Console.Write("{0}\t", dtReader.GetValue(i).ToString().Trim());
}
Console.WriteLine();
}
dtReader.Close();
Notice that the DataTableReader works identically to the data reader object of your data provider. A
DataTableReader can be an ideal choice when you need to pump out the data within a DataTable quickly,
without needing to traverse the internal row and column collections. Now assume you have updated the
previous PrintDataSet() method to invoke PrintTable(), rather than drilling into the Rows and Columns
collections.
static void PrintDataSet(DataSet ds)
{
// Print out the DataSet name, plus any extended properties.
Console.WriteLine("DataSet is named: {0}", ds.DataSetName);
foreach (System.Collections.DictionaryEntry de in ds.ExtendedProperties)
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
Console.WriteLine();
foreach (DataTable dt in ds.Tables)
{
Console.WriteLine("=> {0} Table:", dt.TableName);
// Print out the column names.
for (int curCol = 0; curCol < dt.Columns.Count; curCol++)
{
Console.Write(dt.Columns[curCol].ColumnName.Trim() + "\t");
}
Console.WriteLine("\n----------------------------------");
}
// Call our new helper method.
PrintTable(dt);
}
When you run the application, the output is identical to that shown previously. The only difference
is how you access the DataTable’s contents internally.
Serializing DataTable/DataSet Objects As XML
DataSets and DataTables both support the WriteXml() and ReadXml() methods. WriteXml() allows you to
persist an object’s content to a local file (as well as into any System.IO.Stream-derived type) as an XML
document. ReadXml() allows you to hydrate the state of a DataSet (or DataTable) from a given XML
874