Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Wednesday 29 August 2012

Difference Between DataAdapter And DataReader

A significant number of programmers have been using DataAdapter and DataReader, but most of them don't have time or they don't pay attention to the distinction between them. Today in this article, we will examine the primary difference between a DataAdapter and a DataReader.

DataAdapter:

The DataAdapter object works as a two-way bridge among the data source and the DataSet object. DataSet is a disconnected data container, and the adapter is responsible for filling data and submitting its data back to a specific data source. From an abstract point of view, a dataadapter is similar to a command/query and represents another way of executing commands against a data source. The biggest difference between commands and data adapters is how they return the retrieved data. The dataadapter accesses the data to obtain the data and packs it into an in-memory container (DataSet or DataTable). The important point about the data adapter is that it is a two-way channel used to read data from the data source to the memory table and write the data in the memory back to the data source.

In Simple Words:

We can read multiple lines. It is the bridge between the database and the DataSet.

It is not always connected to the database. This is a multi-purpose method, we can (read from the database, update to the database)

DataReader:

The DataReader is also used to obtain data from the data source. By using the datareader object, we can read one row at a time. It is the first choice when you need direct data access because it uses a real-time connection. And due to the nature of optimization, its retrieval speed is fast and the performance is the best as compare to DataSet.

Like other ADO.NET objects, each data provider has a datareader class. OleDbDataReader is the datareader class of the OleDb data provider. SqlDataReader and ODBC DataReader are also data reader classes for SQL and ODBC data providers.

The datareader object can only be used to read data forward. When using a datareader object, make sure to open the connection first, and then close the data reader and connection after reading all records. Normally developers use ExecuteReader object to bind data with datareader. Below is the example:

Example:


  Public void BindGridView() {  
    using(SqlConnection conn = new SqlConnection("Data Source=XYZ;Integrated Security=true;Initial Catalog=DemoDB")) {  
        con.Open();  
        string qry = "Select UserName, First Name,LastName FROM Users";
        SqlCommand cmd = new SqlCommand(qry, conn);  
        SqlDataReader sdr = cmd.ExecuteReader();  
        GridViewUser.DataSource = sdr;  
        GridViewUser.DataBind();  
        conn.Close();  
    }  
}
 

In Simple words:

We can read a single row. It uses a live connection. It is read-only and forward only.

I hope that after reading this article, you have understood the difference between DataAdapter and DataReader. In case there is something missing in the article, please do write us in the comment section.

No comments:

Post a Comment

Please do not enter spam links.