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 code to use them.
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.