SqlDataReader -- builtin get rowcount?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rich

    SqlDataReader -- builtin get rowcount?

    Is there any builtin functionality to the SqlDataReader for getting a row
    count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?
  • Chris

    #2
    Re: SqlDataReader -- builtin get rowcount?

    Rich wrote:[color=blue]
    > Is there any builtin functionality to the SqlDataReader for getting a row
    > count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?[/color]


    No, since the datareader only fetches rows as you ask for them
    (rdr.Read) it does not know how many there will be. If you use the data
    adapter, then you will be able to get a row count since it fetches the
    rows. Otherswise, make to calls, on to get the rowcount and one to get
    the data.

    Chris

    Comment

    • Deepak

      #3
      RE: SqlDataReader -- builtin get rowcount?

      Hello Rich
      I donot think there is a property that can give you the row count right away
      just like the record count property of ADODB.Recordset
      Probably, You can load the data into a dataset and use datarow collection to
      get the row count.
      Let me know if you find a better solution
      Thanks
      Deepak

      "Rich" wrote:
      [color=blue]
      > Is there any builtin functionality to the SqlDataReader for getting a row
      > count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?[/color]

      Comment

      • Rich

        #4
        RE: SqlDataReader -- builtin get rowcount?

        Thanks all for your replies. Basically, I have a sql table that contains
        columns of column names for other tables. Col1 may contain 90 rows where
        each cell contains column names for tbl1, Col2 contains 40 rows of not null
        data for the columns in tbl2, ...

        I am using a datareader to read the not null values in each column
        individually, and I loop through the datareader to add sqlInsertComman d
        Params (for my Insert SP in sql server which has the same number of params).
        So I am stuck with using

        i += 1 '--i is the column ordincal for tlb1 and tbl2 and the row in the
        columnNames
        '--table
        '--rdr reads the rows in the ColumnNames table
        While rdr.Read
        str1 = rdr.GetString(0 )
        '--this is a Text field for tbl1 and tbl2
        If ((stp = "cd" Or stp = "qa") And i = 2) Or (stp = "qa" And i = 8) Then
        cmdNcalInsert.P arameters.Add(s tr1, SqlDbType.NText , 16,
        str1.Substring( 1, str1.Length - 1))
        ElseIf str1 = "@RowNum" Then
        cmdNcalInsert.P arameters.Add(s tr1, SqlDbType.Int, 4, str1.Substring( 1,
        str1.Length - 1))
        Else
        cmdNcalInsert.P arameters.Add(s tr1, SqlDbType.NVarC har, 20,
        str1.Substring( 1, str1.Length - 1))
        End If
        i += 1
        End While



        "Rich" wrote:
        [color=blue]
        > Is there any builtin functionality to the SqlDataReader for getting a row
        > count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?[/color]

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: SqlDataReader -- builtin get rowcount?

          Rich,

          This kind of procedures you can nicely do with a progressbar. And of couse
          do you than have to set the steps of the progressbar yourself. (something
          you do now)

          See this sample on our website


          I hope this gives an idea.

          Cor


          Comment

          • Ken Tucker [MVP]

            #6
            RE: SqlDataReader -- builtin get rowcount?

            Hi,

            If finding out the number of rows is really important
            executenonquery will return the number of rows affected. You still will
            have use executereader to get a datareader. Running the query twice will
            impact performance.

            Ken
            ---------------

            "Rich" wrote:
            [color=blue]
            > Is there any builtin functionality to the SqlDataReader for getting a row
            > count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?[/color]

            Comment

            • Rich

              #7
              RE: SqlDataReader -- builtin get rowcount?

              Basically, I have a table in sql server that has 4 columns/fields. Each
              column represents a different sql server table. I load parameter names in
              each column

              tbl1 tbl2 tbl3 tbl4
              @ID @ID @ID @ID
              @fld1tbl1 @fld1tbl2 @fld1tbl3 @fld1tbl4
              @fld2tbl1 @fld2tbl2 @fld2tbl3 @fld2tbl4
              .... @lastfid ... ....
              .... @lastfld ...
              @lastfld ...

              @lastfld

              Each table has different fields and different number of fields. I have 4
              separate Insert Into SPs for each table that take the respective parameters.
              The params are averageing from 30 params for one table to 180 params for
              another table. Too many params for an array of params (already tried - works
              but way too messy). So I decided to list my params for each table in a
              Params Table. I use the datareader to read each column for each table

              strSql = "Select col1 From tblParams Where col1 Is Not Null"

              for tbl1 for example. I loop through the data reader and load up my params.
              I can't even remember why I needed the count of rows. But I do need to know
              what row I am on because for col1 row3 the param has to be ntext, the rest of
              the rows are nvarchar(100), lastfld is int, for example.

              Rich

              "Ken Tucker [MVP]" wrote:
              [color=blue]
              > Hi,
              >
              > If finding out the number of rows is really important
              > executenonquery will return the number of rows affected. You still will
              > have use executereader to get a datareader. Running the query twice will
              > impact performance.
              >
              > Ken
              > ---------------
              >
              > "Rich" wrote:
              >[color=green]
              > > Is there any builtin functionality to the SqlDataReader for getting a row
              > > count of records retrieved? Rather than Do While rdr.Read: i+=1: Loop?[/color][/color]

              Comment

              • Rich

                #8
                Re: SqlDataReader -- builtin get rowcount?

                Thanks for the example. This will be very useful.

                Rich

                "Cor Ligthert [MVP]" wrote:
                [color=blue]
                > Rich,
                >
                > This kind of procedures you can nicely do with a progressbar. And of couse
                > do you than have to set the steps of the progressbar yourself. (something
                > you do now)
                >
                > See this sample on our website
                > http://www.windowsformsdatagridhelp....6-fc0d5c470f53
                >
                > I hope this gives an idea.
                >
                > Cor
                >
                >
                >[/color]

                Comment

                Working...