You must Sign In to post a response.
  • Category: .NET

    How to use Threading in Dataset and datareader ?


    Are you looking for a way to use Threading in Dataset and datareader ? then read this threads to know more about it



    I Faced a question regarding Threading. The question asked is

    To retrieve data from a sql shall we use one thread for dataset and a separate thread for datareader? will it run? Is it safe?
  • #748236
    Data reader and data set are 2 different architecture one is connected and one is disconnected.

    In data reader it is connected so you need a live thread and for data set since it is a disconnected architecture you have it in another thread.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #748247
    Use the DataSet in order to do the following with your application:

    1. Navigate between multiple discrete tables of results.
    2. Manipulate data from multiple sources (for example, a mixture of data from more than one database, from an XML file, and from a spreadsheet).
    3. Exchange data between tiers or using an XML Web service. Unlike the DataReader, the DataSet can be passed to a remote client.
    4. Reuse the same set of rows to achieve a performance gain by caching them (such as for sorting, searching, or filtering the data).
    5. Perform a large amount of processing per row. Extended processing on each row returned using a DataReader ties up the connection serving the DataReader longer than necessary, impacting performance.
    6. Manipulate data using XML operations such as Extensible Stylesheet Language Transformations (XSLT transformations) or XPath queries.

    Use the DataReader in your application if you:
    Do not need to cache the data.
    1. Are processing a set of results too large to fit into memory.
    2. Need to quickly access data once, in a forward-only and read-only manner.

    Both functions can be used in different thread based on your scenarios

    reagrds
    Kush

  • #748255
    Yes, I agree with Anil answer, both are in different in nature you can perform in different threads for both..
    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #748276
    In case of datareader application is connected to database untill we read all records from the database cursor memory. Only one record can be read at a time sothere can be only one thread responsible for getting the data from database. If you use multiple threads you will have synchronization issues.
    for datatables, read operations are thread safe whereas update operations such as insert, update and delete are not thread safe so you need to implement synchnization concepts such as locks on dataset update statements.
    Refer below link for more information:
    mobile.experts-exchange.com/Programming/Languages/C_Sharp/Q_22076504.html

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #748284
    Hi Deena,

    DataReader is used to directly connected to database system where as Dataset is generally used to employ disconnected architecture of database System.
    Also Dataset is used to reads data from database and stores in local system where as for DataReader there is no Local storage is required. So you have to use different threads for both dataset and datareader.

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #748303
    HI,,

    DataReader

    DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. DataReader will fetch the data very fast when compared with dataset. Generally we will use ExecuteReader object to bind data to datareader.

    To bind DataReader data to GridView we need to write the code like as shown below:



    Protected void BindGridview()

    {

    using (SqlConnection conn = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=Test"))

    {

    con.Open();

    SqlCommand cmd = new SqlCommand("Select UserName, First Name,LastName,Location FROM Users", conn);

    SqlDataReader sdr = cmd.ExecuteReader();

    gvUserInfo.DataSource = sdr;

    gvUserInfo.DataBind();

    conn.Close();

    }

    }

    •Holds the connection open until you are finished (don't forget to close it!).
    •Can typically only be iterated over once
    •Is not as useful for updating back to the database
    DataSet

    DataSet is a disconnected orient architecture that means there is no need of active connections during work with datasets and it is a collection of DataTables and relations between tables. It is used to hold multiple tables with data. You can select data form tables, create views based on table and ask child rows over relations. Also DataSet provides you with rich features like saving data as XML and loading XML data.

    protected void BindGridview()

    {

    SqlConnection conn = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=Test");

    conn.Open();

    SqlCommand cmd = new SqlCommand("Select UserName, First Name,LastName,Location FROM Users", conn);

    SqlDataAdapter sda = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    da.Fill(ds);

    gvUserInfo.DataSource = ds;

    gvUserInfo.DataBind();

    }


  • Sign In to post your comments