HOME BUILDER
Datasets - at the top of the object hierarchy
JAMES HEIN
A Dataset is an old computer term, primarily used by IBM, that refers to a collection of data. Most of us call this a file but when working with databases the term does not make sense.
Continuing in our coverage of disconnected ADO.NET classes this week, we look at the DataSet object. This object is a memory based representation of data and is the top of the hierarchy of objects that we have covered earlier, like the DataTable.
The DataSet is where schema (database structure) information is held and this can be cloned, data copied and merged with other DataSets and change management is handled. The DataSet schema can be created programmatically on the fly or retrieved from a predefined XML structure.
Along with the DataTable object there is another object at this level called the DataRelation that, not unsurprisingly, is used to join to DataTables in a DataSet.
You start this by creating your DataSet object and then creating and defining your DataTables - say Company and Employee - the same way we have done in earlier examples. With this, completed code like the following is then used.
companyData.Relations.Add("CompanyEmployee",
company.Columns("compID"), employee.Columns("compID")
Where companyData is the DataSet name, company and employee are the two DataTable names and compID is the common column on which the two tables are being joined. To join more columns, simply add more to the list.
One of the problems people have creating records is making sure that the primary keys remain unique. In some systems this is handled using an auto increment field that automatically adds one to the biggest value and uses that as the next ID.
In ADO.NET you have GUID, or global unique identifier. Using the same two tables as before and using empID and compID as the primary keys for the two tables, consider the following code.
Dim cimpID, empID as Guid
compID = Guid.New()
empID = Guid.New()
company.Rows.Add(compID, "My Company")
employee.Rows.Add(compID, empID, "Last", "First", 45600)
empID = Guid.New()
employee.Rows.Add(compID, empID, "Last2", "First2", 28930)
Note the use of the New() method to get the next unique identifier. This will need to be done for every new record. In this case we have added two new employees to the same company so only a new empID is required.
Now there is still an auto-number Id column you can define and use but remember that we are creating these records, in this case, as a disconnected data set. So if we did use this type of field there would be merging problems when trying to synchronise with the live data set, especially if multiple people are creating records offline.
A GUID is truly unique no matter when and where you create it. In the kinds of examples we have used, having a unique key makes it much easier to maintain the integrity of the data and also makes the joins a lot easier. If you want to know what a GUID looks like check out your registry file under HKEY - CLASSES - ROOT.
Back to the DataRelation objects for a moment. These are used to join two DataTable objects that are part of the same DataSet and should be used for parent to child or child to parent tables. This object allows you to create a relationship without the need for unique or foreign key constraints by creating its own unique constraint on the parent and the foreign key in the child table.
With the new XML support that has been added to ADO.NET, you can serialise a DataSet to XML and vice versa.
companyList.WriteXML("CompanyLIst.xml")
In this case the WritsXML method is used to convert the data in a DataSet into an xml file. This allows the transmission of the data using a wide varieties of different protocols, including the basic HTTP. Note that the default data type in this case will default to string unless you specify otherwise.
Another new term is DiffGram. This is an XML document that holds all data from your DataSet object along with all the original DataRow info. It is used where you occasionally connect to a database to syunchronise. The original data is then used to apply the changes. You create one by adding a parameter.
companyList.WriteXML(MapPath("CompanyLIst.xml")
, XmlWriteMode.DiffGram)
The result is a before and after representation of the data and changes made. Enough on disconnected classes: next week we get connected.
Email : jamesh@inet.co.th
Bangkok Post
Last Updated : Sunday April 08, 2007
No comments:
Post a Comment