Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How do I find the record count using Ole in C#?

Posted on 2004-08-28
Medium Priority
Last Modified: 2012-06-27

I'm using OleDbConnection and OleDbCommand objects, but seem to have forgotten how to check the record count.  Do I use the OleDbDataReader?

Question by:ba272
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11924776
  Do it using Sql statement as "Select Count(*) from mytable where ...."

using OleDBCommand and call ExecuteScalar method, which return the first value, this way is the fastest and give you the best performance, other way like filling Dataset will require from you to return all records to know it's count


LVL 10

Expert Comment

ID: 11928835

DataSet dataset1 = SqlCommand.ExecuteDataset();

int recordCount;

recordCount = dataset1.Tables[0].rows.count();

LVL 10

Accepted Solution

123654789987 earned 1000 total points
ID: 11928840
Sorry.. I have mentioned the command for SqlConnection .For OledbConnection it has to be

DataSet dataset1 = OleDbCommand.ExecuteDataset();

int recordCount;

recordCount = dataset1.Tables[0].rows.count();
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11928872
if you have 1 million records, you will fill the dataset with the Million records to get the count???

using OleDbDataReader will not help in that, as I said before using select count(*) is better
LVL 10

Expert Comment

ID: 11928929
If the question is about just getting the Count of records then your solution is acceptable. But if the user wants to use the data from a particular table then why should he fire a separate query to get the count when the data has to be retrieved from the table.

I don't know what the requirement is exactly so let ba272 deceide based on his requirment
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11928934
he already mentioned that he used OleDbConnection and OleDbCommand

Author Comment

ID: 11936256
I actually like Mohammed's approach, since it would empower me to do many more kinds of things than just get a row count.  I could use SQL to retrieve anything I want, and I'm sure the opportunity will arise.  But I don't know how to actually get a value back when I execute the SQL command.  Here's how far I've gotten...

OleDbConnection conn1 = new OleDbConnection();
conn1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
              @"Data source=c:\data\datafile.mdb";

OleDbCommand dbSelectCommand1 = new OleDbCommand();
dbSelectCommand1.Connection = conn1;
string sql;
sql = "SELECT COUNT(*) FROM DataTable";
dbSelectCommand1. CommandText = sql;                        
OleDbDataReader reader1 = dbSelectCommand1.ExecuteReader();      

But where would I actually get the return value?  I'd most like an answer to this question.

I also did try the following approach to getting a row count, but the compiler would not swallow it.  I must have either done something wrong, or this would require the discussion of DataSets, which I don't have time to learn about at the moment.  

DataSet dataset1 = OleDbCommand.ExecuteDataset();

But the return value for an SQL intersts me greatly.  If anyone has the answer at their fingertips, I'd love to hear from you.


Expert Comment

by:Jeremy Daley
ID: 21065041
I'm simply not seeing an executeDataSet() method in .NET.

to get the row count, i've simply done this:

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=zipcodes.mdb";
OleDbCommand cmd = new OleDbCommand("Select Count(*) FROM Zips", conn);
int recordCount = (Int32)cmd.ExecuteScalar();

Open in new window


Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question