C#的SELECT COUNT“SQL命令错误地返回从SQL Server零行 [英] C# 'select count' sql command incorrectly returns zero rows from sql server

查看:289
本文介绍了C#的SELECT COUNT“SQL命令错误地返回从SQL Server零行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从SQL Server表中返回的行数。对多种来源的净展示以下作为一个可行的方法,但它仍然返回0行。当我使用的查询管理工作室,它工作正常,并正确返回的行数。我试过它只是用简单的表名以及完全限定的一个管理工作室没事就喜欢。

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?

推荐答案

设置你的CommandType为Text

command.CommandType = CommandType.Text

详细信息 Damien_The_Unbeliever 的意见,关于是否.NET默认SqlCommandTypes输入文本。

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

如果您拉开了getter方法​​的CommandType上的SqlCommand,你会发现,有怪异的特殊外壳事情,因此如果该值当前为0,它位于并说,它的文/ 1,而不是(同样,从组件/设计的角度来看,默认值被列为1)。但实际内在价值被置为0。

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.

这篇关于C#的SELECT COUNT“SQL命令错误地返回从SQL Server零行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆