Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Filtered Indexes in SQL Server 2008
Importance of Database Backups and Recovery Plan
Data Compression in SQL Server 2008
SQL Server 2008 MERGE Statement

More     
 
Latest FAQ's

ALTER TABLE SWITCH statement failed because the object '%.*ls' is not ...
ALTER TABLE SWITCH statement failed because column '%.*ls' at ordinal %d ...
ALTER TABLE SWITCH statement failed because table '%.*ls' has %d columns ...
SQL Server Reporting Server (SSRS) service is failing to start ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> asp.net / ado.net >> DataTable Enhancements in ADO.NET 2.0

DataTable Enhancements in ADO.NET 2.0

By : Joydip Kanjilal
Feb 08, 2007

ADO.NET 2.0 has added many new powerful features to the DataTable class. Some of these features include the ability to load a DataReader instance into a DataTable instance by using the Load method of the DataTable class, the DataTableReader, built-in serialization support, etc. This article highlights these new features added to the DataTable class in ADO.NET 2.0.

Support for Serialization


In ADO.NET 2.0 a DataTable instance can be serialized by itself. Hence, we do not need to wrap a Datatable instance within a DataSet instance to expose it through Web services or other technologies that require serialized data.
The RemotingFormat property of the DataTable class can be used to specify the Serialization format. The available Serialization Format options are Binary and Xml. Refer to the code example given below:

DataTable dataTable = new DataTable();
//Some code
datatable.RemotingFormat = SerializationFormat.Binary;
//Some code

Why a DataTableReader?

DataReaders are much faster than DataSets and consume less memory. However, the major drawback of using DataReaders in the earlier versions of ADO.NET was that it always required an open connection to operate, i.e., it was connection oriented. Hence we needed to explicitly close the database connections when we were done using it. With ADO.NET 2.0, a DataTableReader class has been introduced that is similar to other data readers but with one exception – it works in a disconnected mode. According to MSDN, “The DataTableReader obtains the contents of one or more DataTable objects in the form of one or more read-only, forward-only result sets. The DataTableReader works much like any other data reader, such as the SqlDataReader, except that the DataTableReader provides for iterating over rows in a DataTable. In other words, it provides for iterating over rows in a cache. The cached data can be modified while the DataTableReader is active, and the reader automatically maintains its position”.


Creating a DataTableReader

The CreateDataReader method of the DataTableReader class can be used to create a DataTableReader. The following listing shows how this is done.

public DataTableReader GetDataTableReader (string connectionString)
{
SqlConnection sqlConnection  = new SqlConnection(connectionString);
sqlConnection .Open()
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(“Select * from States”, sqlConnection);
DataTable dataTable = new DataTable ("States");
sqlDataAdapter.Fill(dataTable);
DataTableReader datatableReader = dataTable.CreateDataReader();
sqlConnection.Close();
return datatableReader;
}

The DataTableReader is a light-weight, forward-only set of data that maintains the same structure as a DataTable, i.e., it exposes the same rows and columns as the DataTable. The following listing illustrates how we can read data using a DataTableReader instance.

String connectionString = …; //Some connection string to connect to the database
DataTableReader dataTableReader = GetDataTableReader(connectionString);
Console.WriteLine(“Displaying the codes for all the states:--“);
while (dataTableReader.Read())
{
Console.WriteLine(DataTableReader[“codeID”].ToString());
}


    Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved