Solved

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

Posted on 2008-10-02
9
729 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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