Convert DataReader To DataTable

As you all know a data reader is the most efficient way for looping through the data. Performance wise a data reader is faster than any of the other way like data adapter and cell set in MDX result. So in few situations you may need to convert this data reader to a data table. Here in this post we will discuss how we can convert it. I hope you will like it.

Background

For the past few months, I have been working with Microsoft ADOMD data sources. And I have written some articles also that will describe the problems I have encountered so far. If you are new to ADOMD I strongly recommend to read my previous articles that you may find useful when you work with ADOMD data sources. You can find those article links here.

Why

You might think, why I am not using other two ways (data adapter and cell set). I will answer it here. I am handling large set of data, so when I use data adapter and cell set it was a bit slow to get the output. So, I was just checking the performance using data reader. It was fast enough when I use data reader. So we can order these three as in the following by performance.

Data reader >> Data Adapter >> Cell Set

Using the code

The following is the function that does what was explained above:

  1. #region Convert Datareader toDatatable  
  2. /// <summary>  
  3. /// Convert Datareader toDatatable  
  4. /// </summary>  
  5. /// <param name="query"></param>  
  6. /// <param name="myConnection"></param>  
  7. public DataTable ConvertDataReaderToDataTable(string query, string myConnection)  
  8. {  
  9.     AdomdConnection conn = new AdomdConnection(myConnection);  
  10.     try  
  11.     {  
  12.         try  
  13.         {  
  14.             conn.Open();  
  15.         }  
  16.         catch (Exception)  
  17.         {}  
  18.         using(AdomdCommand cmd = new AdomdCommand(query, conn))  
  19.         {  
  20.             AdomdDataReader rdr;  
  21.             cmd.CommandTimeout = connectionTimeout;  
  22.             using(AdomdDataAdapter ad = new AdomdDataAdapter(cmd))  
  23.             {  
  24.                 DataTable dtData = new DataTable("Data");  
  25.                 DataTable dtSchema = new DataTable("Schema");  
  26.                 rdr = cmd.ExecuteReader();  
  27.                 if (rdr != null)  
  28.                 {  
  29.                     dtSchema = rdr.GetSchemaTable();  
  30.                     foreach(DataRow schemarow in dtSchema.Rows)  
  31.                     {  
  32.                         dtData.Columns.Add(schemarow.ItemArray[0].ToString(), System.Type.GetType(schemarow.ItemArray[5].ToString()));  
  33.                     }  
  34.                     while (rdr.Read())  
  35.                     {  
  36.                         object[] ColArray = new object[rdr.FieldCount];  
  37.                         for (int i = 0; i < rdr.FieldCount; i++)  
  38.                         {  
  39.                             if (rdr[i] != null) ColArray[i] = rdr[i];  
  40.                         }  
  41.                         dtData.LoadDataRow(ColArray, true);  
  42.                     }  
  43.                     rdr.Close();  
  44.                 }  
  45.                 return dtData;  
  46.             }  
  47.         }  
  48.     }  
  49.     catch (Exception)  
  50.     {  
  51.         throw;  
  52.     }  
  53.     finally  
  54.     {  
  55.         conn.Close(false);  
  56.     }  
  57. }
  58. #endregion;  
Here we are creating two data tables: dtData and dtSchema where dtData is for binding the data and dtSchema is for binding the schema.

Creating data tables:
  1. DataTable dtData = new DataTable("Data");  
  2. DataTable dtSchema = new DataTable("Schema");  
Execute data reader

Now we will execute the reader as in the following:
  1. rdr = cmd.ExecuteReader();  
Get the schema

To generate the schema, we can call the function GetSchemaTable() which is a part of your data reader.

To use this function, you must include Microsoft.AnalysisServices.AdomdClient.dll.

Adding the columns headers to the dtData

The next thing is to add the header names to the data table dtData.
  1. foreach(DataRow schemarow in dtSchema.Rows)  
  2. {  
  3.     dtData.Columns.Add(schemarow.ItemArray[0].ToString(), System.Type.GetType(schemarow.ItemArray[5].ToString()));  
  4. }  
Load the data to dtData

To load the data, we are using a function LoadDataRow() that expects an object as parameter. This will find and update a specific row, if the matching row is not found, it will create another row with the given values.

values

Convert data reader to data table
  1. object[] ColArray = new object[rdr.FieldCount];  
  2. for (int i = 0; i < rdr.FieldCount; i++)  
  3. {  
  4.     if (rdr[i] != null) ColArray[i] = rdr[i];  
  5. }  
  6. dtData.LoadDataRow(ColArray, true);  

Conclusion

Have you ever gone through this kind of requirement. Did I miss anything that you may think which is needed?. I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C-Sharp Corner, Stack Overflow, ASP.NET Forums or Code Project instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I am able to.

Please see this article in my blog here.


Similar Articles