Forums » .NET » .NET »

datareader to datatable.


Posted Date: 19 Apr 2005      Posted By:: ashokparchuri     Member Level: Silver  Member Rank: 0     Points: 2   Responses: 5

hi..

how to get datareader data to datatable...?


thanks in advance-ashok parchuri


Responses

#15906    Author: Sadha Sivam      Member Level: Gold      Member Rank: 0     Date: 19/Apr/2005   Rating: 2 out of 52 out of 5     Points: 2

Hi Ashok,

The following code will transfer the data from datareader & datatable.


'Put user code to initialize the page here
Dim dr As System.Data.SqlClient.SqlDataReader
'make the connection and pull the data into the datareader
Dim dt As New DataTable
'define the table structure
'for example if the datareader contains only 2 columns, empid and empname then
'the implementation should go like this.

dt.Columns.Add("Empid")
dt.Columns.Add("EmpName")

Dim str(1) As String ' where 1 repersent 2 columns, we need to declare an array
' of string which represents each column in the datarow

While dr.Read
str(0) = dr.Item(0)
str(1) = dr.Item(1)
dt.Rows.Add(str)
End While

'now dt will contain all the records


With regards,

Sadha Sivam S,


 
#15911    Author: Sudheer Kumar Yadav      Member Level: Bronze      Member Rank: 0     Date: 19/Apr/2005   Rating: 2 out of 52 out of 5     Points: 2

Hi,

 
#15912    Author: Sudheer Kumar Yadav      Member Level: Bronze      Member Rank: 0     Date: 19/Apr/2005   Rating: 2 out of 52 out of 5     Points: 2

Hi,




 
#15913    Author: Sudheer Kumar Yadav      Member Level: Bronze      Member Rank: 0     Date: 19/Apr/2005   Rating: 2 out of 52 out of 5     Points: 2

Hi,

You can use the following function to convert datareader to dataTable.

public DataTable GetTable(SqlDataReader sqlReader)
{

DataTable schemaTable = sqlReader.GetSchemaTable();
DataTable outputTable = new DataTable();
DataColumn dcColumn;
DataRow drRow;

for (int i = 0; i < schemaTable.Rows.Count; i ++)
{
dcColumn = new DataColumn();
if (! outputTable.Columns.Contains(schemaTable.Rows[i]["ColumnName"].ToString()))
{
dcColumn.ColumnName = schemaTable.Rows[i]["ColumnName"].ToString();
dcColumn.Unique = Convert.ToBoolean(schemaTable.Rows[i]["IsUnique"]);
dcColumn.AllowDBNull = Convert.ToBoolean(schemaTable.Rows[i]["AllowDBNull"]);
dcColumn.ReadOnly = Convert.ToBoolean(schemaTable.Rows[i]["IsReadOnly"]);
outputTable.Columns.Add(dcColumn);
}
}

while (sqlReader.Read())
{

drRow = outputTable.NewRow();
for ( int i = 0; i < sqlReader.FieldCount; i++)
{
drRow[i] = sqlReader.GetValue(i);
}
outputTable.Rows.Add(drRow);
}

return outputTable;

}

Regards,
Sudheer


 
#111038    Author: niranchanadevi rajmohan      Member Level: Bronze      Member Rank: 0     Date: 23/Feb/2007   Rating: 2 out of 52 out of 5     Points: 2

there is a simple way...

jst call the Load() method of DataTable

For eg

c#
mydatatable.load(myreader);

thanks
niranch.


 
Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.





  • Return to Discussion Forum
  • Start new thread


  • Top Contributors
    Today
      Last 7 Days
        more...

        Awards & Gifts
         
        Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India