1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

find number of rows in data reader

Discussion in 'C#' started by rajeeilan, Oct 9, 2007.

  1. #1
    hi,

    i have to find out how many number of rows satisfies my condition. in asp dotnet c# coding

    ex: no of records having firstname="test"

    help me

    regards
     
    rajeeilan, Oct 9, 2007 IP
  2. kadesmith

    kadesmith Peon

    Messages:
    479
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Create a data table and put the rows in it. Then use a .rowfilter to filter out your condition. You will then be able to use the a .count
     
    kadesmith, Oct 10, 2007 IP
  3. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Why do all that? SQL is much more efficient at data manipulation than .Net (hence why we use SQL at all)

    Are you actually wanting the data or simply the count?

    If it just the count then use a SQLCommand and an SQL query simply to give the count and use the ExecuteScalar to return the value

    If you are wanting to actually have the data as well then filter with the SQL query and then fill a datatable with it. If it is a massive amount of data that is being returned it can be more efficient to execute two queries, one for the count and one for the data as holding the complete datatable in memory may be too big a overhead
     
    AstarothSolutions, Oct 11, 2007 IP
    Alexander the Great likes this.
  4. kadesmith

    kadesmith Peon

    Messages:
    479
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The reason you would use the datatable is because he asked how to find the number of rows in a data reader, he said nothing about using SQL to access info from a database.

    If the data is coming from a database then Astaroth's solution is best.
     
    kadesmith, Oct 12, 2007 IP
  5. Alexander the Great

    Alexander the Great Peon

    Messages:
    253
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    while(r.Read()) {
    i++;
    }

    After consuming the data, i holds the number of rows. If all you want is a count, this is hugely inefficient.
     
    Alexander the Great, Oct 20, 2007 IP
  6. teraeon

    teraeon Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Datareader has a forward only cursor which means you can't get the count. What you can do is the following;

    What you can do is use nextresult to do two executes one for the count and one for the records.

    stirng sSQL = "SELECT COUNT(*) as Counter FROM Table; SELECT * FROM Table";
    SqlCommand Cmd = new SqlCommand( sSQL, Conn);
    SqlDataReader RS = Cmd.ExecuteReader();
    int iCount = (int)RS["Counter"];
    RS.NextResult();
    while( RS.Read() ){
    //YOUR CODE HERE
    }
     
    teraeon, Nov 1, 2007 IP