ADO.NET 2.0 : Fine tune your DataSet Performance

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.

 

Assume I am working with Orders table and creating a DataSet out of it. Now this DataSet can be shared through network. When we pass the data 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

 

Namoskar!!!