Solved

How do I use the ExecuteScalar to count number of  rows in my dB

Posted on 2008-10-02
9
714 Views
Last Modified: 2013-12-17
Im trying to excite a scalar comment, to do a count of records but cant get it to work
I  dont know how to write the command.

Thanks for any help!

Code I have:

SqlConnection sqlConnection1 = new SqlConnection("Data Source=s...string works");
        SqlCommand cmd = new SqlCommand();
       
        //OpenFile connection
        sqlConnection1.Open();

        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection1;
        myv = (int)cmd.ExecuteScalar();
        // close connection
        sqlConnection1.Close();
0
Comment
Question by:bmanmike39
  • 3
  • 3
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 250 total points
ID: 22627914
your select query:
SELECT COUNT(*) FROM myTable
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 250 total points
ID: 22627989
try this

        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection1;
        cmd.CommandText = "SELECT COUNT(*) FROM myTable";
        myv = (int)cmd.ExecuteScalar();
        // close connection
        sqlConnection1.Close();
0
 

Author Comment

by:bmanmike39
ID: 22628165
It works but it returns 8 rows when i only have 7 records
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 22

Expert Comment

by:prairiedog
ID: 22628191
Do you have duplicate records in DB?
0
 

Author Comment

by:bmanmike39
ID: 22628231
no counted them just 7
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22628251
Try this:
SELECT DISTINCT COUNT(*) FROM myTable
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22628320
run the commandtext query from your commamd object in the database and then check the results
actually they should match
0
 

Author Comment

by:bmanmike39
ID: 22628324
sorry i was looking at the wrong table.  thanks it works
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 29
encoding an href string 2 20
C# bracket error 3 30
insert value of checklistbox checked 4 24
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now