Introduction to the Data Set


A Set of Data



A database as a list of items. The nature of the items is not particularly important because any list of any type of items constitutes a database. The idea of considering it a database is for better organization of management. This means that, traditionally, the database word suggests that the list must be formally created, either on a piece of paper or on a computer file. Any type of thing can be made into a list. This means that one list can be made of people. Another list can be made of CDs. Another list can be made of countries. Because of this, before creating a list, you should first plan it by deciding what kinds of items would compose the list.

A item that is part of a list is called datum, with the plural being data, but data is also used for singular. The group of items, or data, that makes up a list is referred to as a set of data.

Data Set Creation

To support the creation and management of any type of list, the .NET Framework provides the DataSet class, which is defined in the System::Data namespace. Therefore, to create a list, you can start by declaring a variable of type DataSet. To initialize a DataSet variable, the class is equipped with three constructors, the first of which is the default, meaning it doesn't take any argument. The DataSet default constructor allows you to declare a variable without providing further information, just to let the compiler know that you are going to create or need a list of items. Here is an example:

System::Void btnCreate_Click(System::Object *  sender, System::EventArgs *  e)
	 DataSet *dsVideoCollection = new DataSet();

If you are planning to use a DataSet object from more than one method or event, you can declare it globally, that is, in the class of a form. Here is an example:

#pragma once

namespace Application1
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	. . . No Change
	public __gc class Form1 : public System::Windows::Forms::Form
		. . . No Change

		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container * components;
		DataSet *dsVideoCollection;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
			. . . No Change

	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
			 dsVideoCollection = new DataSet;

When creating a set of data, it is not unusual that you want to give it a name. This would allow you to refer to the list later on by its formal name. To create such a set, you can use the second constructor whose syntax is:

puclic: DataSet(String *name);

This constructor takes as argument the formal name of the set. The name can be anything but it must respect the rules of names of the C++ language. Here is an example:

System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
	 dsVideoCollection = new DataSet(S"Videos");

The third constructor is not used in your applications.

The name of the DataSet variable, in this case dsVideoCollection, is a Windows name that will allow the operating system to identify the DataSet. That name is required as is the case for every variable you declare. The DataSetName name is optional but is useful in many methods as we will see. If you don't specify it, the compiler would generate a default name for the DataSet object.

Instead of programmatically declaring a DataSet variable, the Visual Studio .NET programming environment allows you to do this "visually" by clicking the DataSet button from the Data section of the Toolbox and clicking a form or another container. If you create a DataSet using this approach, you can specify its name using the DataSetName property in the Properties window. In the same way, if you have already created the DataSet object using either the default or the second constructor and you want to set or change its name, assign a string to the DataSet::DataSetName property.

Practical Learning Practical Learning: Creating a DataSet

  1. Start Microsoft Visual Studio .NET and create a new Windows Forms Application named PeopleInMyLife1
  2. In the Toolbox, click the Data button.
    To create a DataSet, click DataSet and click the form
  3. In the Add Dataset dialog box, click the Untyped Dataset radio button and click OK
  4. While the DataSet control is still selected, in the Properties window, click (Name) and type dsPeople
  5. Still in the Properties window, set the DataSetName to People


Application Data Storage



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 List

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. 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.

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 to specify 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);

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.

Opening a List

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. This method opens the file and provides the XML formatting as it was done when the file was saved. 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 must 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.

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).


Home Copyright © 2004-2010 FunctionX, Inc. Next