Blog

Example on Data Reader in C# .ne

12 September, 2018

Related Blogs

Example on Data Reader:-
  • Design a from as follows
  • Place two labels, two text boxes and two buttons
Screenshot_20 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); } Else 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 (); } Else Message Box. Show  (“No more Records”);   Data Reader EX 2 Developing a Dynamic From With Sql Server data base over Managed connection Project EMP à25 columns Deptà8 Bonusà4   Screenshot_21  
  • 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; }//for   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 (); } Else Message Box. Show (“No more Records”); Screenshot_22   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.