DataSet Collections

for RuBoard

When data is placed into a DataSet , the related tables and columns are also retrieved. Each data set has collections that represent all the tables, columns and data rows associated with it.

The HotelBroker class in the Case Study has a method called PrintHotels that illustrates how to retrieve this information and write it to a Console. The hotelsDataset is a data set that has already been filled with the data from the HotelBroker database.

 DataTable t = hotelsDataset.Tables["Hotels"];  if (t == null)    return;  foreach(DataColumn c in t.Columns)    Console.Write("{0, -20}", c.ColumnName);   Console.WriteLine("");  foreach (DataRow r in t.Rows)  {    for (int i = 0; i < t.Columns.Count; i++)      {        Type type = r[i].GetType();        if (type.FullName == "System.Int32")          Console.Write("{0, -20}", r[i]);        else        {          string s = r[i].ToString();          s = s.Trim();          Console.Write("{0, -20}", s);        }      }    Console.WriteLine("");  }  Console.WriteLine(""); 

The Tables collection includes all the DataTable instances in the DataSet . In this particular case there is only one, so there is no need to iterate through that collection. The program then iterates through all the columns in the table and sets them up as headers for the data that will be printed out. After the headers have been set up, all the rows in the table are iterated through. For each column in the row, we ascertain its type and print out the value appropriately. The program checks only for the types that are in the Hotels database table. Checking for types instead of printing out the row values as object enables us to format the data appropriately.

As we will show later, you can populate the dataset through these collections without having to obtain it from a data source. You can just add tables, columns, and rows to the appropriate collections.

for RuBoard


Application Development Using C# and .NET
Application Development Using C# and .NET
ISBN: 013093383X
EAN: 2147483647
Year: 2001
Pages: 158

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net