6

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
  • 6,800
  • 2
  • 23
  • 46
user662285
  • 3,436
  • 22
  • 68
  • 100

3 Answers3

8

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
  • 164,177
  • 33
  • 228
  • 256
  • 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 ? – user662285 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
  • 216,619
  • 41
  • 352
  • 379
0

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

int i = 0;
While (dbReader.Read()){
   i++;
}
kaveman
  • 3,969
  • 22
  • 43
  • 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