CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Professional Learner

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.



Comments

Christopher Steen said:

"Source Server" - The best "Hidden" feature in Whidbey
Debugging [Via: Matt Pietrek ]
101 Code Samples...
# August 10, 2005 11:15 PM

Christopher Steen - Learning .NET said:

"Source Server" - The best "Hidden" feature in Whidbey Debugging [Via: Matt Pietrek ]
101 Code Samples...
# August 10, 2005 11:16 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!

Our Sponsors