Most of the software applications (Windows, Web, or mobile) developed today involve some kind of data access and data manipulation including data retrieval, storage, change, translation, verification, or transportation. For an application to be scalable and interoperable, the app should be able to transport data in-memory. In-memory representation of data may include database tabel schema, its column types and relationships, and metadata. This is where ADO.NET DataSet plays a vital role.
ADO.NET DataSet is a data construct that can contain several relational rowsets, the relations that link those rowsets, and the metadata for each rowset. The DataSet also tracks which fields have changed, their new values and their original values, and can store custom information in its Extended Properties collection. The DataSet can be exported to XML or created from an XML document, thus enabling increased interoperability between applications.
When we look at the DataSet object model, we see that it is made up of three collections; Tables, Relations, and ExtendedProperties. These collections make up the relational data structure of the DataSet. The DataSet.Tables property is a DataTableCollection object, which contains zero or more DataTable objects. Each DataTable represents a table of data from the data source. Each DataTable is made p of a Columns collection and a Rows collection, which are zero or more DataColumns or DataRows, in that order.
The Relations property as a DataRelationCollection object, which contains zero or more DataRelation objects. The DataRelation objects define a parent-child relationship between two tables based on foreign key values. On the other hand, The ExtendedProperties property is a PropertyCollection object, which contains zero or more user-defined properties. The ExtendedProperties collection can be used contains zero or more user-defined properties. This property collection can be used to store custom data related to the DataSet, such as the time when the DataSet was constructed.
One of the key points to remember about the DataSet is that it doesn't care where it originated. Unlike the ADO 2.x Recordset, the DataSet doesn't track which database or XML document its data came from. In this way, the DataSet is a standalone data store that can be passed from tier to tier in an n-tiered architecture. This Article, we will continue by showing us how to use the DataSet in a .NET-based application to retrieve data, track modifications made by the user, and send the data back to the database.
Using C# .NET, we will show how to retrieve products from the Northwind database from a business services application and load the information in the presentation tier, all using .NET. We will walk through examples of how to use the DataSet to save several rows to the database at one time. The code samples will demonstrate how to send new rows, changed rows, and deleted rows in one trip to the business services tier and then on to the database by using a DataSet.
Working with .NET DataSets
Before we start, to understand the DataSet advanced techniques, we first have to recognize the DataTable. As a preview and reminder we will be discussing the DataTable class, and understanding how the DataTable works and its role in a data-driven application. Once we have worked with DataTables, we will begin using DataTables in DataSets, and building DataRelations between two DataTables, in the same way, we would have a relationship between two tables in a database. After everything, we will discuss how to cache DataSets for increased performance.
DataSet Constructors (SerializationInfo, StreamingContext)
This member supports the .NET Framework infrastructure and is not intended to be used directly from our code.
- public DataSet();
- public DataSet(string dataSetName);
- protected DataSet(SerializationInfo info, StreamingContext context);
Object Serialization and the SerializationInfo Class
Serialization is the process of converting a graph of objects, into a linear sequence of bytes. That sequence of bytes can be sent elsewhere (for example, to a remote computer) and deserialized, this means making a clone in that remote memory of the original graph of objects. The process of serialization is effortless with .NET and its release too. When any class in ActiveX DLL or ActiveX exe is created, there are five properties of the class that can be set. The last property known as persistable is what serialization in C#. In C# with common object library, every language supporting .NET architecture known and can use Serialization Feature with the help of RunTime.Serialization Namespace.
When we complete serialization in .NET, the runtime metadata 'knows' all there is to know about each object's layout in memory, its field and property definitions, this makes serialization of objects automatically, without having to write code to serialize each field.
The serialized stream might be encoded using XML, or a compact binary representation. The format is decided by the Formatter object that is called. Pluggable formatters allow the developer to serialize objects in with the two supplied formats (binary and SOAP) or can be created by developers. Serialization can take place with any stream, not just a FileStream also Serialization makes use of several classes, as follows:
Each of these components is 'Pluggable' - the programmer can provide alternatives.
If a class author wishes to take special action when objects of that class are serialized and deserialized, he can choose to implement the ISerializable interface. This allows full control. For example, the author may define his own, custom SerializationInfo, to describe parts of the object or synthesized fields that capture the object state for serialization. If a class implements ISerializable, that interface will always be called in preference to default serialization. An important note on deserialization is that we return the fields in the same order in which they are returned from Reflection. Reflection makes no guarantee that it will follow metadata ordering.
This class holds all the data needed to serialize or deserialize an object. Attention please; this class cannot be inherited.
Any public static members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread-safe. This class is used by objects with custom serialization behavior. The GetObjectData method on either ISerializable or ISerializationSurrogate populates the SerializationInfo with the name, type, and value of each piece of information it wants to serialize. During deserialization, the appropriate function can extract this information. Objects are added to the SerializationInfo at serialization time using the AddValue methods and extracted from the SerializationInfo at deserialization using the GetValue methods.
The following code example demonstrates the SerializationInfo for custom serialization and deserialization of various values.
A DataSet can represent one more database tables. A table representation in ADO.NET is a DataTable object.