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. using (DataReader) inside loop

using (DataReader) inside loop

Scheduled Pinned Locked Moved C#
questiondatabaseperformance
6 Posts 2 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.
  • C Offline
    C Offline
    cnich23
    wrote on last edited by
    #1

    As far as performance issues are concerned I have a question .... I have to make multiple selects based on date inside of a loop. Is it wise to use the USING statement for the datareader inside of the loop or does something need to be done outside the loop. Here is my current code. using (SQLCommand selectAnnv = new SQLCommand(_database)) { //create paramater date for variable select SQLParameter paramDate = new SQLParameter("@DATE"); // create parement query @DATE is variable selectAnnv.CommandText = "SELECT id FROM " + TMEMBERS + " WHERE registered = '(@DATE)'"; selectAnnv.Parameters.Add(paramDate); // loop through each day missed to current day for (DateTime anniversary = lastHandled.AddDays(1); anniversary <= now; anniversary = anniversary.AddDays(1)) { paramDate.Value = GStatic.GetISO8601(anniversary); _**//add ids to list // Data reader intialized outside loop using (SQLDataReader readerAnnv = selectAnnv.ExecuteReader()) { while (readerAnnv.Read()) ids.Add(readerAnnv.GetInt32(0)); }**_ } } Should the bolded/italic part be decalered outside of the loop?

    C D 2 Replies Last reply
    0
    • C cnich23

      As far as performance issues are concerned I have a question .... I have to make multiple selects based on date inside of a loop. Is it wise to use the USING statement for the datareader inside of the loop or does something need to be done outside the loop. Here is my current code. using (SQLCommand selectAnnv = new SQLCommand(_database)) { //create paramater date for variable select SQLParameter paramDate = new SQLParameter("@DATE"); // create parement query @DATE is variable selectAnnv.CommandText = "SELECT id FROM " + TMEMBERS + " WHERE registered = '(@DATE)'"; selectAnnv.Parameters.Add(paramDate); // loop through each day missed to current day for (DateTime anniversary = lastHandled.AddDays(1); anniversary <= now; anniversary = anniversary.AddDays(1)) { paramDate.Value = GStatic.GetISO8601(anniversary); _**//add ids to list // Data reader intialized outside loop using (SQLDataReader readerAnnv = selectAnnv.ExecuteReader()) { while (readerAnnv.Read()) ids.Add(readerAnnv.GetInt32(0)); }**_ } } Should the bolded/italic part be decalered outside of the loop?

      C Offline
      C Offline
      cnich23
      wrote on last edited by
      #2

      BTW this is just an example there is actually a few more variables to read so ExecuteScalar does not apply

      1 Reply Last reply
      0
      • C cnich23

        As far as performance issues are concerned I have a question .... I have to make multiple selects based on date inside of a loop. Is it wise to use the USING statement for the datareader inside of the loop or does something need to be done outside the loop. Here is my current code. using (SQLCommand selectAnnv = new SQLCommand(_database)) { //create paramater date for variable select SQLParameter paramDate = new SQLParameter("@DATE"); // create parement query @DATE is variable selectAnnv.CommandText = "SELECT id FROM " + TMEMBERS + " WHERE registered = '(@DATE)'"; selectAnnv.Parameters.Add(paramDate); // loop through each day missed to current day for (DateTime anniversary = lastHandled.AddDays(1); anniversary <= now; anniversary = anniversary.AddDays(1)) { paramDate.Value = GStatic.GetISO8601(anniversary); _**//add ids to list // Data reader intialized outside loop using (SQLDataReader readerAnnv = selectAnnv.ExecuteReader()) { while (readerAnnv.Read()) ids.Add(readerAnnv.GetInt32(0)); }**_ } } Should the bolded/italic part be decalered outside of the loop?

        D Offline
        D Offline
        Dustin Metzgar
        wrote on last edited by
        #3

        Why not just use a date range in your sql statement?


        Logifusion[^] If not entertaining, write your Congressman.

        C 1 Reply Last reply
        0
        • D Dustin Metzgar

          Why not just use a date range in your sql statement?


          Logifusion[^] If not entertaining, write your Congressman.

          C Offline
          C Offline
          cnich23
          wrote on last edited by
          #4

          The engine does not support date type

          D 1 Reply Last reply
          0
          • C cnich23

            The engine does not support date type

            D Offline
            D Offline
            Dustin Metzgar
            wrote on last edited by
            #5

            Could you clarify that statement for me? You're using a SQLDataReader, so you're talking to a SQL Server.


            Logifusion[^] If not entertaining, write your Congressman.

            C 1 Reply Last reply
            0
            • D Dustin Metzgar

              Could you clarify that statement for me? You're using a SQLDataReader, so you're talking to a SQL Server.


              Logifusion[^] If not entertaining, write your Congressman.

              C Offline
              C Offline
              cnich23
              wrote on last edited by
              #6

              I'm sorry this is a custom DB much like SQLite, with even more limited types

              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