SIGN UP MEMBER LOGIN:    
ARTICLE

Fastest way to populate datatable from database using IDataReader

Posted by Amit Choudhary Articles | ADO.NET in C# October 04, 2011
Here you will see the fastest way to populate a datatable from database using IDataReader.
Reader Level:


Alright folks! Who knows the panic of using a SqlDataAdapter class to fill the Datatable. If you require only a single table to be filled from the database, then this post is to show you there's an alternate way to get a DataTable. That alternate way is returning a DataReader from the command object after executing the command.

Well I always thought of a DataReader when it comes to performance but I can't forget to keep the data in memory and also keep in mind that the DataReader works in connected mode. So I came up with a mixed approach; reading the fetched the data from a DataReader and then put the data into a DataTable. The next thing was to make it reusable so finally I did some juggling with the code and came up with the code below that can convert your DataReader object to a DataTable. 


I always use IDataReader to receive the object returned from the Command.ExecuteReader() method.

You can get the schema of the Table a by method that is available through the IDataReader interface GetSchemaTable(). I'll use this method to get the schema of the table that IDataReader is holding. Then I create a new DataTable using the data we received from the schema and start populating it by using the data in the DataReader. See the below snippet; I've put all the ideas together and created this method GetDataTableFromDataReader().

public DataTable GetDataTableFromDataReader(IDataReader dataReader)
{
    
DataTable schemaTable = dataReader.GetSchemaTable();
    
DataTable resultTable = new DataTable();

    
foreach (DataRow dataRow in schemaTable.Rows)
    {
        
DataColumn dataColumn = new DataColumn();
        dataColumn.ColumnName = dataRow[
"ColumnName"].ToString();
        dataColumn.DataType = 
Type.GetType(dataRow["DataType"].ToString());
        dataColumn.ReadOnly = (
bool)dataRow["IsReadOnly"];
        dataColumn.AutoIncrement = (
bool)dataRow["IsAutoIncrement"];
        dataColumn.Unique = (
bool)dataRow["IsUnique"];

        resultTable.Columns.Add(dataColumn);
    }

    
while (dataReader.Read())
    {
        
DataRow dataRow = resultTable.NewRow();
        
for (int i = 0; i < resultTable.Columns.Count - 1; i++)
        {
            dataRow[i] = dataReader[i];
        }
        resultTable.Rows.Add(dataRow);
    }

    
return resultTable;
}


Now you can use this method to fetch the data in the DataReader and then get the DataTable from it. Its really fast I'm telling you! Yeah people can ask me about the complexity of creating another table's schema and looping through the records, but this method is still worth using  over the SqlDataAdapter class.

Let me go though a small demo over this. Let's create a DB connection and use this method and the Adapter method to get the Performance records.

public class DAL
{
    
static SqlConnection connection;
    
public DAL()
    {
        connection = 
new SqlConnection();
        connection.ConnectionString = System.Configuration.
ConfigurationSettings.AppSettings["myConnectionString"];
    }

    
public DataTable GetDataTableByAdapter(string query)
    {
        connection.Open();
        
DateTime start = DateTime.Now;
        
DataTable dt = new DataTable();
        
new SqlDataAdapter(query, connection).Fill(dt);
        
TimeSpan ts = DateTime.Now.Subtract(start);
        System.Diagnostics.
Trace.Write("Time Elapsed in Adapter: "+ts.TotalMilliseconds);
        connection.Close();
        
return dt;
    }

    
public DataTable GetDataTableFast(string query)
    {
        connection.Open();
        
DateTime start = DateTime.Now;
        IDataReader rdr = 
new SqlCommand(query, connection).ExecuteReader();
        
DataTable resultTable = GetDataTableFromDataReader(rdr);
        
TimeSpan ts = DateTime.Now.Subtract(start);
        System.Diagnostics.
Trace.Write("Time Elapsed in Custom : " + ts.TotalMilliseconds);
        connection.Close();
        
return resultTable;
    }
}

Now lets call this method in a console application and then we'll go through the Trace where the code will be writing the elapsed time in individual operation.

static void Main(string[] args)
        {
            
DAL dal = Singleton<DAL>.Instance;
            
string query = "Select * from Production.Product";
            
            
DataTable dt1 = dal.GetDataTableFast(query);
            
DataTable dt = dal.GetDataTableByAdapter(query);
            System.
Console.Read();
        }


Now Run the code and let's have a look at figures. I ran this program 2 times just to make sure counts are correct

First Run - Time Elapsed in Adapter: 93.0053 Time Elapsed in Custom : 13.0008

Second Run - Time Elapsed in Adapter: 129.0074 Time Elapsed in Custom: 14.0008

So I'm sure you must be impressed with the performance here. Moreover you can make it faster by using the Parallel.For() and Parallel.Foreach() loop if you are working on .NET 4.0 and have a multicore processor.

Get Smarter. Get Faster!! Nobody wants to wait.

Login to add your contents and source code to this article
share this article :
post comment
 

It was Around 500 + records against the Product table of AdventureWorks DB. Well if you want I'll run it against the large records and will update the stats.

Posted by Amit Choudhary Oct 07, 2011

Your test was for how many number of records?

Posted by Javeed M Shaikh Oct 06, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor