Search code examples
c#databaserecordscounting

Counting data in ACCESS database?


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 .


Solution

  •    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))
            {
                con.Open();
                OleDbDataReader DB_Reader = Command.ExecuteReader();
                if (DB_Reader.HasRows)
                {
                    DB_Reader.Read();
                    int id = DB_Reader.GetInt32(0);
                }
            }