Raymond Lewallen

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
3 Useful Changes to DataTable and DataSet in .Net 2.0

The DataSet and DataTable objects have great improvements in many areas.  The DataRowCollection backing object changing from an ArrayList to a Red-Black tree is a GREAT one!  Some, like what I just mentioned with the DataRowCollection, you can’t do anything about, the enhancement is just there.  Below I have noted what may be the three most often used new features of the DataTable and DataSet objects, and you will have to change your existing 1.1 code to use them in 2.0.

DataSet and DataTable Load Methods

Somewhere floating around, I have some code I’ve always hated.  It’s used to fill a datatable with the data from a datareader.  In 2.0, both DataSet and DataTable have new Load methods which can be used to fill a dataset or datatable with the data from a datareader.  Now we can just do like the following:

DataTable.Load

Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader()

Dim dt As New System.Data.DataTable

dt.Load(dr)

There are 3 LoadOption enums to be aware that you can use as the second parameter of the DataTable.Load method.

  • LoadOption.OverwriteRow – The incoming values for this row will be written to both the current value and the original value versions of the data for each column.
  • LoadOption.PreserveCurrentChanges (default) – The incoming values for this row will be written to the original value version of each column. The current version of the data in each column will not be changed.
  • LoadOption.Upsert – The incoming values for this row will be written to the current version of each column. The original version of each column's data will not be changed.

DataTable.WriteXml and DataTable.ReadXml

Prior to 2.0, how did you serialize to xml a datatable object?  You created a dataset and then added the datatable to it, then serialized the DataSet.    No longer do you need to do that.  The DataTable object now has its very own WriteXml and ReadXml methods.  Since DataTable now implements an IXmlSerializable interface, you can now use a datatable as a parameter of a XML Web Service.

DataSet and DataTable RemotingFormat

The new RemotingFormat property allows you a new option for setting the serialization format of a DataTable or DataSet.  The two values accepted for this property are SerializationFormat.Xml or SerializationFormat.Binary.  If you are using remoting, Binary is the option you want for improved performance and smaller data chunks.


Posted 08-09-2005 12:43 AM by Raymond Lewallen
Filed under:

[Advertisement]

Comments

Christopher Steen wrote Link Listing - August 10, 2005
on 08-10-2005 11:15 PM
"Source Server" - The best "Hidden" feature in Whidbey
Debugging [Via: Matt Pietrek ]
101 Code Samples...
Christopher Steen - Learning .NET wrote Link Listing - August 10, 2005
on 08-10-2005 11:16 PM
"Source Server" - The best "Hidden" feature in Whidbey Debugging [Via: Matt Pietrek ]
101 Code Samples...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?