VisualBuilder
  Home > Aspnet > Tutorials > Execute Reader: Fetching Records from Database - ASP.NET Database Tutorial
Tell a friend
Link to us
Total Members
      Members: 84657
     
Sitemap Forum Chat
Home
ASP.NET Database Tutorial Home
1 . Introduction to Asp.Net 2.0
2 . Introduction to ADO.Net
3 . Connecting Access Database with AccessDataSource and GridView Control
4 . Accessing SQL SERVER Database [Using SqlDataSource]
5 . Storing Connection String in Web.Config File
6 . Inserting Records in Database
7 . Updating Records in Database
8 . Updating Records in Database
9 . Deleting Records from Database
10 . Execute Scalar: Getting Single Value from the Database
11 . Execute Reader: Fetching Records from Database
12 . Data Adapter
13 . Data Adapter and Database Connection
14 . Performing Batch Updates: Using SqlCommandBuilder
15 . Improving Performance using Stored Procedure
16 . Filtering and Sorting Data Using DataView/DataTable
17 . Data Binding and Data Synchronization
18 . SQL Transaction and Locking
19 . SQL Joins and .Net
20 . Automatically Generation of SQL Statement
 
Aspnet Group Home
Aspnet Discussion (10)
Aspnet Members (2382)
Aspnet Resources
Aspnet Source Code (388)
Aspnet Articles (1)
Aspnet Blogs
Aspnet Jobs
Aspnet Components (201)
Aspnet Books
Aspnet Websites (21)
Aspnet News (105)
Aspnet Q & A (114)
- Aspnet Ask Question
- Aspnet Questions
- Aspnet Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (434 )
Source Code (3275 )
Articles (11 )
Components (1595 )
News (888 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Aspnet database Tutorial
 Execute Reader: Fetching Records from Database
  << Prev: Execute Scalar: Getting Single Value from the Database Next: Data Adapter >>

SqlDataReader is used to retrieve data from the database. A SqlDataReader is also called as fast-forward firehouse-like streams of data. You can read from SqlDataReader objects in a forward-only sequential manner.  Once you've read some data, you must save it because you will not be able to go back and read it again.


The forward only design of the SqlDataReader is what enables it to be fast.  It doesn't have overhead associated with traversing the data or writing it back to the data source.


 


Creating SqlDataReader Object


 


The SqlDataReader object doesn't require any memory space to be initialized to it rather we have to assign the reference of the command object executereader method.


SqlDataReader rdr = cmd.ExecuteReader ();


 


The following example will show the use of SqlDataReader object to read the data from the database.


 


<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 


<html xmlns ="http://www.w3.org/1999/xhtml" >


<head runat ="server">


</head >


<body >


<form id ="form1" runat ="server">


<div><strong> ExecuteReader < br />


<br />


<br />


<asp : Button ID ="btnExecuteReader" runat ="server"


Text ="Execute Reader- Flat"


OnClick ="btnExecuteReader_Click" />


<br />


<br />


<asp:GridView ID ="grdVEmployees" runat ="server" AutoGenerateColumns ="False">


<Columns>


<asp:BoundField DataField ="FirstName" HeaderText ="First Name" />


<asp:BoundField DataField ="LastName" HeaderText ="Last Name"/>


<asp:BoundField DataField ="Title" HeaderText ="Title" />


<asp:BoundField DataField ="Address" HeaderText ="Address" />


</Columns>


</asp:GridView>


<br />


<br />


<asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="Execute Reader- Filling GridView" Width ="206px" />< br />


</strong>


</div>


</form>


</body>


</html>


 


 


ExecuteScalar.aspx.cs    


using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;


using System.Data.SqlClient;


 


public partial class ExecuteReader : System.Web.UI. Page


{


  protected void Page_Load( object sender, EventArgs e)


    {


    }


  protected void btnExecuteReader_Click( object sender, EventArgs e)


    { //Fetching records form Database: Using the ExecuteReader..


    string strConn = "Data Source=localhost;Initial Catalog= Northwind; user     Id=sa;Password =test" ;


    string sqlQuery = "select * from categories" ;


    SqlConnection conn = new SqlConnection (strConn);


    SqlCommand cmd = new SqlCommand ();


    SqlDataReader reader;


    cmd.Connection = conn;


    cmd.CommandText = sqlQuery;


    cmd.CommandType = CommandType .Text;


    conn.Open();


    reader = cmd.ExecuteReader();


    Response.Write( "<table border=1>" );


    Response.Write( "<tr>" );


    Response.Write( "<td>" );


    Response.Write( "<b>Category Name</b>" );


    Response.Write( "</td>" );


    Response.Write( "<td>" );


    Response.Write( "<b>Description</b>" );


    Response.Write( "</td>" );


    Response.Write( "</tr>" );


    while (reader.Read()) {


        Response.Write( "<tr>" );


        Response.Write( "<td>" );


        Response.Write(reader[ "CategoryName" ].ToString());


        Response.Write( "</td>" );


        Response.Write( "<td>" );


        Response.Write(reader[ "Description" ].ToString());


        Response.Write( "</td>" );


        Response.Write( "</tr>" );


    }


    Response.Write( "</table>" );


    reader.Close();


    conn.Close();


    }


  protected void Button1_Click( object sender, EventArgs e)


    { // Execute Reader: Act as a Data Source to GridView Control


    string strConn = "Data Source=localhost;Initial Catalog= Northwind; user Id=sa;Password =test" ;


    string sqlQuery = "select * from Employees" ;


    SqlConnection conn = new SqlConnection (strConn);


    SqlCommand cmd = new SqlCommand ();


    SqlDataReader reader;


    cmd.Connection = conn;


    cmd.CommandText = sqlQuery;


    cmd.CommandType = CommandType .Text;


    conn.Open();


    reader = cmd.ExecuteReader();


    reader.Read();


    grdVEmployees.DataSource = reader;


    grdVEmployees.DataBind();


    conn.Close();


    }


}


  Output:      

  << Prev: Execute Scalar: Getting Single Value from the Database Next: Data Adapter >>
Aspnet Database Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
desktop fax service
Video Surveillance
Hosted Exchange, SDSL
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising