The Records of a Table |
|
The Rows of a Table |
Introduction to Records |
In our description of tables, we saw that a table was made of one or various columns that represented some categories of data. Here is an example of a table with a few columns:
using System; using System.Data; namespace VideoCollection
public static class Program { static DataSet dsVideos; static DataColumn colShelfNumber; static DataColumn colTitle; static DataColumn colDirector; static DataColumn colLength; static DataColumn colYear; static DataColumn colRating; static DataTable tblVideos; static void CreateCollection() { dsVideos = new DataSet("Videos"); tblVideos = new DataTable("Video"); colShelfNumber = new DataColumn("ShelfNumber", Type.GetType("System.String")); tblVideos.Columns.Add(colShelfNumber); colTitle = new DataColumn("Title", Type.GetType("System.String")); tblVideos.Columns.Add(colTitle); colDirector = new DataColumn("Director", Type.GetType("System.String")); tblVideos.Columns.Add(colDirector); colLength = new DataColumn("Length", Type.GetType("System.String")); tblVideos.Columns.Add(colLength); colYear = new DataColumn("Year", Type.GetType("System.Int16")); tblVideos.Columns.Add(colYear); colRating = new DataColumn("Rating", Type.GetType("System.String")); tblVideos.Columns.Add(colRating); dsVideos.Tables.Add(tblVideos); } static int Main(string[] args) { CreateCollection(); return 0; } } }
After creating such a table and its columns, you (actually the user) can enter values in the table to make it a valuable list. Filling up a table with values is referred to as data entry.
The Row of a Table |
A record on a table is represented as a row (horizontal) of data. A row, or record, is an object based on the DataRow class.
To support the various records that belong to a table, the DataTable class is equipped with a property called Rows. The DataTable.Rows property is an object of the DataRowCollection class. The DataRowCollection class provides the necessary properties and methods you can use to create and manage records of a table.
Introduction to Data Entry |
When performing data entry and while doing it on a record, the record has a status that can be identified by the DataRow.RowState property which is a value based on the DataRowState enumerator.
A record on a table is represented as a row of data. To support the various records that belong to a table, the DataTable class is equipped with the Rows property which is an object of type DataRowCollection with each record being an object of type DataRow.
Before adding a new record to a table, you must let the table know. This is done by calling the DataTable.NewRow() method. Its syntax is:
public DataRow NewRow();
The DataTable.NewRow() method returns a DataRow object. Here is an example of calling it:
using System; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void CreateRecord() { DataRow rowVideo = tblVideos.NewRow(); } static int Main(string[] args) { return 0; } } }
Data Entry |
Introduction |
When you call the DataTable.NewRow() method, the record's status is DataRowState.Detached. After calling the DataTable.NewRow() method, you can specify the value that the column would carry. To do this, you must specify the table's column whose value you want to provide. You can locate a column based on an index as we mentioned already that the columns of a table are stored in the DataTable.Columns property which is based on the DataColumnCollection class. An example would be rowVideo["Title"], which specifies the column named Title. After specifying the column, assign it the desired but appropriate value based on the DataColumn.DataType value you would have provided. Here are examples of assigning values to the columns of a table:
using System; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void CreateRecord() { DataRow rowVideo = tblVideos.NewRow(); rowVideo[0] = "GT-682"; rowVideo[1] = "A Few Good Men"; rowVideo[2] = "Rob Reiner"; rowVideo[3] = "138 Minutes"; rowVideo[4] = 1992; rowVideo[5] = "R"; } static int Main(string[] args) { return 0; } } }
Each column can also be identified by its index in the table.
Adding a Record to a Table |
After specifying the value(s) of the column(s), you must add it (them) to the table. To do this, you must call the Add() method of the DataRowCollection class. This method is overloaded with two versions. One of the versions uses the following syntax:
public void Add(DataRow row);
This method expects the name of the record as argument, which would be the value returned by a previous call to the DataTable.NewRow() method. Here is an example:
using System; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void CreateRecord() { DataRow rowVideo = tblVideos.NewRow(); rowVideo[0] = "GT-682"; rowVideo[1] = "A Few Good Men"; rowVideo[2] = "Rob Reiner"; rowVideo[3] = "138 Minutes"; rowVideo[4] = 1992; rowVideo[5] = "R"; tblVideos.Rows.Add(rowVideo); } static int Main(string[] args) { return 0; } } }
When the record has been added to the table, the record has a status of DataRowState.Added.
Adding an Array of Records |
The above version of the DataRowCollection.Add() method means that you must identify each column before assigning a value to it. If you already know the sequence of columns and don't need to explicitly identify them, you can store all values in an array and simply add the array as a complete record. To support this, the DataRowCollection class provide another version of the .Add() method whose syntax is:
public virtual DataRow Add(object[] values);
Here is an example:
using System; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void CreateRecord() { object[] arrRecord = { "MM-258", "Fatal Attraction", "Adrian Lyne", 1987, "120 Minutes", "R" }; tblVideos.Rows.Add(arrRecord); } static int Main(string[] args) { return 0; } } }
There is an alternative to this second version of the DataRowCollection.Add() method. As opposed to passing an array of values to the Add() method, you can first define an array, assign that array to a DataRow variable, then pass that DataRow object to the Add() method. To support this technique, the DataRow class is equipped with an ItemArray property that expects an array. Here is an example
using System; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void CreateRecord() { object[] arrVideo = { "FD-205", "Her Alibi", "Bruce Beresford", "94 Minute", 1989, "PG-13" }; DataRow rowVideo = tblVideos.NewRow(); rowVideo.ItemArray = arrVideo; tblVideos.Rows.Add(rowVideo); } static int Main(string[] args) { return 0; } } }
After creating the records of a table, if a record contains invalid values, the DataRow.HasErrors property can help you identify them.
The Number of Records of a Table |
After you have created a table and its columns but before adding any row, the number of the table's record is set to 0. Every time you add a new record, the number of records is incremented by 1. To get the number of records that a table contains, access the Count property of its DataRowCollection. The Count property is inherited from the InternalDataCollectionBase class, which is the parent of many collection classes.
Saving the Records of a Table |
Introduction |
When the application closes, unfortunately, all the information created while the application was running is lost. While the first goal of an application is to create one or more lists used to organize information, probably the essence of an information-based or a data-based application is to preserve information created when using the application and be able to retrieve that information the next time the application runs, without re-creating it.
Of course, there are various ways you can save the information created in an application. As the DataSet class is equipped with all the necessary features used to create and manage one or more lists of an application, it also provides a very high level of saving the information stored in its lists.
Saving a Data Set |
Once a new record has been created or when the lists of the data set have been populated with information, you can save the changes and store them to a computer file. By default, the DataSet class is equipped to save its lists as XML. To support this, it is equipped with the WriteXml() method that is overloaded with various versions. One of the versions of this method uses the following syntax:
public void WriteXml(string fileName);
This method takes as argument the name of the new file or its path. When providing this argument, make sure you add the .xml extension to the file name. This method does two things: it checks the existence of the file and it saves it. If the file you provided is not found in the path, this method creates it and writes the record(s) to it. If the file exists already, this method opens it, finds its end, and appends the new data at the end. This makes the method very useful and friendly.
Here is an example of saving a data set using this method:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { static DataSet dsVideos; static DataColumn colShelfNumber; static DataColumn colTitle; static DataColumn colDirector; static DataColumn colLength; static DataColumn colYear; static DataColumn colRating; static DataTable tblVideos; static string strDirectory; static void CreateCollection() { dsVideos = new DataSet("Videos"); tblVideos = new DataTable("Video"); colShelfNumber = new DataColumn("ShelfNumber", Type.GetType("System.String")); tblVideos.Columns.Add(colShelfNumber); colTitle = new DataColumn("Title", Type.GetType("System.String")); tblVideos.Columns.Add(colTitle); colDirector = new DataColumn("Director", Type.GetType("System.String")); tblVideos.Columns.Add(colDirector); colLength = new DataColumn("Length", Type.GetType("System.String")); tblVideos.Columns.Add(colLength); colYear = new DataColumn("Year", Type.GetType("System.Int16")); tblVideos.Columns.Add(colYear); colRating = new DataColumn("Rating", Type.GetType("System.String")); tblVideos.Columns.Add(colRating); dsVideos.Tables.Add(tblVideos); strDirectory = @"C:\Video Collection"; Directory.CreateDirectory(strDirectory); dsVideos.WriteXml(strDirectory + @"\videos.xml"); } static void CreateRecord() { DataRow rowVideo = tblVideos.NewRow(); rowVideo[0] = "GT-682"; rowVideo[1] = "A Few Good Men"; rowVideo[2] = "Rob Reiner"; rowVideo[3] = "138 Minutes"; rowVideo[4] = 1992; rowVideo[5] = "R"; tblVideos.Rows.Add(rowVideo); dsVideos.WriteXml(strDirectory + @"\videos.xml"); } static int Main(string[] args) { CreateCollection(); CreateRecord(); return 0; } } }
If you want to control whether the file should be created from scratch, instead of passing the name of the file to this method, first create a stream using a Stream-derived class such as FileStream. This allows specifying the necessary options using the FileMode, FileAccess, and FileShare properties. Once the stream is ready, pass it to the WriteXml() method because it is also overloaded with the following syntax:
public void WriteXml(Stream stream);
Here is an example:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void CreateRecord() { FileStream fsVideos = new FileStream(strDirectory + @"\videos.xml", FileMode.Create, FileAccess.Write); object[] rowVideo = { "MM-258", "Fatal Attraction", "Adrian Lyne", "120 Minutes", 1987, "R" }; tblVideos.Rows.Add(rowVideo); dsVideos.WriteXml(fsVideos); fsVideos.Close(); } static int Main(string[] args) { CreateCollection(); CreateRecord(); return 0; } } }
If you want the file to be formatted as text, you can use the following version of the method:
public void WriteXml(TextWriter writer);
If you prefer to use an XmlWriter variable to manage the file, use the following version of the method:
public void WriteXml(XmlWriter writer);
Obviously to use this method, you must first define an XmlWriter type of variable.
Application: Creating a Data Set |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Data; namespace cpap4 { public class Inventory { private DataColumn colPartNumber; private DataColumn colYear; private DataColumn colMake; private DataColumn colModel; private DataColumn colPartName; private DataColumn colPartPrice; private DataColumn colItemNumber; private DataColumn colItemName; private DataColumn colItemPrice; private DataTable tblAutoParts; private DataTable tblStoreItems; private DataSet dsStoreItems; string strDirectory; string strFilename; public Inventory() { colPartNumber = new DataColumn("PartNumber", Type.GetType("System.Int32")); colYear = new DataColumn("Year", Type.GetType("System.Int32")); colMake = new DataColumn("Make", Type.GetType("System.String")); colModel = new DataColumn("Model", Type.GetType("System.String")); colPartName = new DataColumn("PartName", Type.GetType("System.String")); colPartPrice = new DataColumn("PartPrice", Type.GetType("System.Double")); tblAutoParts = new DataTable("AutoPart"); tblAutoParts.Columns.Add(colPartNumber); tblAutoParts.Columns.Add(colYear); tblAutoParts.Columns.Add(colMake); tblAutoParts.Columns.Add(colModel); tblAutoParts.Columns.Add(colPartName); tblAutoParts.Columns.Add(colPartPrice); tblStoreItems = new DataTable("StoreItem"); colItemNumber = new DataColumn("ItemNumber", Type.GetType("System.Int32")); colItemName = new DataColumn("ItemName", Type.GetType("System.String")); colItemPrice = new DataColumn("ItemPrice", Type.GetType("System.Double")); tblStoreItems .Columns.Add(colItemNumber); tblStoreItems.Columns.Add(colItemName); tblStoreItems.Columns.Add(colItemPrice); dsStoreItems = new DataSet("StoreItems"); dsStoreItems.Tables.Add(tblAutoParts); dsStoreItems.Tables.Add(tblStoreItems); strDirectory = @"C:\College Park Auto Parts"; strFilename = strDirectory + "\\" + "StoreItems.xml"; DirectoryInfo dirInfo = new DirectoryInfo(strDirectory); if (!dirInfo.Exists) dirInfo.Create(); } public void CreateStoreItem() { int year = 1960; int iPartNumber = 0, iItemNumber = 0; double unitPrice = 0.00D; string strMake = "Unknown", strModel = "Unknown", strPartName = "N/A", strItemName = "N/A"; int typeOfItem = 0; char ansAdd = 'n'; if (File.Exists(strFilename)) dsStoreItems.ReadXml(strFilename); do { try { Console.WriteLine("What type of item do you want to add"); Console.WriteLine("1. An auto part (for a car or an engine)"); Console.WriteLine("2. Another type of item, " + "not for a specific car"); Console.WriteLine("0. Stop"); Console.WriteLine("Enter the following pieces of information"); Console.Write("Your Choice: "); typeOfItem = int.Parse(Console.ReadLine()); if (typeOfItem == 1) { Random rndPartNumber = new Random(); iPartNumber = rndPartNumber.Next(100000, 999999); try { Console.Write("Car Year: "); year = int.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Invalid year"); } Console.Write("Make (or None if N/A): "); strMake = Console.ReadLine(); Console.Write("Model (or None if N/A): "); strModel = Console.ReadLine(); Console.Write("Part Name: "); strPartName = Console.ReadLine(); try { Console.Write("Unit Price: "); unitPrice = double.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Invalid unit price"); } Console.WriteLine("\nHere is a summary of " + "the part to be added"); Console.WriteLine("--------------------------------"); Console.WriteLine("Part Number: {0}", iPartNumber); Console.WriteLine("Year: {0}", year); Console.WriteLine("Make: {0}", strMake); Console.WriteLine("Model: {0}", strModel); Console.WriteLine("Part Name: {0}", strPartName); Console.WriteLine("Unit Price: {0:C}", unitPrice); Console.WriteLine("--------------------------------"); Console.Write("Are you ready to add it " + "to the database (y/n)? "); ansAdd = char.Parse(Console.ReadLine()); if ((ansAdd == 'y') || (ansAdd == 'Y')) { DataRow part = tblAutoParts.NewRow(); part["PartNumber"] = iPartNumber; part["Year"] = year; part["Make"] = strMake; part["Model"] = strModel; part["PartName"] = strPartName; part["PartPrice"] = unitPrice; tblAutoParts.Rows.Add(part); dsStoreItems.WriteXml(strFilename); } else Console.WriteLine("The part will not be " + "added to the database"); } else if (typeOfItem == 2) { Random rndPartNumber = new Random(); iItemNumber = rndPartNumber.Next(100000, 999999); Console.Write("Item/Description: "); strItemName = Console.ReadLine(); try { Console.Write("Unit Price: "); unitPrice = double.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Invalid unit price"); } Console.WriteLine("\nHere is a summary of" + " the part to be added"); Console.WriteLine("--------------------------------"); Console.WriteLine("Item Number: {0}", iItemNumber); Console.WriteLine("Name/Descr: {0}", strItemName); Console.WriteLine("Unit Price: {0:C}", unitPrice); Console.WriteLine("--------------------------------"); Console.Write("Are you ready to add it " + "to the database (y/n)? "); ansAdd = char.Parse(Console.ReadLine()); if ((ansAdd == 'y') || (ansAdd == 'Y')) { DataRow item = tblStoreItems.NewRow(); item["ItemNumber"] = iItemNumber; item["ItemName"] = strItemName; item["ItemPrice"] = unitPrice; tblStoreItems.Rows.Add(item); dsStoreItems.WriteXml(strFilename); } else Console.WriteLine("The part will not be " + "added to the database"); } } catch (FormatException) { Console.WriteLine("Invalid Menu Selection"); } } while (typeOfItem == 1 || typeOfItem == 2); } public void ShowInventory() { } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace cpap4 { public class Program { static int Main(string[] args) { Inventory item = new Inventory(); item.CreateStoreItem(); Console.WriteLine(); return 0; } } }
What type of item do you want to add 1. An auto part (for a car or an engine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 1 Car Year: 2005 Make (or None if N/A): Acura Model (or None if N/A): NSX 3.0L V6 Part Name: Oil Filter Unit Price: 8.85 Here is a summary of the part to be added -------------------------------- Part Number: 421960 Year: 2005 Make: Acura Model: NSX 3.0L V6 Part Name: Oil Filter Unit Price: $8.85 -------------------------------- Are you ready to add it to the database (y/n)? y What type of item do you want to add 1. An auto part (for a car or an engine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 1 Car Year: 2002 Make (or None if N/A): Audi Model (or None if N/A): Quattro 1.8L Turbo Part Name: Clutch Release Bearing Unit Price: 55.50 Here is a summary of the part to be added -------------------------------- Part Number: 234374 Year: 2002 Make: Audi Model: Quattro 1.8L Turbo Part Name: Clash Bear Unit Price: $55.50 -------------------------------- Are you ready to add it to the database (y/n)? y What type of item do you want to add 1. An auto part (for a car or an engine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 1 Car Year: BMW Invalid year Make (or None if N/A): BMW Model (or None if N/A): 325I 2.5L L6 Part Name: Ignition Coil Unit Price: 60.85 Here is a summary of the part to be added -------------------------------- Part Number: 899611 Year: 2002 Make: BMW Model: 325i 2.5L L6 Part Name: Ignition Coil Unit Price: $60.85 -------------------------------- Are you ready to add it to the database (y/n)? n The part will not be added to the database What type of item do you want to add 1. An auto part (for a car or an enfine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 1 Car Year: 2002 Make (or None if N/A): BMW Model (or None if N/A): 325i 2.5L L6 Part Name: Ignition Coil Unit Price: 60.85 Here is a summary of the part to be added -------------------------------- Part Number: 761840 Year: 2002 Make: BMW Model: 325I 2.5L L6 Part Name: Ignition Coil Unit Price: $60.85 -------------------------------- Are you ready to add it to the database (y/n)? Y What type of item do you want to add 1. An auto part (for a car or an engine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 2 Item/Description: Soda 2L Bottle Unit Price: 1.75 Here is a summary of the part to be added -------------------------------- Item Number: 894761 Name/Descr: Soda 2L Bottle Unit Price: $1.75 -------------------------------- Are you ready to add it to the database (y/n)? Y What type of item do you want to add 1. An auto part (for a car or an engine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 2 Item/Description: 2 Unit Price: 3 Here is a summary of the part to be added -------------------------------- Item Number: 722464 Name/Descr: 2 Unit Price: $3.00 -------------------------------- Are you ready to add it to the database (y/n)? n The part will not be added to the database What type of item do you want to add 1. An auto part (for a car or an engine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 2 Item/Description: STP Gas Treatment 3-Pack Unit Price: 2.99 Here is a summary of the part to be added -------------------------------- Item Number: 126066 Name/Descr: STP Gas Treatment 3-Pack Unit Price: $2.99 -------------------------------- Are you ready to add it to the database (y/n)? Y What type of item do you want to add 1. An auto part (for a car or an enfine) 2. Another type of item, not for a specific car 0. Stop Enter the following pieces of information Your Choice: 0 Press any key to continue . . .
Opening a Data Set |
To open the data saved from a list, the DataSet class provides the ReadXml() method that is overloaded with various versions. One of the versions of this method uses the following syntax:
public XmlReadMode ReadXml(string fileName);
This method takes as argument the name of an existing XML file or its path. The method opens the file and provides the XML formatting as it was done when the file was saved. Here is an example of calling this method:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { static DataSet dsVideos; static DataColumn colShelfNumber; static DataColumn colTitle; static DataColumn colDirector; static DataColumn colLength; static DataColumn colYear; static DataColumn colRating; static DataTable tblVideos; static string strDirectory; static void CreateCollection() { dsVideos = new DataSet("Videos"); tblVideos = new DataTable("Video"); colShelfNumber = new DataColumn("ShelfNumber", Type.GetType("System.String")); tblVideos.Columns.Add(colShelfNumber); colTitle = new DataColumn("Title", Type.GetType("System.String")); tblVideos.Columns.Add(colTitle); colDirector = new DataColumn("Director", Type.GetType("System.String")); tblVideos.Columns.Add(colDirector); colLength = new DataColumn("Length", Type.GetType("System.String")); tblVideos.Columns.Add(colLength); colYear = new DataColumn("Year", Type.GetType("System.Int16")); tblVideos.Columns.Add(colYear); colRating = new DataColumn("Rating", Type.GetType("System.String")); tblVideos.Columns.Add(colRating); dsVideos.Tables.Add(tblVideos); strDirectory = @"C:\Video Collection"; Directory.CreateDirectory(strDirectory); } static void CreateRecord() { if (File.Exists(strDirectory + @"\videos.xml")) dsVideos.ReadXml(strDirectory + @"\videos.xml"); object[] arrVideo = { "FD-205", "Her Alibi", "Bruce Beresford", "94 Minute", 1989, "PG-13" }; DataRow rowVideo = tblVideos.NewRow(); rowVideo.ItemArray = arrVideo; tblVideos.Rows.Add(rowVideo); dsVideos.WriteXml(strDirectory + @"\videos.xml"); } static int Main(string[] args) { CreateCollection(); CreateRecord(); return 0; } } }
Although this method can read any XML file, if you use it to open a file that was saved by someone else or another application and you want to use it in your application, you should be familiar with the names of its nodes. If it contains names that are not "registered" or recognized by your DataSet object, the lists that compose your application may not be able to read it, not because the list was not formatted right, but because the lists of your application would be holding different names.
If the file was saved using a Stream-based class, you can pass a stream to the method based on the following syntax:
public XmlReadMode ReadXml(Stream stream);
In the same way, the method provides an equivalent version for the TextWriter and the XmlWriter versions:
public XmlReadMode ReadXml(TextReader reader); public XmlReadMode ReadXml(XmlReader reader);
To use one of these versions, you must first define a TextWriter or an XmlReader type of variable.
When retrieving the content of the XML file, if you want it delivered as text, call the DataSet.GetXml() method. Its syntax is:
public string GetXml();
As you can see, this method returns a String string.
Once a file has been opened, you can explore its content. The most obvious operation related to opening a data set consists of viewing its records.
Committing or Rejecting Changes to a List |
When a user has created a record, the data set that holds the information is considered to have been modified because, obviously, it doesn't have the same information or the same records it had when the application was launched. You, as the programmer, have the option of accepting the changes or rejecting them. To accept the changes, call the DataSet.AcceptChanges() method. Its syntax is:
public void AcceptChanges();
If you don't want the changes to take effect, you can reject them by calling the DataSet.RejectChanges() method. Its syntax is:
public virtual void RejectChanges();
This method can be called to dismiss whatever changes where made on the records of the list(s).
Locating Records and Their Values |
Locating a Record |
Before performing any operation on a record, you must be able to locate it. That is, you must be able to identify a record among the various records of a table. To locate a record in the DataTable.Rows collection, the DataRowCollection class has an indexed property that is defined as follows:
public DataRow this[int index] {get;}
The records of a table are stored in a list (called the DataRowCollection). The first record has an index of 0. The second record has an index of 1, and so on. Here is an example of using it to retrieve the information stored in a record:
DataRow row = this.dtVideos.Rows[2];
When you pass an index to this property, the compiler would check whether the record exists. If a record with that index exists, its DataRow value is produced.
In the previous lessons, we learned how to locate a column using the foreach loop to visit the members of a DataColumnCollection collection. Like the DataColumnCollection class, the DataRowCollection class implements the GetEnumerator() method of the IEnumerable interface. This means that you can apply the foreach loop to its collection of records to visit each collection. As mentioned already, to access a record, you can pass its index to the indexed property of the DataRowCollection, which produces a DataRow object. Using these concepts, you can access the values of a table. Here is an example:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void ShowVideos() { dsVideos.ReadXml(strDirectory + @"\videos.xml"); Console.WriteLine("Video Collection"); Console.WriteLine("================================"); foreach (DataRow row in tblVideos.Rows) { foreach (DataColumn col in tblVideos.Columns) { Console.WriteLine("{0}", row[col]); } Console.WriteLine("--------------------------------"); } } static int Main(string[] args) { CreateCollection(); ShowVideos(); return 0; } } }
This would produce:
Video Collection ================================ GT-682 A Few Good Men Rob Reiner 138 Minutes 1992 R -------------------------------- MM-258 Fatal Attraction Adrian Lyne 120 Minutes 1987 R -------------------------------- FD-205 Her Alibi Bruce Beresford 94 Minute 1989 PG-13 -------------------------------- Press any key to continue . . .
The DataRow class itself is equipped with an indexed property that allows you to access the value stored in a particular column. For example, you can use a for loop to visit each column by its index. Once you get to a column, you can then use the indexed property of a row to access the value stored under that column. Here are examples:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void ShowVideos() { dsVideos.ReadXml(strDirectory + @"\videos.xml"); Console.WriteLine("================================"); Console.WriteLine("Video Collection"); Console.WriteLine("================================"); for (int i = 0; i < tblVideos.Rows.Count; i++) { DataRow row = tblVideos.Rows[i]; Console.WriteLine("Shelf #: {0}", tblVideos.Rows[i]["ShelfNumber"]); Console.WriteLine("Title: {0}", tblVideos.Rows[i]["Title"]); Console.WriteLine("Director: {0}", tblVideos.Rows[i]["Director"]); Console.WriteLine("Length: {0}", tblVideos.Rows[i]["Length"]); Console.WriteLine("Year: {0}", tblVideos.Rows[i]["Year"]); Console.WriteLine("Rating: {0}", tblVideos.Rows[i]["Rating"]); Console.WriteLine("--------------------------------"); } } static int Main(string[] args) { CreateCollection(); ShowVideos(); return 0; } } }
This would produce:
================================ Video Collection ================================ Shelf #: GT-682 Title: A Few Good Men Director: Rob Reiner Length: 138 Minutes Year: 1992 Rating: R -------------------------------- Shelf #: MM-258 Title: Fatal Attraction Director: Adrian Lyne Length: 120 Minutes Year: 1987 Rating: R -------------------------------- Shelf #: FD-205 Title: Her Alibi Director: Bruce Beresford Length: 94 Minute Year: 1989 Rating: PG-13 -------------------------------- Press any key to continue . . .
When using any of these previous techniques (whether using for or foreach), if you specify an index that is either less than 0 or beyond the number of records in the table, the compiler would throw an IndexOutOfRangeException exception.
Locating a Value |
As mentioned already, a record is in fact one or a group of values from each of the columns of the table. Consider the following table:
Title | Director | Length | © Year | Rating |
A Few Good Men | Rob Reiner | 138 Minutes | 1992 | R |
The Distinguished Gentleman | Jonathan Lynn | 112 Minutes | R | |
The Lady Killers | Joel Coen & Ethan Coen | 104 Minutes | R | |
Fatal Attraction | Adrian Lyne | 120 Minutes | 1987 | R |
Her Alibi | Bruce Beresford | 94 Minutes | 1989 | PG-13 |
The Manchurian Candidate | Jonathan Demme | 129 Minutes | 2004 | R |
The "A Few Good Men" string is a value of the Title column. In the same way, 1992 is a value of the Year column. In some circumstances, you will need to locate a particular value in order to perform an operation on it. You can start by locating the record you need and return its DataRow object. To know the table that the record belongs to, access its DataRow.Table property. This property is declared as follows: public DataTable Table {get;} |
To locate the value that a record holds under a particular column, the DataRow class had an indexed property that is overloaded with various versions (actually six, but at this time we are interested in the first three only). One of the versions of this property uses the following syntax:
public object this[string columnName] {get; set;}
To use this property, pass the object name of the column in the square brackets. Here are examples:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void ShowVideos() { dsVideos.ReadXml(strDirectory + @"\videos.xml"); Console.WriteLine("================================"); Console.WriteLine("Video Collection"); Console.WriteLine("================================"); for (int i = 0; i < tblVideos.Rows.Count; i++) { DataRow row = tblVideos.Rows[i]; Console.WriteLine("Shelf #: {0}", tblVideos.Rows[i][colShelfNumber]); Console.WriteLine("Title: {0}", tblVideos.Rows[i][colTitle]); Console.WriteLine("Director: {0}", tblVideos.Rows[i][colDirector]); Console.WriteLine("Length: {0}", tblVideos.Rows[i][colLength]); Console.WriteLine("Year: {0}", tblVideos.Rows[i][colYear]); Console.WriteLine("Rating: {0}", tblVideos.Rows[i][colRating]); Console.WriteLine("--------------------------------"); } } static int Main(string[] args) { CreateCollection(); ShowVideos(); return 0; } } }
Instead of using the index of a column, you can also locate a value using the variable name of its column. To do this, you can use the following syntax of the DataRow indexed property:
public object this[DataColumn column] {get; set;}
This property expects the object name of the column passed in its square brackets. We saw earlier how to use this version of the property. Here are examples, using foreach:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void ShowVideos() { dsVideos.ReadXml(strDirectory + @"\videos.xml"); Console.WriteLine("================================"); Console.WriteLine("Video Collection"); Console.WriteLine("================================"); foreach (DataRow row in tblVideos.Rows) { Console.WriteLine("Shelf #: {0}", row["ShelfNumber"]); Console.WriteLine("Title: {0}", row["Title"]); Console.WriteLine("Director: {0}", row["Director"]); Console.WriteLine("Length: {0}", row["Length"]); Console.WriteLine("Year: {0}", row["Year"]); Console.WriteLine("Rating: {0}", row["Rating"]); Console.WriteLine("--------------------------------"); } } static int Main(string[] args) { CreateCollection(); ShowVideos(); return 0; } } }
The third option you have is to identify the column by its index. To do this, use the following syntax of the DataRow indexed property:
public object this[int columnIndex] {get; set;}
This property expects the index of the column.
Here are examples using the for loop:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void ShowVideos() { dsVideos.ReadXml(strDirectory + @"\videos.xml"); Console.WriteLine("================================"); Console.WriteLine("Video Collection"); Console.WriteLine("================================"); for (int i = 0; i < tblVideos.Rows.Count; i++) { DataRow row = tblVideos.Rows[i]; Console.WriteLine("Shelf #: {0}", tblVideos.Rows[i][0]); Console.WriteLine("Title: {0}", tblVideos.Rows[i][1]); Console.WriteLine("Director: {0}", tblVideos.Rows[i][2]); Console.WriteLine("Length: {0}", tblVideos.Rows[i][3]); Console.WriteLine("Year: {0}", tblVideos.Rows[i][4]); Console.WriteLine("Rating: {0}", tblVideos.Rows[i][5]); Console.WriteLine("--------------------------------"); } } static int Main(string[] args) { CreateCollection(); ShowVideos(); return 0; } } }
Or, here are examples using foreach:
using System; using System.IO; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static void ShowVideos() { dsVideos.ReadXml(strDirectory + @"\videos.xml"); Console.WriteLine("================================"); Console.WriteLine("Video Collection"); Console.WriteLine("================================"); foreach (DataRow row in tblVideos.Rows) { Console.WriteLine("Shelf #: {0}", row[0]); Console.WriteLine("Title: {0}", row[1]); Console.WriteLine("Director: {0}", row[2]); Console.WriteLine("Length: {0}", row[3]); Console.WriteLine("Year: {0}", row[4]); Console.WriteLine("Rating: {0}", row[5]); Console.WriteLine("--------------------------------"); } } static int Main(string[] args) { CreateCollection(); ShowVideos(); return 0; } } }
Application: Getting the Values of a Data Set |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Data; namespace cpap4 { public class Inventory { // These are the columns of the AutoParts table private DataColumn colPartNumber; private DataColumn colYear; private DataColumn colMake; private DataColumn colModel; private DataColumn colPartName; private DataColumn colPartPrice; // These are the columns of the StoreItems table private DataColumn colItemNumber; private DataColumn colItemName; private DataColumn colItemPrice; // These are the table of the CollegeParkAutoParts database private DataTable tblAutoParts; private DataTable tblStoreItems; // This is the database private DataSet dsStoreItems; // These are accessory strings string strDirectory; string strFilename; // This default constructor is used to create // the structures of the tables public Inventory() { colPartNumber = new DataColumn("PartNumber", Type.GetType("System.Int32")); colYear = new DataColumn("Year", Type.GetType("System.Int32")); colMake = new DataColumn("Make", Type.GetType("System.String")); colModel = new DataColumn("Model", Type.GetType("System.String")); colPartName = new DataColumn("PartName", Type.GetType("System.String")); colPartPrice = new DataColumn("PartPrice", Type.GetType("System.Double")); tblAutoParts = new DataTable("AutoPart"); tblAutoParts.Columns.Add(colPartNumber); tblAutoParts.Columns.Add(colYear); tblAutoParts.Columns.Add(colMake); tblAutoParts.Columns.Add(colModel); tblAutoParts.Columns.Add(colPartName); tblAutoParts.Columns.Add(colPartPrice); tblStoreItems = new DataTable("StoreItem"); colItemNumber = new DataColumn("ItemNumber", Type.GetType("System.Int32")); colItemName = new DataColumn("ItemName", Type.GetType("System.String")); colItemPrice = new DataColumn("ItemPrice", Type.GetType("System.Double")); tblStoreItems .Columns.Add(colItemNumber); tblStoreItems.Columns.Add(colItemName); tblStoreItems.Columns.Add(colItemPrice); dsStoreItems = new DataSet("StoreItems"); dsStoreItems.Tables.Add(tblAutoParts); dsStoreItems.Tables.Add(tblStoreItems); // This database will use a folder // named College Park Auto Parts // and located on the C: drive strDirectory = @"C:\College Park Auto Parts"; strFilename = strDirectory + "\\" + "StoreItems.xml"; DirectoryInfo dirInfo = new DirectoryInfo(strDirectory); // If the folder doesn't exist already, create it if (!dirInfo.Exists) dirInfo.Create(); } // This method guides the user in creating a new store item public void CreateStoreItem() { // Accessory variables int year = 1960; int iPartNumber = 0, iItemNumber = 0; double unitPrice = 0.00D; string strMake = "Unknown", strModel = "Unknown", strPartName = "N/A", strItemName = "N/A"; int typeOfItem = 0; char ansAdd = 'n'; // If the StoreItems.xml file exists already, then open it if (File.Exists(strFilename)) dsStoreItems.ReadXml(strFilename); // This do...while is used in case the user wants to // repeatedly perform these actions do { try { // Find out what the user wants to do Console.WriteLine("What type of item do you want to add"); Console.WriteLine("1. An auto part (for a car or an engine)"); Console.WriteLine("2. Another type of item, not for a specific car"); Console.WriteLine("0. Stop"); Console.WriteLine("Enter the following pieces of information"); Console.Write("Your Choice: "); typeOfItem = int.Parse(Console.ReadLine()); // The user wants to add a new car part if (typeOfItem == 1) { // Create a random number of 6 digits Random rndPartNumber = new Random(); iPartNumber = rndPartNumber.Next(100000, 999999); // Request the information about the car and the part try { Console.Write("Car Year: "); year = int.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Invalid year"); } Console.Write("Make (or None if N/A): "); strMake = Console.ReadLine(); Console.Write("Model (or None if N/A): "); strModel = Console.ReadLine(); Console.Write("Part Name: "); strPartName = Console.ReadLine(); try { Console.Write("Unit Price: "); unitPrice = double.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Invalid unit price"); } // Present the user with a summary of the part to be added // This is a safe guard for a console application so that // if the user made a mistake, he or she can dismiss it // instead of adding a part with wrong information Console.WriteLine("\nHere is a summary of the part to be added"); Console.WriteLine("--------------------------------"); Console.WriteLine("Part Number: {0}", iPartNumber); Console.WriteLine("Year: {0}", year); Console.WriteLine("Make: {0}", strMake); Console.WriteLine("Model: {0}", strModel); Console.WriteLine("Part Name: {0}", strPartName); Console.WriteLine("Unit Price: {0:C}", unitPrice); Console.WriteLine("--------------------------------"); Console.Write("Are you ready to add it to the database (y/n)? "); ansAdd = char.Parse(Console.ReadLine()); // If the user has decided to add the part to the database, // then add it if ((ansAdd == 'y') || (ansAdd == 'Y')) { DataRow part = tblAutoParts.NewRow(); part["PartNumber"] = iPartNumber; part["Year"] = year; part["Make"] = strMake; part["Model"] = strModel; part["PartName"] = strPartName; part["PartPrice"] = unitPrice; tblAutoParts.Rows.Add(part); dsStoreItems.WriteXml(strFilename); } else Console.WriteLine("The part will not be added to the database"); } // For the same logic for other store items // The items in this section can be anything else if (typeOfItem == 2) { Random rndPartNumber = new Random(); iItemNumber = rndPartNumber.Next(100000, 999999); Console.Write("Item/Description: "); strItemName = Console.ReadLine(); try { Console.Write("Unit Price: "); unitPrice = double.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Invalid unit price"); } Console.WriteLine("\nHere is a summary of the part to be added"); Console.WriteLine("--------------------------------"); Console.WriteLine("Item Number: {0}", iItemNumber); Console.WriteLine("Name/Descr: {0}", strItemName); Console.WriteLine("Unit Price: {0:C}", unitPrice); Console.WriteLine("--------------------------------"); Console.Write("Are you ready to add it to the database (y/n)? "); ansAdd = char.Parse(Console.ReadLine()); if ((ansAdd == 'y') || (ansAdd == 'Y')) { DataRow item = tblStoreItems.NewRow(); item["ItemNumber"] = iItemNumber; item["ItemName"] = strItemName; item["ItemPrice"] = unitPrice; tblStoreItems.Rows.Add(item); dsStoreItems.WriteXml(strFilename); } else Console.WriteLine("The part will not be added to the database"); } } catch (FormatException) { Console.WriteLine("Invalid Menu Selection"); } } while (typeOfItem == 1 || typeOfItem == 2); } // This method is used to display the store inventory of all items public void ShowInventory() { if (File.Exists(strFilename)) { dsStoreItems.ReadXml(strFilename); Console.WriteLine("================================"); Console.WriteLine(" College Park Auto Parts"); Console.WriteLine(" Store Inventory"); Console.WriteLine(" Car Parts"); Console.WriteLine("================================"); foreach (DataRow part in tblAutoParts.Rows) { Console.WriteLine("Part #: {0}", part["PartNumber"]); Console.WriteLine("Car Year: {0}", part["Year"]); Console.WriteLine("Make: {0}", part["Make"]); Console.WriteLine("Model: {0}", part["Model"]); Console.WriteLine("Part Name: {0}", part["PartName"]); Console.WriteLine("Unit Price: {0:C}", part["PartPrice"]); Console.WriteLine("--------------------------------"); } Console.WriteLine("================================"); Console.WriteLine(" Other Store Items"); Console.WriteLine("================================"); foreach (DataRow item in tblStoreItems.Rows) { Console.WriteLine("Item #: {0}", item["ItemNumber"]); Console.WriteLine("Name/Description: {0}", item["ItemName"]); Console.WriteLine("Unit Price: {0:C}", item["ItemPrice"]); Console.WriteLine("--------------------------------"); } } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CollegeParkAutoParts1 { class Program { static int Main(string[] args) { char choice = '0'; Inventory inv = new Inventory(); Console.WriteLine("================================"); Console.WriteLine(" College Park Auto Parts"); Console.WriteLine("================================"); do { try { Console.WriteLine("What do you want to do?"); Console.WriteLine("1. Add New Store Item"); Console.WriteLine("2. View Inventory"); Console.WriteLine("0. Quit"); Console.Write("Your Selection? "); choice = char.Parse(Console.ReadLine()); if (choice == '1') inv.CreateStoreItem(); else if (choice == '2') inv.ShowInventory(); } catch (FormatException) { Console.WriteLine("Unrecognizable Menu Selection"); } } while ((choice == '1') || (choice == '2')); Console.WriteLine(); return 0; } } }
===================================== College Park Auto Parts ===================================== What do you want to do? 1. Add New Store Item 2. View Inventory 0. Quit Your Selection? 2 ========================================== College Park Auto Parts Store Inventory Car Parts ========================================== Part #: 299693 Car Year: 2005 Make: Acura Model: NSX 3.0L V6 Part Name: Oil Filter Unit Price: $8.85 ------------------------------------------ Part #: 398747 Car Year: 2002 Make: Audi Model: Quattro 1.8L Turbo Part Name: Clash Bear Unit Price: $55.50 ------------------------------------------ Part #: 174724 Car Year: 2002 Make: BMW Model: 325i 2.5L L6 Part Name: Ignition Coil Unit Price: $60.85 ------------------------------------------ ========================================== Other Store Items ========================================== Item #: 319027 Name/Description: Soda 2L Bottle Unit Price: $1.75 ------------------------------------------ Item #: 865745 Name/Description: STP Gas Treatment 3-Pack Unit Price: $2.99 ------------------------------------------ What do you want to do? 1. Add New Store Item 2. View Inventory 0. Quit Your Selection? 0 Press any key to continue . . .
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Next |
|