Take the 2-minute tour ×

I have 3 records in my database (database type : microsoft access ) . they are personal information of 3 Customers.

Now in interface I have button in the name of "Count Customers" . when I push it I want it to count records and says How many record(s) I have in Database ! . for exmple here I have 3 , it must say you have 3 records or 3 cutomers .

I use Visual Studio 2008 , language is C# . my database is Microsoft Access Database .

share|improve this question
You should not only state what you want, but also show what you have tried. –  CodeCaster Feb 8 '13 at 11:54
add comment

closed as not a real question by Thor, pradeek, Jon Egerton, Joel Potter, rekire Feb 8 '13 at 14:55


3 Answers

up vote 3 down vote accepted
   using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Test.mdb"))
        using (OleDbCommand Command = new OleDbCommand(" SELECT count (CustomerId) from Customer as total", con))
            OleDbDataReader DB_Reader = Command.ExecuteReader();
            if (DB_Reader.HasRows)
                int id = DB_Reader.GetInt32(0);
share|improve this answer
thank you . I use your code but it doesnt word , would you plz creat one and attach it so I can download it . thank you agian –  Hamed Parvini Feb 8 '13 at 12:41
what problem you are getting? –  Popeye Feb 8 '13 at 12:47
ALSO when I use GetInt it sys : Error 1 'System.Data.OleDb.OleDbDataReader' does not contain a definition for 'GetInt' and no extension method 'GetInt' accepting a first argument of type 'System.Data.OleDb.OleDbDataReader' could be found (are you missing a using directive or an assembly reference?) O:\C#\Shomareshe data\shomaresh data\shomaresh data\Form1.cs 39 47 shomaresh data –  Hamed Parvini Feb 8 '13 at 12:51
show 6 more comments

this simple query will return the total number of rows in a table

share|improve this answer
add comment
using (OledbConnection cn = new OledbConnection())
                cn.ConnectionString = OledbConnectionString;

                OledbCommand commandRowCount = new OledbCommand("SELECT COUNT(*) FROM [TABLENAME]", cn);
                countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
                MessageBox.Show("Starting row count: " + countStart.ToString());
share|improve this answer
thank you so much , but I want to use it for WP not for DOS , if you have time please change it for C# WP . –  Hamed Parvini Feb 8 '13 at 12:42
WP? This is the correct answer and nothing to do with DOS. It illustrates what you need to do. You can change the MessageBox.Show line to whatever you need. –  cjb110 Feb 8 '13 at 13:35
OK thank you , please see this errors and tell me what to do plz axofile.com/images/4ayh5dqyjb1t22i79yas.png –  Hamed Parvini Feb 8 '13 at 13:44
add comment
