Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 'select count' sql command incorrectly returns zero rows from sql server

I'm trying to return the rowcount from a SQL Server table. Multiple sources on the 'net show the below as being a workable method, but it continues to return '0 rows'. When I use that query in management studio, it works fine and returns the rowcount correctly. I've tried it just with the simple table name as well as the fully qualified one that management studio tends to like.

            using (SqlConnection cn = new SqlConnection())
            {
                cn.ConnectionString = sqlConnectionString;
                cn.Open();

                SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM [LBSExplorer].[dbo].[myTable]", cn);
                countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
                Console.WriteLine("Starting row count: " + countStart.ToString());
            }

Any suggestions on what could be causing it?

like image 333
Glinkot Avatar asked Jun 13 '11 02:06

Glinkot


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Here's how I'd write it:

using (SqlConnection cn = new SqlConnection(sqlConnectionString))
{
    cn.Open();

    using (SqlCommand commandRowCount
        = new SqlCommand("SELECT COUNT(*) FROM [LBSExplorer].[dbo].[myTable]", cn))
    {
        commandRowCount.CommandType = CommandType.Text;
        var countStart = (Int32)commandRowCount.ExecuteScalar();
        Console.WriteLine("Starting row count: " + countStart.ToString());
    }
}
like image 60
Alex R. Avatar answered Nov 11 '22 10:11

Alex R.


Set your CommandType to Text

command.CommandType = CommandType.Text

More Details from Damien_The_Unbeliever comment, regarding whether or not .NET defaults SqlCommandTypes to type Text.

If you pull apart the getter for CommandType on SqlCommand, you'll find that there's weird special casing going on, whereby if the value is currently 0, it lies and says that it's Text/1 instead (similarly, from a component/design perspective, the default value is listed as 1). But the actual internal value is left as 0.

like image 28
Brian Webster Avatar answered Nov 11 '22 10:11

Brian Webster