Describe DataReader object of ADO.NET with example.

Answer

The DataReader object is a forward-only and read only object
- It is simple and fast compare to dataset.
- It provides connection oriented environment.
- It needs explicit open and close the connection.
- DataReader object provides the read() method for reading the records. read() method returns Boolean type.
- DataReader object cannot initialize directly, you must use ExecuteReader() method to initialize this object.

Example:

using System;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
public partial class CareerRide : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e) 
        {
             if (!Page.IsPostBack)
             {
                   SqlDataReader reader;
                   SqlConnection MyConnection = new SqlConnection("Data Source=name of your datasource;Initial Catalog=Employee;Integrated Security=True"); 
                   SqlCommand cmd = new SqlCommand();
                  cmd.CommandText = "SELECT * FROM emp";
                  cmd.CommandType = CommandType.Text;
                  cmd.Connection = MyConnection; MyConnection.Open(); 
                  reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                  GridView1.DataSource = reader;
                  GridView1.DataBind();
                  cmd.Dispose(); 
                  MyConnection.Dispose(); 
            } 
      }
}

All ado.net Questions

Ask your interview questions on ado-net

Write Your comment or Questions if you want the answers on ado-net from ado-net Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---