Solved

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

Posted on 2004-08-28
8
2,827 Views
Last Modified: 2012-06-27
Hi,

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

Thanks.
0
Comment
Question by:ba272
8 Comments
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Hello
  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

HTH

Regards,
Mohammed
0
 
LVL 10

Expert Comment

by:123654789987
Comment Utility

DataSet dataset1 = SqlCommand.ExecuteDataset();

int recordCount;

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



0
 
LVL 10

Accepted Solution

by:
123654789987 earned 250 total points
Comment Utility
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();
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:123654789987
Comment Utility
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
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
he already mentioned that he used OleDbConnection and OleDbCommand
0
 

Author Comment

by:ba272
Comment Utility
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.


Thanks.
0
 
LVL 4

Expert Comment

by:daleyjem
Comment Utility
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";

conn.Open();

OleDbCommand cmd = new OleDbCommand("Select Count(*) FROM Zips", conn);

int recordCount = (Int32)cmd.ExecuteScalar();

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now