Pages

Thursday, February 24, 2011

24/02 Accessing DATA from a DataReader:


Date 24/2/11 Thursday

Accessing DATA from a DataReader: - DataReader is a class which hold Data in the form of Rows and Columns (Table Structure). To access the data from the DataReader, it provide us following methods.

1.      Read() à bool    (return type)
-          Moves the record pointer from current location to next Row and returns a status specifying, where the Row contains Data to which it has moved, that will be true if present or False if not present.

2.      GetValue(int index) à object
-          Used for retrieving field values from the row to which pointer was pointing by specific the column index position.

Note -  we can also access the row pointer by pointer in the form of single dimensional array also, either by specifying column index position or name, As following:

< DR > [index ]             à object
< DR > [Colname]        à object


3.      GetName(int Index)   à String
-          Returns the name of column for given index position.

4.      NextResult()               à bool
-          Moved the record pointer from current table to next table if a table exists & returns true or else returns False.

Add a new form in the project:
Pics

-          Now write the following code:
using System.Data.OleDb;     

Variable Declaration :

OleDbConnection  con;
OleDbcommand     cmd;
OleDbDataReader   dr;

Under Form load :

con = new OleDbConnection(“Provider=Msdaora; User ID = Scott; Password=tiger”);
cmd = new OleDbcommand(“Select Deptno, Dname, Loc From Dept”, con);

con.Open();

dr = cmd.ExecuteReader();
lable1.Text = dr.GetName(0);
lable2.Text = dr.GetName(1);
lable3.Text = dr.GetName(2);
ShowData();

Define a method ShowData :

Private void ShowData()
{
   If(dr.Read())
   {
      textBox1.Text = dr.GetValue(0).ToStrring();
      textBox1.Text = dr[1].ToString();
      textBox1.Text = dr[“Loc”].ToString();
   }

else
   {
         MessageBox.Show(“Last Record”
   }

}


Under next button :

ShowData();


Under Close Button :

If(con.state != ConnectionState.Closed)
con.Class();
this.Close();

1 comments:

fdfsdf said...

Dotnet Programmer : Article's | Project's | Interview Question & Answer

http://allittechnologies.blogspot.in/

Post a Comment