Chapter 5. Copying and Transferring Data

Chapter 5. Copying and Transferring Data


Recipe  5.1.   Copying Rows from One DataTable to Another

Recipe  5.2.   Copying Tables from One DataSet to Another

Recipe  5.3.   Converting a DataReader to a DataSet

Recipe  5.4.   Serializing Data

Recipe  5.5.   Deserializing Data

Recipe  5.6.   Merging Data

Recipe  5.7.   Transmitting a DataSet Securely

Recipe  5.8.   Transferring Login Credentials Securely

Recipe  5.9.   Loading an ADO Recordset into a DataSet

Recipe  5.10.   Converting a DataSet to an ADO Recordset

Recipe  5.11.   Exporting the Results of a Query as a String

Recipe  5.12.   Exporting the Results of a Query to an Array


This chapter focuses on copying data between ADO.NET classes and between ADO and ADO.NET classes, serializing and deserializing data, and encrypting data and login credentials to build secure applications.

Copying rows from one DataTable to another and copying tables from one DataSet to another are operations performed frequently in data-centric applications. Recipe 5.1 and Recipe 5.2 show different ways to copy data and discuss the advantages, limitations, and disadvantages of the different approaches.

The DataReader provides connected forward-only, read-only access to a data stream while the DataSet provides disconnected access to data stored as an in-memory relational database. There is no built-in method to convert a DataReader to a DataSet , so Recipe 5.3 shows how it's done.

In addition, this chapter covers:

Serializing and deserializing data

Serialization allows data conversion to a format that can be persisted or transported. The .NET framework supports serialization and deserialization with the System.Runtime.Serialization namespace. Support for binary, XML, and SOAP formats is built in and serialization can easily be customized if required. Recipe 5.4 and Recipe 5.5 show how to serialize and deserialize data.

As well as copying data, ADO.NET supports merging disconnected data in DataTable or DataSet objects into each other. Recipe 5.6 shows how merging works and how to use the arguments that control the merge operation.

Security and encryption

The .NET Framework provides extensive support for encryption in the System.Security.Cryptography namespace. The classes in the namespace are both easy and intuitive to use. You can use the cryptography classes to encrypt a disconnected data class such as a DataSet prior to transmission in a remoting or web services call. Encrypting the data allows it to be transmitted securely using HTTP as a transmission protocol. An encrypted protocol such as HTTPS is not required. Recipe 5.7 explores cryptographic algorithms, public keys, private keys, and demonstrates encryption solutions using symmetric and asymmetric keys.

ASP.NET uses the classes in the System.Web.Security namespace to implement security in web service applications. Securing authentication credentialsusually a username and passwordduring transmission and storage in a database is critical to building secure distributed applications. Recipe 5.8 demonstrates how to protect credentials using a hash algorithm with password salt.


The OLE DB DataAdapter supports importing ADO Recordset or Record objects into a DataTable or DataSet . This is a one-way operation: changes made to the DataTable or DataSet can't be updated back to ADO using the data adapter. ADO.NET does not, however, provide a way to convert a DataTable or DataSet to an ADO Recordset ; therefore, Recipe 5.10 shows how to do this.

With evolution of ADO to ADO.NET, a few functions got lost along the way. Recipe 5.11 and Recipe 5.12 recreate the ADO Recordset GetString( ) and GetRows( ) methods in ADO.NET.