New Dataset features in ADO.Net 2.0
ADO.NET class libraries are used for accessing data from a wide range of data sources. One of the best features in ADO.Net is DataSet.Even though it was an exciting feature, performance was a concern with DataSet. Lets look some changes made to DataSet in ADO.Net in this article.
ADO.NET class libraries are used for accessing data from a wide range of data sources. One of the best features in ADO.Net is DataSet. Even though it was an exciting feature, performance was a concern with DataSet. Changes made to DataSet in ADO.Net 2.0 are mainly form performance perspective. There are several changes made to the DataSet class to achieve higher performance. These improvements are primarily because of New Indexing Engine and binary serialization option. In the later part of this article we will discuss about the changes to DataTable class which is more independent in ADO.Net 2.0 unlike pervious versions. Now DataTable supports many XML features which were only available to DataSet in the earlier versions i.e. ReadXML, ReadXMLSchema, WriteXML, Write XMLSchema etc. It can also be serialized independently like a DataSet.
DataSet are slow in general, in framework 2.0 many new enhancements are done to achieve better performance. Some of these changes are New Indexing Engine and binary serialization option. The new indexing engine performs much faster data manipulation i.e. inserts updates etc. This ensures better performance while Fill and Merge operations. //To set binary serialization
In Framework 1.1, the only remoting option available for DataSet was XML serialization which was verbose and resulted in sluggish performance. Framework 2.0 allows programmers to assign formatting option for a DataSet as well as a DataTable to binary or xml by setting the RemotingFormat property as shown below
ds.RemotingFormat = SerializationFormat.Binary;
//To set xml serialization
ds.RemotingFormat = SerializationFormat.Xml;
By default the RemotingFormat property is set to SerializationFormat.Xml. If you look closely at the DataSet class, it implements ISerializable interface. This is a simple interface with just a single method as shown below.
void GetObjectData(SerializationInfo info, StreamingContext context);
Now in Framework 2.0, while implementing the GetObjectData method, DataSet class will use the RemotingFormat Property as well as shown below. public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
SerializationFormat format1 = this.RemotingFormat;
this.SerializeDataSet(info, context, format1);
SerializeDataSet is a private method implemented by DataSet to populate the SerializationInfo memory buffer with data. private void SerializeDataSet(SerializationInfo info, StreamingContext context, SerializationFormat remotingFormat)
//Implementation goes here
Binary Serialization provides enormous improvement in terms of performance and the gain is more significant when we deal with larger dataset (with large number of rows). Following diagram illustrates the same fact.
3. New and Much Improved DataTable
DataTable was definitely one of the key classes in ADO.net APIs; however it had a limitation that it was dependent of parent DataSet class it is associated with. Whenever programmers required to work with DataTable, they had to assign the parent DataSet as well. In Framework 2.0, DataTable is an independent entity, it supports all basic XML related functionality provided by the DataSet like ReadXML, ReadXMLSchema, WriteXML, WriteXMLSchema etc. Also now DataTable can be serialized independently using the RemotingFormat property explained in Performance section.
Another new addition in framework 2.0 is the Load Method.
DataTable can be loaded using IDataReader object as described below
DataTable dt = new DataTable();
The Load method also supports other overload which accepts LoadOption as described below. The LoadOption enum has two other options i.e. PreserveChanges and Upsert. There is one more overload for Load method which also allows the programmer to specify the FillErrorEventHandler delegate to be called when error occurs while loading data.
DataTable also provides a new method for creating a DataTableReader as described below.
DataTableReader dtReader = dt.CreateDataReader();
Please note that DataTableReader class derives from DbDataReader.
public sealed class DataTableReader : DbDataReader
CreateDataReader method of DataTable makes it easy for programmers who require data not in the DataTable but in a DataReader object. Load and CreateDataReader method can be used to exchange data between DataTable and DataReader.