![]() |
Saving the Records of a Table |
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.
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.
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.
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). |
|
|
||
| Previous | Copyright © 2006 FunctionX, Inc. | Next |
|
|
||