-->

15 March 2020

ADO.NET DataReader

  Asp.Net CS By Example       15 March 2020
 ADO.NET OracleDataReader 

 In this post we are exploring more about OracleDataReader class.We use an object of the OracleDataReader class to read rows retrieved from a Oracle database.

Data reader objects only allow we to access rows one after another from beginning to end. For this reason, these objects aren’t the most flexible when accessing rows, but they do offer the fastest access to the rows stored in them. If we don’t need flexible access to rows, we might want to consider using a data reader object.

The OracleDataReader supports the following Properties (this is not a complete list):

Sr.No Property Type Description
1) Depth int Gets a value indicating the depth of nesting for the current row.
2) FieldCount int Gets the number of columns in the current row.
3) IsClosed bool Gets a bool value indicating whether the data reader is closed.
4) RecordsAffected int Gets the number of rows added, modified, or removed by execution of the SQL statement.

The OracleDataReader supports the following Methods (this is not a complete list):

Sr.No Method Return Type Description
1) GetBoolean() bool Returns the value of the specified column as a bool.
2) GetByte() byte Returns the value of the specified column as a byte.
3) GetBytes() long Reads a stream of byte values from the specified column into a byte array. The long value returned is the number of byte values read from the column.
4) GetChar() char Returns the value of the specified column as a char
5) GetChars() long Reads a stream of char values from the specified column into a char array. The long value returned is the number of char values read from the column.
6) GetDataTypeName() string Returns the name of the source data type for the specified column.
7) GetDateTime() DateTime Returns the value of the specified column as a DateTime.
8) GetDecimal() decimal Returns the value of the specified column as a decimal.
9) GetDouble() double Returns the value of the specified column as a double.
10) GetFieldType() Type Returns the Type of the specified column.
11) GetFloat() float Returns the value of the specified column as a float.
12) GetGuid() Guid Returns the value of the specified column as a globally unique identifier (GUID).
13) GetInt16() short Returns the value of the specified column as a short.
14) GetInt32() int Returns the value of the specified column as an int.
15) GetInt64() long Returns the value of the specified column as a long.
16) GetName() string Returns the name of the specified column.
17) GetOrdinal() int Returns the numeric position, or ordinal, of the specified column (first column has an ordinal of 0).
18) GetSchemaTable() DataTable Returns a DataTable that contains details of the columns stored in the data reader.


 Now here we look sample code how to use DataReader Class of Ado.Net. We have fetch data from database using DataReader Class in Console Project.
  Code: Program.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;

namespace ProjDataReaderExample
{
    class  Program
    {
        public static void Main()
        {
            string connectionString = "data source=LearnAsp;user id=LearnAsp;"
            + "password=LearnAsp; Pooling=true; Max pool size=200; Min pool size=0;";

            Console.WriteLine("\n\tAdo.Net DataReader Example\n");
            OracleConnection myConn = new OracleConnection(connectionString);

            string selectString = "SELECT deptno,dname, loc FROM dept ";

            OracleCommand myCommand = myConn.CreateCommand();
            myCommand.CommandText = selectString;
            myConn.Open();
            OracleDataReader myDataReader = myCommand.ExecuteReader();
            myDataReader.Read();

            Console.WriteLine("\tRetrieving a row from the Department table\n\n");
            while (myDataReader.Read())
            {
                Console.WriteLine("\tmyDataReader[\" deptno\"] = " + myDataReader["deptno"]);
                Console.WriteLine("\tmyDataReader[\" dname\"] =" + myDataReader["dname"]);
                Console.WriteLine("\tmyDataReader[\" loc\"] =" + myDataReader["loc"]);
                Console.WriteLine("");
            }

            Console.WriteLine("\t All Record Fetch From Table. \n\n");

            myConn.Close();
        }
    }
} 

Output
Ado.Net DataReader Exmaple

  That's it we have use DataReader Class to Fetch Data from Database using a C#.Net Console Project.
logoblog

Thanks for reading ADO.NET DataReader

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.