Thursday, May 29, 2008

How to retrieve column names using datareader? - Part I

Sometimes, we people when doing a higher things had lost some basics from memories, the sam happened with me.

Yesterday [May 28, 2008] my supervisor asked me a bit question - "How to retrieve column names using datareader?" I was junked that time and noticed that I have lost something from my memories of basics.
Today, I have sit back with my PC and workout the problem and write following Code snippet for VS2005:

We need a table to attain the above task, following query will solve the problem:

/* This query is created a HRnPAYROLL DATABASE
* for use of different examples shown in
* Book: C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora */

--First Create HRnPAYROLL DATABASE
Create Database HRnPAYROLL
go

Use HRnPAYROLL

--Create Employees Table and fill data

Create Table Employees
(
ID Varchar(4) Primary Key,
FirstName Varchar(25) Default 'Gaurav',
LastName Varchar(25) Default 'Arora',
Age Varchar(4) Default '19'
)
go

--*****************************************************************************************************************
Insert Into Employees Values ('0001','Anil','Jain','37');
Insert Into Employees Values ('0002','Aman','Jain','35');
Insert Into Employees Values ('0003','Amanpreet','Singh','28');
Insert Into Employees Values ('0004','Anuradha','Bhatia','24');
Insert Into Employees Values ('0005','Archana','Garg','25');
Insert Into Employees Values ('0111','Babita','Gupta','33');
Insert Into Employees Values ('0112','Babban','Das','24');
Insert Into Employees Values ('0113','Babu','Ram','24');
Insert Into Employees Values ('0114','Babbu','Man','38');
Insert Into Employees Values ('0115','Balbir','Singh','24');
Insert Into Employees Values ('0201','Chaman','Lal','38');
Insert Into Employees Values ('0202','Charan','Das','37');
Insert Into Employees Values ('0203','Chander Bhan','Singh','49');
Insert Into Employees Values ('0204','Changej','Khan','24');
Insert Into Employees Values ('0205','Champak','Lal','18');
Insert Into Employees (ID,FirstName,Age) Values ( '0160',' AMIT KUMAR','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0180',' ABHISHEK MISHRA','24')
Insert Into Employees (ID,FirstName,Age) Values ( '0181',' ARUN KUMAR','23')
Insert Into Employees (ID,FirstName,Age) Values ( '0184',' ASHUTOSH BAJPAYEE','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0185',' ANIL KUMAR YADAV','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0188',' ASHOK KUMAR PATHAK','25')
Insert Into Employees (ID,FirstName,Age) Values ( '0190',' ABHISHEK KUMAR SINGH','23')
Insert Into Employees (ID,FirstName,Age) Values ( '0194',' AMIT KUMAR THAKUR','22')
Insert Into Employees (ID,FirstName,Age) Values ( '0549',' BRIJESH CHANDRA','21')
Insert Into Employees (ID,FirstName,Age) Values ( '0558',' BRAJESH KUMAR','27')
Insert Into Employees (ID,FirstName,Age) Values ( '0560',' B.K. SINGH','31')
Insert Into Employees (ID,FirstName,Age) Values ( '0562',' BALBIR SINGH [MANGAT]','35')
Insert Into Employees (ID,FirstName,Age) Values ( '1244',' DHARMESH PANWAR','27')
Insert Into Employees (ID,FirstName,Age) Values ( '1296',' DEEPAK PRASAD','26')
Insert Into Employees (ID,FirstName,Age) Values ( '1320',' DEEPAK JAIN','30')
Insert Into Employees (ID,FirstName,Age) Values ( '2004',' FAKHRE MUBEEN','21')
Insert Into Employees (ID,FirstName,Age) Values ( '2466',' GAURAV ARORA','19')
Insert Into Employees (ID,FirstName,Age) Values ( '2480',' GURDEEP SINGH','34')
Insert Into Employees (ID,FirstName,Age) Values ( '2481',' GOPAL DATT SATI','27')
Insert Into Employees (ID,FirstName,Age) Values ( '3210',' KAHLON I.J. SINGH','26')
Insert Into Employees (ID,FirstName,Age) Values ( '3211',' INDRESH KUMAR PANDEY','35')
Insert Into Employees (ID,FirstName,Age) Values ( '3641',' JAI PRAKASH','27')
Insert Into Employees (ID,FirstName,Age) Values ( '3683',' JAYCHANDRA','27')
Insert Into Employees (ID,FirstName,Age) Values ( '3698',' J.K. SINGH','23')
Insert Into Employees (ID,FirstName,Age) Values ( '3700',' JEET NARAYAN SINGH','29')
Insert Into Employees (ID,FirstName,Age) Values ( '3701',' JIVENDRA KUMAR','29')
Insert Into Employees (ID,FirstName,Age) Values ( '4009',' KAPOOR SINGH','30')
Insert Into Employees (ID,FirstName,Age) Values ( '4057',' KISHAN LAMA','21')
Insert Into Employees (ID,FirstName,Age) Values ( '4110',' KRISHAN KUMAR MISHRA','33')
Insert Into Employees (ID,FirstName,Age) Values ( '4112',' K.B. CHOUBEY','33')
Insert Into Employees (ID,FirstName,Age) Values ( '4113',' KAISH UDDIN','35')
Insert Into Employees (ID,FirstName,Age) Values ( '4455',' LOKNATH BALBANTARAY','34')
Insert Into Employees (ID,FirstName,Age) Values ( '4801',' M.K. DEBROY','30')
Insert Into Employees (ID,FirstName,Age) Values ( '4884',' MADAN GIRI','21')
Insert Into Employees (ID,FirstName,Age) Values ( '4936',' MANJISH KUMAR','34')
Insert Into Employees (ID,FirstName,Age) Values ( '4946',' MADAN SINGH','24')
Insert Into Employees (ID,FirstName,Age) Values ( '4949',' MANOJ KUMAR TIWARI','21')
Insert Into Employees (ID,FirstName,Age) Values ( '4978',' MUKHTAR AHMED','18')
Insert Into Employees (ID,FirstName,Age) Values ( '4985',' MANOJ SAXENA','31')
Insert Into Employees (ID,FirstName,Age) Values ( '4987',' MANISH MISHRA','31')
Insert Into Employees (ID,FirstName,Age) Values ( '5263',' NEERAJ MAHAWAR','33')
Insert Into Employees (ID,FirstName,Age) Values ( '5271',' NIRAJ KUMAR SINHA','19')
Insert Into Employees (ID,FirstName,Age) Values ( '5272',' N.K. SINGH','21')
Insert Into Employees (ID,FirstName,Age) Values ( '5274',' NITIN GUPTA','32')
Insert Into Employees (ID,FirstName,Age) Values ( '5275',' NARESH KUMAR GUPTA','23')
Insert Into Employees (ID,FirstName,Age) Values ( '5612',' O.P. YADAV','32')
Insert Into Employees (ID,FirstName,Age) Values ( '6135',' PRAMOD SINGH RAWAT','30')
Insert Into Employees (ID,FirstName,Age) Values ( '6136',' PRAMOD KUMAR GAUTAM','22')
Insert Into Employees (ID,FirstName,Age) Values ( '6137',' PRAMOD KUMAR SINHA','31')
Insert Into Employees (ID,FirstName,Age) Values ( '6138',' PRASAD SASNUR','20')
Insert Into Employees (ID,FirstName,Age) Values ( '6700',' R. BHATTACHARJEE','21')
Insert Into Employees (ID,FirstName,Age) Values ( '6753',' RAMA SHANKAR','20')
Insert Into Employees (ID,FirstName,Age) Values ( '6765',' RAMESH CHANDRA','27')
Insert Into Employees (ID,FirstName,Age) Values ( '6776',' RAMU YADAV','20')
Insert Into Employees (ID,FirstName,Age) Values ( '6899',' RAVINDER LAMBA','29')
Insert Into Employees (ID,FirstName,Age) Values ( '6925',' RAVINDRA KUMAR RANA','24')
Insert Into Employees (ID,FirstName,Age) Values ( '6996',' R.P. YADAV','28')
Insert Into Employees (ID,FirstName,Age) Values ( '7010',' RAJESH PUNDIR','33')
Insert Into Employees (ID,FirstName,Age) Values ( '7011',' RAJIV CHAUDHARY','32')
Insert Into Employees (ID,FirstName,Age) Values ( '7012',' RAVINDRA YADAV','23')
Insert Into Employees (ID,FirstName,Age) Values ( '7013',' RAJESH SHARMA','32')
Insert Into Employees (ID,FirstName,Age) Values ( '7016',' RAVI SHANKAR','19')
Insert Into Employees (ID,FirstName,Age) Values ( '7017',' RAM BILAS CHOUDHARY','23')
Insert Into Employees (ID,FirstName,Age) Values ( '7022',' RAJ KISHORE RAI','24')
Insert Into Employees (ID,FirstName,Age) Values ( '7023',' R. RAMESH','35')
Insert Into Employees (ID,FirstName,Age) Values ( '7123',' SATISH KUMAR','32')
Insert Into Employees (ID,FirstName,Age) Values ( '7296',' SHANKAR LAL','29')
Insert Into Employees (ID,FirstName,Age) Values ( '7471',' SANJEEVAN M.K.','30')
Insert Into Employees (ID,FirstName,Age) Values ( '7480',' SANDEEP KR. SHRIVASTA','34')
Insert Into Employees (ID,FirstName,Age) Values ( '7484',' SUBHARAM BANERJEE','25')
Insert Into Employees (ID,FirstName,Age) Values ( '7490',' S.M. TRIPATHI','31')
Insert Into Employees (ID,FirstName,Age) Values ( '7501',' TEK BAHADUR CHATTRI','23')
Insert Into Employees (ID,FirstName,Age) Values ( '7513',' TULSI PRASAD','30')
Insert Into Employees (ID,FirstName,Age) Values ( '7919',' UMESH CHANDRA MISHRA','19')
Insert Into Employees (ID,FirstName,Age) Values ( '8480',' VIVEK TRIPATHI','31')
Insert Into Employees (ID,FirstName,Age) Values ( '8481',' VIJAY KUMAR','22')
Insert Into Employees (ID,FirstName,Age) Values ( '8485',' VIJAY KUMAR MISHRA','20')
Insert Into Employees (ID,FirstName,Age) Values ( '8486',' VINEET KUMAR SAXENA','35')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J001',' ANURAG', 'MISHRA','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J002',' ANURAG', ' DWIVEDI','24')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J006',' A.B.', ' KATIYAR','23')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J007',' AJAY', ' NAGAR','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J009',' AMIT KUMAR', ' SINGH','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J011',' AKHILESH KUMAR', ' VERMA','25')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J012',' ASHUTOSH', ' SRIVASTVA','23')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J013',' AKHILESH', ' BABU','22')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J018',' ASHOK KUMAR', ' PAL','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J019',' ANIL KUMAR', ' SINGH','27')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J023',' AMRISH', ' SHARMA','31')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J151',' CHANDRA PRATAP', ' SINGH','35')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J152',' C.P.', ' MISHRA','27')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J201',' DILIP', ' NAYAK','26')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J204',' DHEERAJ KUMAR', ' SACHAN','30')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J205',' DEVBRAT', ' SINGH','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J209',' DINESH', ' MISHRA','19')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J210',' DHARMENDRA KR.', ' PANDEY','34')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J316',' HEMANT', ' KUMAR','27')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J350',' JAGDISH', ' AWASTHI','26')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J366',' K.K.', ' KHULAR','35')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J371',' KAVIRAJ',' ','27')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J372',' KRISHNA KANT', ' SINGH','27')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J383',' LIBREN STENLY', ' LUGEN','23')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J384',' LING RAJ', ' SAHU','29')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J396',' MAHENDRA', ' PRAJAPATI','29')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J400',' MANJEET SINGH', ' REHSHI','30')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J403',' MAHENDRA', ' KHARAD','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J405',' MANPREET SINGH', ' JABBAL','33')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J411',' NEERAJ', ' TIWARI','33')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J416',' NARENDRA PRATAP', ' SINGH','35')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J417',' NARAYAN', ' SINGH','34')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J443',' PRAMOD KUMAR', ' SHARMA','30')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J444',' PRAFUL CHANDRA', ' RAI','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J445',' PRAKASH', ' SHARMA','34')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J447',' PRAVEEN KUMAR', ' DUBEY','24')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J448',' PRADEEP', ' KUMAR','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J450',' PRADEEP KR.', ' DWIVEDI','18')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J451',' PARTHA', ' CHOUDHARY','31')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J501',' RAM BABU', '','31')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J502',' RAJEEV', ' SHAHANI','33')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J508',' RAJEEV', ' LOCHAN','19')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J510',' RAJIV', ' KUMAR','21')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J551',' SANJAY', ' SINGH','32')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J554',' SUBODH', ' TIWARI','23')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J556',' SHIV SHANKAR', ' GOSWAMI','32')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J559',' SANJAY', ' TOMAR','30')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J562',' SURENDER', ' PAL SINGH','22')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J563',' SANDEEP', ' KUMAR','31')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J651',' T.', 'RAMESH','20')
Insert Into Employees (ID,FirstName,LastName,Age) Values ( 'J701',' UDAY RAJ', ' SINGH','20')

--*****************************************************************************************************************
-- Create HR_MAST_DEPT Table

CREATE TABLE HR_MAST_DEPT
(
[code] [char] (2) Primary Key ,
[Dpname] [varchar] (25) NOT NULL ,
[city] [varchar] (25),
[country] [varchar] (50),
[inhouse] [bit],

)
gO

-- Fill HR_MAST_DEPT Table

Insert Into HR_MAST_DEPT Values ( '10' , 'Information Technology' , 'Delhi' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '20' , 'Human Resources Dept' , 'Mohali' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '30' , 'Marketing' , 'Toronto' , 'Canada' , '0' )
Insert Into HR_MAST_DEPT Values ( '40' , 'Civil' , 'Calgiri' , 'Canada' , '0' )
Insert Into HR_MAST_DEPT Values ( '50' , 'Sales' , 'Gurgaon' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '60' , 'Commercial' , 'Delhi' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '70' , 'Admin' , 'Noida' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '80' , 'Testing' , 'Gurgaon' , 'India' , '1' )
Insert Into HR_MAST_DEPT Values ( '90' , 'Implementing' , 'Delhi' , 'India' , '1' )

--*****************************************************************************************************************

No comments:

Post a Comment