The Tables of a Data Set |
|
The tables that belong to a DataSet object are stored in a property called Tables. TheDataSet.Tables property is an object of type DataTableCollection. TheDataTableCollection is a class that provides everything you need to add, locate, or manage any table that belongs to a DataSet object.
The DataTableCollection class implements the GetEnumerator() method of theIEnumerable interface. This allows you to use a foreach loop to visit each member table of the collection. Once you have reached a table in the collection, you can access any of its public properties or methods. Here is an example of applying foreach on a collection of tables of adata set to list their names: using System;
using System.Data;
public class VideoCollection
{
public DataTable tblActors;
public DataTable tblFormats;
public DataTable tblRatings;
public DataTable tblDirectors;
public DataTable tblVideoCategories;
public DataSet dsVideoCollection;
public VideoCollection()
{
dsVideoCollection = new DataSet("Videos");
tblDirectors = new DataTable();
tblDirectors.TableName = "Directors";
dsVideoCollection.Tables.Add(tblDirectors);
tblVideoCategories = new DataTable("Categories");
dsVideoCollection.Tables.Add(tblVideoCategories);
tblRatings = dsVideoCollection.Tables.Add("Ratings");
tblActors = dsVideoCollection.Tables.Add("Actors");
tblFormats = dsVideoCollection.Tables.Add("Formats");
}
public void ShowTables()
{
DataTableCollection tables = this.dsVideoCollection.Tables;
foreach (DataTable tbl in tables)
Console.WriteLine("Table Name: {0}", tbl.TableName);
}
}
public static class Program
{
static int Main(string[] args)
{
VideoCollection coll = new VideoCollection();
coll.ShowTables();
Console.WriteLine();
return 0;
}
}
This would produce: Table Name: Directors Table Name: Categories Table Name: Ratings Table Name: Actors Table Name: Formats Press any key to continue . . . You can use this approach to identity a table and then perform a desired operation on it.
Using the DataSet.Tables property, to add a created table to a DataSet object, call one of theAdd() methods of the DataTableCollection class. The first version of this method has the following syntax: public virtual DataTable Add(); This method can be used to add a new table that uses the default name. Here is an example: using System; using System.Data; public class VideoCollection { public DataTable tblRatings; public DataTable tblDirectors; public DataTable tblVideoCategories; public DataSet dsVideoCollection; public VideoCollection() { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; tblVideoCategories = new DataTable("Categories"); tblRatings = dsVideoCollection.Tables.Add(); } } If this is the first table added to the collection, it would be named Table1. The second version of the DataTableCollection.Add() method uses the following syntax: public virtual void Add(DataTable table); This version allows you to add a predefined or declared DataTable object. Here is an example: using System; using System.Data; public class VideoCollection { public DataTable tblRatings; public DataTable tblDirectors; public DataTable tblVideoCategories; public DataSet dsVideoCollection; public VideoCollection() { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); } } This second version of the method requires that you create a DataTable object first and the table probably has a name. Alternatively, if you want to add a table using its formal name, you can use the third version of this method. Its syntax is: public virtual DataTable Add(string name); This version works like the first except that, instead of the default name (such as Table1, Table2, etc), it lets you specify the desired name of the new table. Here are examples: using System; using System.Data; public class VideoCollection { public DataTable tblActors; public DataTable tblFormats; public DataTable tblRatings; public DataTable tblDirectors; public DataTable tblVideoCategories; public DataSet dsVideoCollection; public VideoCollection() { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); } }
Instead of adding one table at a time, you can create a list of tables and then add it to theDataSet.Tables collection. To support this operation, the DataTableCollection is equipped with the AddRange() method. Its syntax is: public void AddRange(DataTable[] tables); This method expects an array of DataTable objects as argument. Here is an example: using System; using System.Data; public class BookCollection { private void Create() { DataSet dsBooks = new DataSet("Book"); DataTable dtCategories = new DataTable("Categorie"); DataTable dtAuthors = new DataTable("Author"); DataTable dtPublishers = new DataTable("Publisher"); DataTable dtBooks = new DataTable("Book"); DataTable[] colTables = { dtCategories, dtAuthors, dtPublishers, dtBooks }; dsBooks.Tables.AddRange(colTables); } }
After creating the tables that are part of an application, before performing any operation on a table, you must first retrieve its reference. This can be done by locating the particular desired table from the collection. To locate a table in the DataSet.Tables collection, the DataTableCollection class is equipped with the Item property in two versions. To locate a table using its name, use the following version of this property: public DataTable this[string name] {get;} To use this property, enter the object name of the table in the square brackets of theDataTableCollection[] property. Here is an example: using System;
using System.Data;
public class VideoCollection
{
public DataTable tblActors;
public DataTable tblFormats;
public DataTable tblRatings;
public DataTable tblDirectors;
public DataTable tblVideoCategories;
public DataSet dsVideoCollection;
public VideoCollection()
{
dsVideoCollection = new DataSet("Videos");
tblDirectors = new DataTable();
tblDirectors.TableName = "Directors";
dsVideoCollection.Tables.Add(tblDirectors);
tblVideoCategories = new DataTable("Categories");
dsVideoCollection.Tables.Add(tblVideoCategories);
tblRatings = dsVideoCollection.Tables.Add();
tblActors = dsVideoCollection.Tables.Add("Actors");
tblFormats = dsVideoCollection.Tables.Add("Formats");
}
}
public static class Program
{
static int Main(string[] args)
{
VideoCollection coll = new VideoCollection();
DataTable tbl = coll.dsVideoCollection.Tables["Directors"];
Console.WriteLine("Table Name: {0}", tbl.TableName);
return 0;
}
}
This would produce: Table Name: Directors Press any key to continue . . . Instead of locating a table by its name, you can use its index from the collection. To do this, you can use the second version of the DataTableCollection[] property. Its syntax is: public DataTable this[int index] {get;} This property expects as argument the index of the table in the DataSet.Tables collection. Here is an example: using System;
using System.Data;
public class VideoCollection
{
public DataTable tblActors;
public DataTable tblFormats;
public DataTable tblRatings;
public DataTable tblDirectors;
public DataTable tblVideoCategories;
public DataSet dsVideoCollection;
public VideoCollection()
{
dsVideoCollection = new DataSet("Videos");
tblDirectors = new DataTable();
tblDirectors.TableName = "Directors";
dsVideoCollection.Tables.Add(tblDirectors);
tblVideoCategories = new DataTable("Categories");
dsVideoCollection.Tables.Add(tblVideoCategories);
tblRatings = dsVideoCollection.Tables.Add();
tblActors = dsVideoCollection.Tables.Add("Actors");
tblFormats = dsVideoCollection.Tables.Add("Formats");
}
}
public static class Program
{
static int Main(string[] args)
{
VideoCollection coll = new VideoCollection();
DataTable tbl = coll.dsVideoCollection.Tables[3];
Console.WriteLine("Table Name: {0}", tbl.TableName);
return 0;
}
}
This would produce: Table Name: Actors Press any key to continue . . . If you provide an index below or beyond the number of tables in the set, the compiler would throw an IndexOutOfRangeException exception. To avoid this, you can request the index of the table. To do this, call the DataTableCollection.IndexOf() method. It is overloaded in two versions. One of the versions takes as argument the variable name of the table. The syntax of this method is: public virtual int IndexOf(DataTable table); Here is an example of calling this method: using System;
using System.Data;
public class VideoCollection
{
public DataTable tblActors;
public DataTable tblFormats;
public DataTable tblRatings;
public DataTable tblDirectors;
public DataTable tblVideoCategories;
public DataSet dsVideoCollection;
public VideoCollection()
{
dsVideoCollection = new DataSet("Videos");
tblDirectors = new DataTable();
tblDirectors.TableName = "Directors";
dsVideoCollection.Tables.Add(tblDirectors);
tblVideoCategories = new DataTable("Categories");
dsVideoCollection.Tables.Add(tblVideoCategories);
tblRatings = dsVideoCollection.Tables.Add();
tblActors = dsVideoCollection.Tables.Add("Actors");
tblFormats = dsVideoCollection.Tables.Add("Formats");
}
public void LocateTable()
{
int index = dsVideoCollection.Tables.IndexOf(tblActors);
Console.WriteLine("Table Index: {0}", index.ToString());
}
}
public static class Program
{
static int Main(string[] args)
{
VideoCollection coll = new VideoCollection();
coll.LocateTable();
return 0;
}
}
This would produce: Table Index: 3 Press any key to continue . . . Instead of using the variable name of the table, you can locate it using its formal name. To do this, call the following version of the IndexOf() method: public virtual int IndexOf(string tableName); When the tables of a DataSet have been created, you can get their list as an array using theDataTableCollection.List property. This property returns an ArrayList type of list. Instead of directly locating a table, you may be interested to know whether a particular table exists in the DataSet.Tables collection. To check this, you can call theDataTableCollection.Contains() method. Its syntax is: public bool Contains(string name); This method expects the object name of a table as argument. If the table exists in the collection, this method returns true. Here is an example: using System; using System.Data; public class VideoCollection { public DataTable tblActors; public DataTable tblFormats; public DataTable tblRatings; public DataTable tblDirectors; public DataTable tblVideoCategories; public DataSet dsVideoCollection; public VideoCollection() { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); } } public static class Program { static int Main(string[] args) { VideoCollection coll = new VideoCollection(); if( coll.dsVideoCollection.Tables.Contains("Actors") ) Console.WriteLine("The Actors table exists"); else Console.WriteLine("The Actors table does not exist"); Console.WriteLine(); if (coll.dsVideoCollection.Tables.Contains("VideoTypes")) Console.WriteLine("The VideoTypes table exists"); else Console.WriteLine("The VideoTypes table does not exist"); Console.WriteLine(); return 0; } } This would produce: The Actors table exists The VideoTypes table does not exist Press any key to continue . . .
|