Sie sind auf Seite 1von 5

ADO.

NET SqlDataReader Class


This class is used to read data from SQL Server database. It reads data in
forward-only stream of rows from a SQL Server database. it is sealed class
so that cannot be inherited. It inherits DbDataReader class and implements
IDisposable interface.

SqlDataReader Signature
1. public class SqlDataReader : System.Data.Common.DbDataReader, IDi
sposable  

SqlDataReader Properties

Property Description

Connection It is used to get the SqlConnection associated with the SqlData

Depth It is used to get a value that indicates the depth of nesting for

FieldCount It is used to get the number of columns in the current row.

HasRows It is used to get a value that indicates whether the SqlDataRea


or more rows.

IsClosed It is used to retrieve a boolean value that indicates wheth


SqlDataReader instance has been closed.

Item[String] It is used to get the value of the specified column in its native
column name.

Item[Int32] It is used to get the value of the specified column in its native
column ordinal.
RecordsAffected It is used to get the number of rows changed, inserted or dele
of the Transact-SQL statement.

VisibleFieldCoun It is used to get the number of fields in the SqlDataReader tha


t

Methods

Method Description

Close() It is used to closes the SqlDataReader object.

GetBoolean(Int32) It is used to get the value of the specified column as a Bool

GetByte(Int32) It is used to get the value of the specified column as a byte

GetChar(Int32) It is used to get the value of the specified column as a sing

GetDateTime(Int32 It is used to get the value of the specified column as a Date


)

GetDecimal(Int32) It is used to get the value of the specified column as a Deci

GetDouble(Int32) It is used to get the value of the specified column as a


floating point number.

GetFloat(Int32) It is used to get the value of the specified column as a


floating point number.

GetName(Int32) It is used to get the name of the specified column.


GetSchemaTable() It is used to get a DataTable that describes the column
SqlDataReader.

GetValue(Int32) It is used to get the value of the specified column in its nati

GetValues(Object[]) It is used to populate an array of objects with the colum


current row.

NextResult() It is used to get the next result, when reading the


statements.

Read() It is used to read record from the SQL Server database.

To create a SqlDataReader instance, we must call the ExecuteReader


method of the SqlCommand object.

Example
In the following program, we are using SqlDataReader to get data from the
SQL Server. A C# code is given below.

// Program.cs

1. using System;  
2. using System.Data.SqlClient;  
3. namespace AdoNetConsoleApplication  
4. {  
5.     class Program  
6.     {  
7.         static void Main(string[] args)  
8.         {  
9.             new Program().GetData();  
10.         }  
11.         public void GetData()  
12.         {  
13.             SqlConnection con = null;  
14.             try  
15.             {  
16.                 // Creating Connection  
17.                 con = new SqlConnection("data source=.; database=stude
nt; integrated security=SSPI");  
18.                 // writing sql query  
19.                 SqlCommand cm = new SqlCommand("select * from stude
nt", con);  
20.                 // Opening Connection  
21.                 con.Open();  
22.                 // Executing the SQL query  
23.                 SqlDataReader sdr = cm.ExecuteReader();  
24.                 while (sdr.Read())  
25.                 {  
26.                     Console.WriteLine(sdr["name"]+" "+ sdr["email"]);  
27.                 }  
28.             }  
29.             catch (Exception e)  
30.             {  
31.                 Console.WriteLine("OOPs, something went wrong." + e);  
32.             }  
33.             // Closing the connection  
34.             finally  
35.             {  
36.                 con.Close();  
37.             }  
38.         }  
39.     }  
40. }  

Output:

Execute this program by combination of Ctrl+F5 and it will produce the


following output.

Das könnte Ihnen auch gefallen