+1 vote
63 views

Hi, I am new in c# and I would like to know how to get record count of a mysql query with c#.
The code I am using is:

MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM `customer`;", Con);
var rd = cmd.ExecuteReader();

Here what to do with variabl rd to get count?

by (1.4k points)   | 63 views

1 Answer

0 votes

No Need to use DataReader. Please use this code:

var Con = new MySqlConnection("ConString");
try
   {

    Con.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM `customer`;", Con);

    int Count = init.Parse(cmd.ExecuteScalar().ToString());
    MessageBox.Show("Total Record(s): " + Count.ToString());
   }
catch(Exeption e)
    {
                    MessageBox.Show(e.ToString());
        }
by (3.3k points)  

Related questions

+1 vote
1 answer
asked May 26, 2020 by toheed (1.4k points) | 24 views
+1 vote
1 answer
asked May 26, 2020 by toheed (1.4k points) | 20 views
+1 vote
1 answer
0 votes
1 answer
+2 votes
1 answer
asked Jun 5, 2020 by toheed (1.4k points) | 32 views
+3 votes
1 answer
asked May 22, 2020 by athar (3.3k points) | 172 views
+2 votes
0 answers
asked Jun 20, 2020 by athar (3.3k points) | 33 views
+3 votes
1 answer
asked Apr 10, 2020 by admin (1.4k points) | 51 views
+3 votes
1 answer




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
131 questions
94 answers
44 comments
134 users