Data Tables |
|
Imagine you have a list of movie directors and you want to group their names into a list. Here is an example: Rob Reiner, Jonathan Lynn, Bruce Beresford, Jonathan Demme, Adrian Lyne This is a one-dimensional list like a simple array. While working on this list, you may decide to create a video collection and make the above items into a formal list. A typical movie provides such information as its length, its rating, the year it was released, etc. To create such a list, you would group items by categories. One category may contain the titles of the videos. Another category may contain the names of the directors, and so on. To better organize a list, you may create each category, then enter the value of each category that corresponds to a particular video. Here is an example:
This type of list is called a table: A table is a two-dimensional list that contains one or different categories of items and each category is represented with a particular value. A category of values is called a column. Under each category, you may have a group of values that belong to the same entry. Such a group of values is called a row or a record. In the above table, the values "A Few Good Men", "Rob Reiner", "1992", "138 Minute", "VH", and "R" constitute one row or record.
To support the creation and management of a table, the .NET Framework provides the DataTable class that is defined in the System.Data namespace. There are various ways you can create a table. You can declare a variable of type DataTable. To initialize the variable, the DataTable class is equipped with three constructors. The default constructor allows you to create a table without giving more details, especially without formally naming it. Here is an example: using System; using System.Data; public class VideoCollection { public DataSet dsVideoCollection; public VideoCollection() { dsVideoCollection = new DataSet("Videos"); DataTable tblDirectors = new DataTable(); } } If you are planning to refer to the table from more than one method, you should declare it globally. Here is an example: using System; using System.Data; public class VideoCollection { public DataTable tblDirectors; public DataSet dsVideoCollection; public VideoCollection() { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); } } The name of the DataTable variable is required when creating a table. This name will be used by you and the compiler to identify the table. In some cases, you may want the table to hold an object name that you would want to use later as we will see with some methods. To provide a formal name to a table when creating it, you can use the second constructor of the DataTable class. Its syntax is: public DataTable(string tableName); This constructor expects as argument a string that would constitute the object name of the table. Here is an example: using System; using System.Data; public class VideoCollection { public DataTable tblDirectors; public DataTable tblVideoCategories; public DataSet dsVideoCollection; public VideoCollection() { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblVideoCategories = new DataTable("Categories"); } } If you have already declared a DataTable variable using either of both constructors and decide to specify or change its name, you can assign a string to the DataTable.TableName property. Here is an example: using System; using System.Data; public class VideoCollection { 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"); } } The TableName name is primarily optional but it is used by various methods as we will see. If you don't provide it, for example if you declare a DataTable variable using the default constructor and don't specify its name through the DataTable.TableName property, the compiler would generate a default name for the table, such as Table1, Table2, and so on. In the same way, you can create as many tables as you judge necessary for your application and as we will see when moving on. After creating a table, you can add it to a DataSet object. |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|