+1 vote
14 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.2k points)   | 14 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.1k points)  

Related questions

+1 vote
1 answer
+1 vote
1 answer
asked May 27 by toheed (1.2k points) | 11 views
+1 vote
1 answer
0 votes
1 answer
+2 votes
1 answer
asked Jun 5 by toheed (1.2k points) | 13 views
+3 votes
1 answer
+2 votes
0 answers
asked Jun 20 by athar (3.1k points) | 13 views
+3 votes
1 answer
+1 vote
2 answers




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.
113 questions
85 answers
38 comments
103 users