Articles → ADO.NET → Load datatable through datareader in ado.net

Load datatable through datareader in ado.net






















Software requirement









Prerequisite knowledge















Steps of execution















Create a new table




Picture showing the design view of the table in sql server

Click to Enlarge


Add data in the table




Picture showing the table with data in sql server

Click to Enlarge


Create a new project




Picture showing the project structure of the sample project in solution explorer

Click to Enlarge


Add code to load datatable though datareader




using System;
using System.Data.SqlClient;
using System.Data;
public partial class _Default: System.Web.UI.Page {
	protected void Page_Load(object sender, EventArgs e) {
		using(SqlConnection oConnection = new SqlConnection(@"connection_string")) { // Open a connection
			oConnection.Open();
			using(SqlCommand cmd = new SqlCommand("Select * from Course", oConnection)) {
				SqlDataReader dr = cmd.ExecuteReader();
				DataTable dt = new DataTable();
				dt.Load(dr);
			}
		}
	}
}



Output


Picture showing the output of Load datatable through datareader in ado.net

Click to Enlarge

Posted By  -  Karan Gupta
 
Posted On  -  Thursday, April 11, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250