Something NEW?.....Naaa...Not much.

↑ Grab this Headline Animator

Tuesday 6 November 2007

Lightweight Dataset

ADO.NET 2.0 offers us a unique feature through which we can make a real lightweight DataSet. This is very important and much neglected development practice to overlook the performance part. So when you have huge data and you are getting multiple hit this small tune may help you a lot.
When we pass the data through network, the bigger stream size can create problem. Let us create a text file to check the size,

DataSet dsOrders = new DataSet();

using (SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Orders", CONN_STR))
{
da.Fill(dsOrders);
gridOrders.DataSource = dsOrders;
gridOrders.DataMember = dsOrders.Tables[0].TableName;
}

//Setting the Property to make it lightweight
dsOrders.RemotingFormat = SerializationFormat.Binary;

BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = new FileStream(@"C:\DataSet_2.txt", FileMode.OpenOrCreate))
{
bf.Serialize(fs, dsOrders);
}

If you do not set the SerializationFormat.Binary then the default setting is SerializationFormat.Xml. This is huge compared to Binary.

Size of the generated file, is

Xml : 434 KB
Binary : 139 KB

How's this?

No comments: