Example on Data Reader in C# .ne

Example on Data Reader:-

  • Design a from as follows
  • Place two labels, two text boxes and two buttons


using  System. Data .oledb;

code in GD

Static  oledbConnection  con = new  oledbConnection  (“User id  = Scoot; Password = tiger; Provider = ms data.1*”);

Oledb command cmd  = new oledbcommand   (“Select  * from EMP, con”);

Oledb Data Reader . dr;//abstrsct

Code for Button 1_click


Con. open ();

Message Box. Show (“Connection is ok”);

If (con. State  = = connection State. closed)


Con. open ();

Dr = cmd. Execute Reader ();

Message Box. Show (“Con. Server version”);

// column Names

Label 1.Text  = dr. Get Name (0);

Label 1.Text  = dr. Get Name (1);



Message Box. Show (“Connection  is already opened”);

Code for Button2 _click


If  (dr.  Read  () = = true)


textBox1. Text  = dr [0] . To String ();

textBox1. Text  = dr [1] . To String ();



Message Box. Show  (“No more Records”);


Data Reader EX 2

Developing a Dynamic From With Sql Server data base over Managed connection


EMP à25 columns






  • Open windows Forms Application Project
  • Open a Text box and two buttons as shown


Using System. Data. Sqlclient;

Code for GD

Sqlconnection con = new Sqlconnection  (“User id = Sa; Password =; database = northwind; Server = local host”);

Sql command cmd = new sql command ();

Sql  Data Reader dr;


Label[]  L;

Text Box [] T;

Int  x = 50;

Int [] n = new int  []

Code for Button 1_click

con. open ();

String  q = “Select * from ”  + text Box1 .Text;

One space must be given

cmd. Command Text = q;

cmd. connection = con;

dr = cmd. Execute Reader ();

Message Box .Show  (dr. Field count + “”);

L = new Label  [dr. Field Count];

T = new Text Box [dr. Field Count];

//add the controls in to form

For (int  I =0; I < dr. Field Count ; I ++)


L [i] = new Label ();

L[i] .Text  = dr. Get Name (i);

L[i] . Location = new Point  (100, x);

This .Controls. Add (L  [i]);

T [i] = new Text Box  ();

T [i] . Location = new Point  (200,x);

This. Controls . Add ( T [i]);

X= x + 30;



Code for Button 2_clikc                       DR [0]                     DR [1]

{                                                              ENO          EN       SAL

If (dr. Read () = = true)                           101          Ravi              5000

{                                                        T [0] Text  = DR [0]. To String ();

For (int I = 0; I <dr. Field Count ; I++)

T [i] .Text  = dr [i]. To. String ();



Message Box. Show (“No more Records”);



OBS :-


When connection is closed , then all associated  data readers will be closed automatically.

Data Reader  works only when connection model is active  (open)

Data Reader are connection oriented.

If no. of clients are more in the network . then Data base efficiency will decreases.



“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Example on Data Reader in C# .ne"

    Leave a Message

    Your email address will not be published. Required fields are marked *


    Please leave a message and we'll get back to you soon.
    Three + 6