For Performance improvement I want to convert datatable to datareader. I can not do that through query. So is there any other way to do so?
            Asked
            
        
        
            Active
            
        
            Viewed 4.2k times
        
    4 Answers
46
            I know this is old, but the answers here seem to have missed the point of the OPs question.
DataTables have a method called CreateDataReader which will allow you to convert a DataTable to a DbDataReader object. In this case a DataTableReader.
DataTable table = new DataTable(); 
//Fill table with data 
//table = YourGetDataMethod(); 
DataTableReader reader = table.CreateDataReader();
I should point out that this will not increase performance since you should be using one or the other.
Here are some more resources on the matter:
 
    
    
        Community
        
- 1
- 1
 
    
    
        Brandon Boone
        
- 16,281
- 4
- 73
- 100
2
            
            
        For example
public DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
    DataTable datatable = new DataTable();
    DataTable schemaTable = dataReader.GetSchemaTable();
    try
    {
        foreach (DataRow myRow in schemaTable.Rows)
        {
            DataColumn myDataColumn = new DataColumn();
            myDataColumn.DataType = myRow.GetType();
            myDataColumn.ColumnName = myRow[0].ToString();
            datatable.Columns.Add(myDataColumn);
        }
        while (dataReader.Read())
        {
            DataRow myDataRow = datatable.NewRow();
            for (int i = 0; i < schemaTable.Rows.Count; i++)
            {
                myDataRow[i] = dataReader[i].ToString();
            }
            datatable.Rows.Add(myDataRow);
            myDataRow = null;
        }
        schemaTable = null;
        return datatable;
    }
    catch (Exception ex)
    {
        Error.Log(ex.ToString());
        return datatable;
    }
}
 
    
    
        L_J
        
- 2,351
- 10
- 23
- 28
 
    
    
        zhaixiaohu
        
- 82
- 3
0
            
            
        Use DataTable constructor,
DataTable table = new DataTable(); 
//Fill table with data 
DataTableReader reader = new DataTableReader(table);
Good Look!
 
    
    
        Locoucla
        
- 1
0
            
            
        public DataTable GetTable(IDataReader _reader)
{
    DataTable dataTable1 = _reader.GetSchemaTable();
    DataTable dataTable2 = new DataTable();
    string[] arrayList = new string[dataTable1.Rows.Count];
    for (int i = 0; i < dataTable1.Rows.Count; i++)
    {
        DataColumn dataColumn = new DataColumn();
        if (!dataTable2.Columns.Contains(dataTable1.Rows[i]["ColumnName "].ToString()))
        {
            dataColumn.ColumnName = dataTable1.Rows[i]["ColumnName "].ToString();
            dataColumn.Unique = Convert.ToBoolean(dataTable1.Rows[i]["IsUnique "]);
            dataColumn.AllowDBNull = Convert.ToBoolean(dataTable1.Rows[i]["AllowDBNull "]);
            dataColumn.ReadOnly = Convert.ToBoolean(dataTable1.Rows[i]["IsReadOnly "]);
            dataColumn.DataType = (Type)dataTable1.Rows[i]["DataType "];
            arrayList[i] = dataColumn.ColumnName;
            dataTable2.Columns.Add(dataColumn);
        }
    }
    dataTable2.BeginLoadData();
    while (_reader.Read())
    {
        DataRow dataRow = dataTable2.NewRow();
        for (int j = 0; j < arrayList.Length; j++)
        {
            dataRow[arrayList[j]] = _reader[arrayList[j]];
        }
        dataTable2.Rows.Add(dataRow);
    }
    _reader.Close();
    dataTable2.EndLoadData();
    return dataTable2;
}
 
    
    
        L_J
        
- 2,351
- 10
- 23
- 28
 
    
    
        zhaixiaohu
        
- 82
- 3
- 
                    FYI, DataTable.Loan(DataReader) does this and much more. Especially, when the reader was created with KeyInfo. – AMissico Apr 11 '13 at 18:09
 
    