Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Saturday, 22 July 2017

Difference Between DataAdapter And DataReader In Asp.Net With Example

 asp.net, C#, data reader and data adapter differences     2 comments   


Introduction
In this article we will learn what is DataAdapter and DataReader. What is the difference between dataadapter and datareader. When do we use dataadapter and when to use datareader. Use data reader and data adapter in connected and disconnected mode.

Previous Updates
In previous articles we have learnt Showing what is connection pooling in c# . Bind multiple dropdown list using single method call using c#. Read Excel file in C#  And Display in Grid. Read Excel in Windows Application project. Chart With Database In Asp.Net Using C#.  Insert Only Numeric Values In Asp TextBox Using Regex . Get TextBox , Dropdown, CheckBox  control Values In Aspx.cs Page From User Control Using C#.


When we are working with ADO.Net we used DataReader DataSet , DataAdapter , DataTables to interact with database. But they all have their different meaning and use based on the logic. 

DataReader
DataReader as its name implies it is used to read the data. DataReader reads the data from database. DataReader gets the records from Stored Procedure , table returning SELECT query.
DataReader works only in forward direction means row read once cannot be read again because of this it is fast to fetching records.
Most used function of datareader is Read function which  reads the table row one by one and returns true or false if it is read.
Datareader always required a Open connection for executing the SQL commands. Once a connection closed then you will not been able to read the data from  datareader. Thats why it is used in connecte mode in SQL.

string conStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conStr))
{
    using (SqlCommand cmd = new SqlCommand("SELECT EmpId, EmpName FROM Employee", con))
    {
        cmd.CommandType = CommandType.Text;
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string name = dr["EmpID"].ToString();
            string city = dr["EmpName"].ToString();
             Response.Write("EmpID: " + EmpID);
                Response.Write("Name: " + EmpName);
        }
         con.Close();
    }
}

DataAdapter
DataAdapter is also a ADO.Net object. It is used to execute the SQL statements and populate the result of that SQL query in DataSet or in DataTable.
DataAdapter gets all the returned rows from Sql statement at once and then fill the data into DataSet or datatable. Because of this dataadapter is slow in comparision to datareader.
DataReader have Read function just like Dataadapter has Fill function which fills the returned rows from sql statement onto dataset or datatable.
DataAdapter will not reuire any open and close connection . Means DataAdapter can work in Disconnected mode.

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("SELECT EmpId, EmpName FROM Employee", con))
    {
        cmd.CommandType = CommandType.Text;
        using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
        {
            DataSet ds = new DataSet();
             adp.Fill(ds);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string EmpID = row["EmpID"].ToString();
               string EmpName = row["EmpName"].ToString();
                Response.Write("EmpID: " + EmpID);
                Response.Write("Name: " + EmpName);
            }
        }
    }
}


  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

2 comments:

  1. Academic Paper17 May 2020 at 01:44

    I guess, that people, who are working in this sphere, know the difference between DataAdapter and DataReader. Though, it was very interesting for me to read.

    ReplyDelete
    Replies
      Reply
  2. Alexandra Bevan25 July 2021 at 23:16

    Hello there, You have performed an excellent job. I will definitely digg it and personally recommend to my friends. I am confident they will be benefited from this Microsoft AZ-120 Exam Questions.

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write