Databases Fundamentals: 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. In 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 Minutes", "VH", and "R" constitute one row one 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: private void btnCreate_Click(object sender, System.EventArgs e) { DataTable dtDirectors = new DataTable(); } If you are planning to refer to the table from more than one method or event, you should declare it globally. Here is an example: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication30 { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnCreate; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private DataSet dsVideoCollection; private DataTable dtDirectors; . . . No Change private void btnCreate_Click(object sender, System.EventArgs e) { dsVideoCollection = new DataSet("Videos"); dtDirectors = 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 on 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.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication30 { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnCreate; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private DataSet dsVideoCollection; private DataTable dtDirectors; private DataTable dtVideoCategories; . . . No Change /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void btnCreate_Click(object sender, System.EventArgs e) { dsVideoCollection = new DataSet("Videos"); dtVideoCategories = new DataTable("Categories"); dtDirectors = new DataTable(); } } } 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: private void btnCreate_Click(object sender, System.EventArgs e) { dsVideoCollection = new DataSet("Videos"); dtVideoCategories = new DataTable("Categories"); dtDirectors = new DataTable(); dtDirectors.TableName = "Directors"; } 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. The tables that belong to a DataSet object are stored in a property called Tables. The DataSet.Tables property is an object of type DataTableCollection. The DataTableCollection is a class that provides everything you need to add, locate, or manage any table that belongs to a DataSet. Using the DataSet.Tables property, to add a created table to a DataSet, call one of the Add() 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 a default name. Here is an example: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication30 { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnCreate; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private DataSet dsVideoCollection; private DataTable dtDirectors; private DataTable dtVideoCategories; private DataTable dtRatings; . . . No Change private void btnCreate_Click(object sender, System.EventArgs e) { dsVideoCollection = new DataSet("Videos"); dtVideoCategories = new DataTable("Categories"); dtDirectors = new DataTable(); dtDirectors.TableName = "Directors"; dtRatings = 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: private void btnCreate_Click(object sender, System.EventArgs e) { dsVideoCollection = new DataSet("Videos"); dtVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(dtVideoCategories); dtDirectors = new DataTable(); dtDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(dtDirectors); dtRatings = dsVideoCollection.Tables.Add(); } This second version of this method requires that you create a DataTable object first and the table probably have 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 object name of the new table. Here are examples: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication30 { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnCreate; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private DataSet dsVideoCollection; private DataTable dtDirectors; private DataTable dtVideoCategories; private DataTable dtRatings; private DataTable dtActors; private DataTable dtFormats; . . . No Change private void btnCreate_Click(object sender, System.EventArgs e) { dsVideoCollection = new DataSet("Videos"); dtVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(dtVideoCategories); dtDirectors = new DataTable(); dtDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(dtDirectors); dtRatings = dsVideoCollection.Tables.Add(); dtActors = dsVideoCollection.Tables.Add("Actors"); dtFormats = dsVideoCollection.Tables.Add("Formats"); } } } Instead of adding one table at a time, you can create a list of tables and then add it to the DataSet.Tables collection. To support this operation, the DataTableCollection class 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: private void btnTables_Click(object sender, System.EventArgs e) { DataSet dsBooks = new DataSet("Books"); DataTable dtCategories = new DataTable("Categories"); DataTable dtAuthors = new DataTable("Authors"); DataTable dtPublishers = new DataTable("Publishers"); DataTable dtBooks = new DataTable("Books"); DataTable[] colTables = { dtCategories, dtAuthors, dtPublishers, dtBooks }; dsBooks.Tables.AddRange(colTables); } While it is perfectly possible to programmatically create a table, the Visual Studio .NET programming environment provides graphical means of creating a table, without guessing. This allows you to visually set the characteristics of the table and let Visual Studio .NET write the code for you. As mentioned already, the tables of a DataSet object are stored in its Tables properties. In the same way, the Properties window provides the Tables field that allows you to visually create the tables.
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 that ships 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 the DataTableCollection.Item[] property. Here is an example: private void btnTable_Click(object sender, System.EventArgs e) { DataTable tbl = this.dsVideoCollection.Tables["Directors"]; Text = tbl.TableName; } 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.Item[] 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: private void btnTable_Click(object sender, System.EventArgs e) { DataTable tbl = this.dsVideoCollection.Tables[3]; Text = tbl.TableName; } 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 with 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: private void btnTable_Click(object sender, System.EventArgs e) { int index = this.dsVideoCollection.Tables.IndexOf(this.dtFormats); Text = index.ToString(); } Instead of using the variable name of the table, you can locate it using its object name. To do this, call the following version of the IndexOf() method: public virtual int IndexOf(string name); When the tables of a DataSet object have been created, you can get their list as an array using the DataTableCollection.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 the DataTableCollection.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: private void btnTable_Click(object sender, System.EventArgs e) { if( this.dsVideoCollection.Tables.Contains("Actors") ) Text = "The Actors table exists"; else Text = "Unknown Table"; }
If you happen to have a table you don't need anymore or whose role is undefined in your application, you can delete that table. This operation is supported by the DataTableCollection.Remove() method that is overloaded with two versions. To locate a table using its variable declared name and delete it, you can use the following version: public void Remove(DataTable table); This version expects the name that was used to declare the DataTable object. If the table exists in the DateSet.Tables collection, it would be deleted. Here is an example: private void btnRemove_Click(object sender, System.EventArgs e) { this.dsVideoCollection.Tables.Remove(this.dtFormats); } When calling this method, if the DataTable object passed as argument is not found, the compiler would throw either an ArgumentNullException or an ArgumentException exceptions. For this reason, before deleting a table, you should first check its existence by calling the DataTableCollection.Contains() method. To delete a table using its object name, you can call the following version of the DataTableCollection.Remove() method: public void Remove(string name); This method expects the formal name of the table as argument. If a table exists under that name, it would be deleted. If no table with that name is found, the compiler would throw an ArgumentException exception. Once again, you should first check that a table with that name exists before deleting it. To delete a table you created visually, in the Tables Collection Editor, click it in the Members list and click the Remove button. Besides checking the existence of a table, even if the table is found, it may not allow the user to delete it. To find out whether a table can be deleted, you can call the DataTableCollection.CanRemove() method. Its syntax is: public bool CanRemove(DataTable table); To delete all tables of a DataSet, you can call the DataTableCollection.Clear() method. Its syntax is: public void Clear(); Calling this method would remove all DataTable objects of the DataSet object. |
|
||
Previous | Copyright © 2004-2010 FunctionX, Inc. | Next |
|