7

I want to get the retrived records count from the OleDbDataReader in C# ?

strQuery = "SELECT * FROM Table_Name" ;                   
    dbCommand = new OleDbCommand(strQuery, dbConnection);
    dbReader = dbCommand.ExecuteReader();
    //Now how to get RowCount from the Table after this.

Any help is appreciated.

Thanks.

js1568
  • 7,012
  • 2
  • 27
  • 47
Bokambo
  • 4,204
  • 27
  • 79
  • 130

3 Answers3

9

For more detail : Get row count by 'ExecuteScalar'

Make use of ExecuteSclar() rather than going for read function.

SqlCommand cmd = new SqlCommand("SELECT count(*) FROM " + Table_Name, conn);
    try
    {
        conn.Open();
        int total = (Int32)cmd.ExecuteScalar();
    }
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • Thanks. Now i am using sqlBulkCopy to copy from MS Access to SQL DB . now i want to know that how many rows have been copied how i can achieve that ? – Bokambo May 09 '11 at 06:20
2

You could change the query to:

strQuery = "SELECT count(*) as RowCount, * FROM " + Table_Name;

That would allow you to retrieve the rowcount like:

dbReader.Read();
var rowCount = (int)dbRead["RowCount"];
Andomar
  • 232,371
  • 49
  • 380
  • 404
0

This will do it, but there are likely better ways:

int i = 0;
While (dbReader.Read()){
   i++;
}
kaveman
  • 4,339
  • 25
  • 44
  • it's a way that works, and doesn't modify the OP's query. If the OP actually needs data from records, then even better since processing can be done in the `While` loop – kaveman May 09 '11 at 06:01
  • 1
    but i admit it's not the most efficient to loop over the rows just to get the count – kaveman May 09 '11 at 06:02