Solved

Get the number of rows in database

Posted on 2012-03-27
1
269 Views
Last Modified: 2012-06-21
I am using vb.net and access 2007 database.

I need a code that can give me the number of rows that is in a database based on a query.

The command i used to get the query is by using OledbDataReader.

thanks in advance.
0
Comment
Question by:baabaa_nl
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37771433
use OleDbCommand.ExecuteScalar method.

Here is an example:-

public void ReadMyData(string myConnString) {
   string mySelectQuery = "SELECT count(*) FROM Orders";
   OleDbConnection myConnection = new OleDbConnection(myConnString);
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
   myConnection.Open();
   OleDbDataReader myReader;
   myReader = myCommand.ExecuteReader();
   // Always call Read before accessing data.
   while (myReader.Read()) {
      Console.WriteLine(myReader.GetInt32(0));
   }
   // always call Close when done reading.
   myReader.Close();
   // Close the connection when done with it.
   myConnection.Close();
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

821 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