Monday 1 April 2013

SqlDataReader in ado.net


Hello Friends !! Today I am just going to explain about SqlDataReader which as given below :

SqlDataReader
SqlDataReader


  • SqlDataReader is the class of connected architecture in .NET framework.
  • DataReader is Connected Architecture since it keeps the connection open until all rows are fetched one by one.
  • The SqlDataReader is used to read a row of record at a time which is got using SqlCommand
  • if we want to read the next row then we cannot return back to the previous row for that we have to   use the ExecuteReader method of the SqlCommand class.
  • SqlDataReader   is a stream-based,, read-only retrieval of query results from the Data Sources  which do not update the data. The DataReader cannot be created directly from code; they can create only by calling the ExecuteReader method of a Command Object.

Syntax:

SqlDataReader dr = cmd.ExecuteReader ();

Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void search_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=.;initial catalog=search;integrated security=true");
            SqlCommand cmd = new SqlCommand("select *from find", con);
            con.Open();
         
                SqlDataReader dr = cmd.ExecuteReader();
             
                    dr.Read();
                    if (dr[0].ToString() == roll_no.Text)
                    {
                      //  roll_no.Text = dr[0].ToString();
                    full_name.Text = dr[1].ToString();
                    email_id.Text = dr[2].ToString();
                    user_address.Text = dr[3].ToString();
                    phone_no.Text = dr[4].ToString();
                }
                else
                {
                    MessageBox.Show("invalid id ");
                }
             con.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
       
    }
}

Code Screen:

   
I hope this post will help you..        
Still If you've some query regarding to this article then please post your comment.Your Comment will be appropriate 

0 comments:

Post a Comment

Hey thanax alot to comment i will revert you back soon...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites