Articles → ADO.NET → Load Datatable Through Datareader In Ado.Net
Load Datatable Through Datareader In Ado.Net
Software Requirement
- Visual studio 2003(or above) is installed on your machine.
- SQL Server 2000(or above) is installed on your machine
Prerequisite Knowledge
- Basics about SQL objects like tables.
- How to create tables in SQL server and how to insert data into the table?
- How to create a project using visual studio?
- Page events like page load event.
- Basic knowledge of ado.net objects like dataset and datatable.
Steps Of Execution
- Create a new table
- Add data in the table
- Create a new project
- Add code to load datatable though datareader
Create A New Table
Click to Enlarge
Add Data In The Table
Click to Enlarge
Create A New Project
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
Click to Enlarge