Articles → ADO.NET → Load Datatable Through Datareader In Ado.Net

Load Datatable Through Datareader In Ado.Net






Software Requirement




  1. Visual studio 2003(or above) is installed on your machine.
  2. SQL Server 2000(or above) is installed on your machine

Prerequisite Knowledge




  1. Basics about SQL objects like tables.
  2. How to create tables in SQL server and how to insert data into the table?
  3. How to create a project using visual studio?
  4. Page events like page load event.
  5. Basic knowledge of ado.net objects like dataset and datatable.

Steps Of Execution




  1. Create a new table
  2. Add data in the table
  3. Create a new project
  4. Add code to load datatable though datareader



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