Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SqlDataReader vs. SqlDataAdapter

SqlDataReader vs. SqlDataAdapter

Scheduled Pinned Locked Moved C#
visual-studioquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    Would anyone care to point out to me what the main differences between SqlDataReader and SqlDataAdapter are, when to use one and when to use the other? I've been using SqlDataReader exclusively so far and have not yet even bothered to read what SqlDataAdapter really is about.

    OriginalGriffO W P 3 Replies Last reply
    0
    • D Dewald

      Would anyone care to point out to me what the main differences between SqlDataReader and SqlDataAdapter are, when to use one and when to use the other? I've been using SqlDataReader exclusively so far and have not yet even bothered to read what SqlDataAdapter really is about.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      The big difference is time. When you use a SqlDataReader, you open a connection, start reading, then loop though each record, processing it. When you have finished, you close the database connection. When you use a SqlDataAdapter, it opens the connection, takes a snapshot of the database, and closes the connection. You can then process the records. You can use a DataAdapter to bind a data source to a control, such as a DataGridView, rather than iterating though each record. Have a look on Google: "SqlDataReader vs. SqlDataAdapter" will give you a lot of hits, most of which are pretty useful. (And yes, I do know that a SqlDataAdapter leaves the connection open if it was already - that's the advanced course, ok?)

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      D 1 Reply Last reply
      0
      • D Dewald

        Would anyone care to point out to me what the main differences between SqlDataReader and SqlDataAdapter are, when to use one and when to use the other? I've been using SqlDataReader exclusively so far and have not yet even bothered to read what SqlDataAdapter really is about.

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        I personally like to stick to DataReaders and convert the data into proper CLR objects, rather than binding directly to a DataAdapter. Especially when it comes to WPF applications, as then you can have all the proper interfaces such as IDataErrorInfo and INotifyPropertyChanged which makes Databinding and Validation much easier. I have heard arguments for DataAdapters before but never used them. Just my 2c. :)

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          The big difference is time. When you use a SqlDataReader, you open a connection, start reading, then loop though each record, processing it. When you have finished, you close the database connection. When you use a SqlDataAdapter, it opens the connection, takes a snapshot of the database, and closes the connection. You can then process the records. You can use a DataAdapter to bind a data source to a control, such as a DataGridView, rather than iterating though each record. Have a look on Google: "SqlDataReader vs. SqlDataAdapter" will give you a lot of hits, most of which are pretty useful. (And yes, I do know that a SqlDataAdapter leaves the connection open if it was already - that's the advanced course, ok?)

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

          D Offline
          D Offline
          Dewald
          wrote on last edited by
          #4

          OK thanks, that helps. As regards databinding, I've always used SqlDataReader, even for that, like so:

          using (SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
          {
          DataSet myDataSet = new DataSet();
          DataTable myDataTable = new DataTable();
          myDataSet.Tables.Add(myDataTable);
          myDataSet.Load(mySqlDataReader, LoadOption.PreserveChanges, myDataSource.Tables[0]);
          myDataGridView.DataSource = myDataSource.Tables[0];
          }

          Do you know of any advantage over this by rather using a SqlDataAdapter? How would you do that?

          P 1 Reply Last reply
          0
          • D Dewald

            Would anyone care to point out to me what the main differences between SqlDataReader and SqlDataAdapter are, when to use one and when to use the other? I've been using SqlDataReader exclusively so far and have not yet even bothered to read what SqlDataAdapter really is about.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I always use a DataReader, never a DataAdapter or DataSet. In my opinion, DataAdapters, DataSets, and DataGrids were developed to allow Microsoft presenters to very quickly query, display, and update simple data and get an "oooh" from the crowd. They are fine for prototyping, but are not suitable for production enterprise applications. DataAdapters are very limited in what they can do and actually try to do too much -- e.g. optimistic concurrency[^]. The few times I've been lazy and used a DataAdapter in a real scenario, it bit me and I had to rework the code to use a DataReader instead. Also, bear in mind that in ADO.net, all data access goes through a DataReader -- ExecuteScalar, ExecuteNonQuery, DataAdapter.Fill, and DataAdapter.Update all use ExecuteReader to perform the tasks. ExecuteReader can be used to execute any SQL statement your database accepts -- queries, DML, and DDL. And, in some cases (SQL Server) you can pass a semi-colon delimited list of SQL Statements (though it doesn't report the results of non-queries the way I'd like X| ). By using a DataReader you can reduce your application's memory footprint when you don't need to have all the records in memory at the same time. Or you can fill a collection of custom objects rather than a DataTable. Or you can fill a DataTable your own way and avoid the DataAdapter's overhead. DataGrids and DataGridViews are rarely an appropriate control for displaying and editing data in real situations.

            1 Reply Last reply
            0
            • D Dewald

              OK thanks, that helps. As regards databinding, I've always used SqlDataReader, even for that, like so:

              using (SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
              {
              DataSet myDataSet = new DataSet();
              DataTable myDataTable = new DataTable();
              myDataSet.Tables.Add(myDataTable);
              myDataSet.Load(mySqlDataReader, LoadOption.PreserveChanges, myDataSource.Tables[0]);
              myDataGridView.DataSource = myDataSource.Tables[0];
              }

              Do you know of any advantage over this by rather using a SqlDataAdapter? How would you do that?

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I wouldn't even use a DataSet like that, I define and fill DataTables myself. This[^] article shows how.

              D 1 Reply Last reply
              0
              • P PIEBALDconsult

                I wouldn't even use a DataSet like that, I define and fill DataTables myself. This[^] article shows how.

                D Offline
                D Offline
                Dewald
                wrote on last edited by
                #7

                Thanks, it looks interesting, but jeeeez, it looks complicated. I'll go through it more attentively when I have a little more time but, briefly, could you explain why that method is better than using a DataSet like in my earlier example?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups