Home

Introduction to the Data Set

 

A Set of Data

 

Introduction

A database is 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 in human memory, on a piece of paper, on a computer file, etc. 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, and so on. Because of this, before creating a list, you should first plan it by deciding what kinds of items would compose the list.

An 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:

Imports System
Imports System.xml
Imports System.data

Module Exercise

    Public Sub Main()
        Dim dsVideoCollection As DataSet = New DataSet
    End Sub

End Module	

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:

Imports System
Imports System.xml
Imports System.data

Module Exercise
    Dim dsVideoCollection As DataSet

    Public Sub Main()
        dsVideoCollection = New DataSet
    End Sub

End Module

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:

public DataSet(string dataSetName);

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 VBasic language. Here is an example:

Imports System
Imports System.xml
Imports System.data

Module Exercise
    Dim dsVideoCollection As DataSet

    Public Sub Main()
        dsVideoCollection = New DataSet("Videos")
    End Sub

End Module

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.

 

Application Data Storage

 

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

Overloads Public Sub WriteXml(ByVal fileName As String)

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:

Overloads Public Sub WriteXml(ByVal stream As Stream)

If you want the file to be formatted as text, you can use the following version of the method:

Overloads Public Sub WriteXml(ByVal writer As TextWriter)

If you prefer to use an XmlWriter variable to manage the file, use the following version of the method:

Overloads Public Sub WriteXml(ByVal writer As XmlWriter)

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:

Overloads Public Function ReadXml(ByVal fileName As String) As XmlReadMode

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:

Overloads Public Function ReadXml(ByVal stream As Stream) As XmlReadMode

In the same way, the method provides an equivalent version for the TextWriter and the XmlWriter versions:

Overloads Public Function ReadXml(ByVal reader As TextReader) As XmlReadMode
Overloads Public Function ReadXml(ByVal reader As XmlReader) As XmlReadMode

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 Function GetXml() As String

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 Sub 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 Overridable Sub RejectChanges()

This method can be called to dismiss whatever changes where made on the records of the list(s).

 

Home Copyright © 2005-2016, FunctionX Next