Details the differences between datareader and dataset in ES0en.net

  • 2020-06-19 10:05:01
  • OfStack

1. Method of data acquisition
DataReader is the online operation data, DataReader 1 will directly occupy the SqlConnection connection, and other operations cannot use the SqlConnection connection object during its acquisition of data.

while(datareader.read())
{
..............
}
dataview.datasource=datareader;
dataview.databind();

DataSet is for offline manipulation of data. DataSet reads the data into memory once and then disconnects, allowing other operations to connect to the object using SqlConnection.
The background code

public string test = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds=new DataSet();// Here's your data. I won't write it down 
        test = "<table>";
        for (int i = 0; i < ds.Tables[0].Rows; i++)
        {
            test+="<tr><td>"+ds.Tables[0].Rows[i][" The field you want "].ToString()+"</td></tr>"
        }
        test+="</table>";
    }

Page code

<form id="form1" runat="server">
    <%=test %>
    </form>

Since DataReader only reads one row at a time, it occupies less memory. However, DataReader is forward and read-only, meaning it can only be read in one direction. If you want to go back to read the last piece of data, it is not allowed, and it is not allowed to modify the data.
DataSet1 reads all the data at once, so it's more resource-intensive, but it also gives you more flexibility, allowing you to add, delete, change, read in any order and write back to the database if you disconnect from the database.
One thing to note is that DataReader1 reads 1 row at a time, which does not mean that the data in the database has been modified and new data can be read, which is the protection at the database level.
2. A mechanism for obtaining data
DataReader reads data via ES27en.ES28en.
DataSet populates the data via ES31en.ES32en
So DataReader cannot close the connection while fetching the data. DataSet will, because DbDataAdapter has already read the data to the application server, be careful when using DataReader 1 to close the connection.
3. The other difference
DataReader reads faster than DataSet
DataReader is the data provider class and DataSet is a generic class that populates data with DbDataAdapter.
Because DataSet is offline operation data, be careful when using locks in a transaction because DataSet disconnects when it populates the data and releases the lock.
4. Using DataReader can improve the efficiency of execution. There are two ways to improve the performance of the code: one is ordinal lookup and the other is to use the appropriate Get method for lookup. Because the query result 1 generally does not change unless the query is changed again, you can locate the record by locating the column. The problem with this approach is that it is possible to know the name of a column without knowing where it is, and the solution is to call the GetOrdinal() method of the DataReader object, which takes a column name and returns the column number of the column name. Ex. :

int id = reader.GetOrdinal("CategoryName");
while (reader.Read())
{
  Response.Write(reader.GetInt32(0).ToString() + " " + reader.GetString(1).ToString() + "<br />");
  reader.Close();
}

DataReader's GetInt32() and GetString(), which return the value of a column by receiving a column number, are the two most commonly used, and there are many other types.
(Note: When the DataReader object calls the Close() method, it closes the connection to the database. If it re-opens the second connection before closing, an exception message is generated.)

Related articles: