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
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
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
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.
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.
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 }