Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get retrived records count from OleDbDataReader in C#?

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.

like image 631
Bokambo Avatar asked May 09 '11 05:05

Bokambo


2 Answers

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();
    }
like image 135
Pranay Rana Avatar answered Sep 29 '22 01:09

Pranay Rana


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"];
like image 37
Andomar Avatar answered Sep 29 '22 01:09

Andomar