TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ADO.Net Datareader

ADO Net Datareader with introduction, data providers, sql server connectivity, connection, command, datareader, dataset, dataadapter, datatables, web form examples, mvc examples etc.

<< Back to ADO

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

public class SqlDataReader : System.Data.Common.DbDataReader, IDisposable

SqlDataReader Properties

Property Description
Connection It is used to get the SqlConnection associated with the SqlDataReader.
Depth It is used to get a value that indicates the depth of nesting for the current row.
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 SqlDataReader contains one or more rows.
IsClosed It is used to retrieve a boolean value that indicates whether the specified SqlDataReader instance has been closed.
Item[String] It is used to get the value of the specified column in its native format given the column name.
Item[Int32] It is used to get the value of the specified column in its native format given the column ordinal.
RecordsAffected It is used to get the number of rows changed, inserted or deleted by execution of the Transact-SQL statement.
VisibleFieldCount It is used to get the number of fields in the SqlDataReader that are not hidden.

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 Boolean.
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 single character.
GetDateTime(Int32) It is used to get the value of the specified column as a DateTime object.
GetDecimal(Int32) It is used to get the value of the specified column as a Decimal object.
GetDouble(Int32) It is used to get the value of the specified column as a double-precision floating point number.
GetFloat(Int32) It is used to get the value of the specified column as a single-precision 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 metadata of the SqlDataReader.
GetValue(Int32) It is used to get the value of the specified column in its native format.
GetValues(Object[]) It is used to populate an array of objects with the column values of the current row.
NextResult() It is used to get the next result, when reading the results of SQL 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

using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            new Program().GetData();
        }
        public void GetData()
        {
            SqlConnection con = null;
            try
            {
                // Creating Connection
                con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
                // writing sql query
                SqlCommand cm = new SqlCommand("select * from student", con);
                // Opening Connection
                con.Open();
                // Executing the SQL query
                SqlDataReader sdr = cm.ExecuteReader();
                while (sdr.Read())
                {
                    Console.WriteLine(sdr["name"]+" "+ sdr["email"]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("OOPs, something went wrong." + e);
            }
            // Closing the connection
            finally
            {
                con.Close();
            }
        }
    }
}

Output:

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

ADO Net Sqldatareader Class 1
Next TopicADO.NET DataSet




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf