Posts

Showing posts with the label SQLDataReader

Donate

How Get Total Number Of Rows From SQLDataReader Object Using C#

Good Evening Gents! In the office, we were using SQLDataReader class to read from SQL Server database in a straight-forward manner. The SQLDataReader has several properties that are commonly used such as HasRows and FieldCount. However in one of my task, I want to retrieve the total number of rows returned from the SQLDataReader object and there's no such property that support this. Doing some search led me to this link How to get number of rows using SqlDataReader in C# . The one that worked for me was the answer using select @@ROWCOUNT query. I modified his answer by writing my own method that will accept a List of SQLParameters given that the original query requires parameters. private static void GetTotalRowCount( string query, string connectionString, ref int totalRows, List<SqlParameter> sqlParameters) { try { using ( var sqlCon = new SqlConnection(connectionString)) { sqlCon.Open(); var cmd = sqlCon.CreateCommand(); cmd.CommandText = q

Donate