Populer Topics

Data Reader

We can use the datareader object to create a readonly, forword only string of data.

This an efficient method for accessing data. We only need to read through once. We can improve application performance by using this object because it holds only a single row of a data at a time in memory in instance of catching the entire set of objects record sql data reader class provides following.

Property Description
HasRow

It return or false weather data reader contain row or not. We should check before read the data.

FieldCount

This will return no of the field content in datareader class is close. This give us true or false weather datareader is close alter reading or not.

 

using System;
using System.Data.SqlClient;
using System.Data;

namespace Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection ConWithString = new SqlConnection(@"Data Source=.\MSSQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");

            ConWithString.Open();
            SqlCommand cmd = new SqlCommand("select * from Info", ConWithString);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Response.Write("Number=" + dr["sno"].ToString());
                    Response.Write("Name=" + dr["nm"].ToString());
                    Response.Write("City=" + dr["city"].ToString());
                }
                dr.Close();
            }
            ConWithString.Close();
        }
    }
}
Method Description
Read()

One of the important method which allow us to read data. This method moves datareader pointer to next record & return true or false weather row is exits or not.

Close()

It is used to close datareader pointer which is connect to database table.

isDbNull() This method has one columns index parameter it return true or false about the columns data it specialize column data is null it return true otherwise it return false.