我试图从SQL Server表中返回rowcount。 '网上的多个来源显示下面是一个可行的方法,但它继续返回'0行'。当我在management studio中使用该查询时,它工作正常并正确返回rowcount。我只用简单的表名以及管理工作室倾向于喜欢的完全合格的名称来尝试它。C#'select count'sql命令错误地从sql server返回零行
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());
}
什么可能会导致它的任何建议?
'noprimarykey2'是表格还是视图?我之所以问,是因为它可能是一个视图,由当前用户过滤数据。 – 2011-06-13 02:44:53
这是一张桌子,我为了清晰起见将它重新命名。谢谢 – Glinkot 2011-06-13 02:46:09
@Glinkot - 你确定你连接到Management Studio和代码中的同一个SQL Server实例? – 2011-06-13 02:48:19