尝试通过数据适配器检索一些数据并最终解析为json。但count(*)操作总是返回0.当直接在DB中运行相同的查询时,我会收到正确的值。
我只看到了使用ExecuteScalar函数检索的SQL count(*)函数的示例,因此不确定这是否是我唯一的选择。
try
{
using (SqlConnection connection = new SqlConnection(ConnectionStr))
{
connection.Open();
using (SqlCommand sqlCommand = new SqlCommand())
{
sqlCommand.Connection = connection;
sqlCommand.CommandText="SELECT 'Year > 2010' AS YEAR_BUILT, COUNT(*) AS VALUE " +
" FROM STRUCTURE AS STRUCTURE " +
" WHERE (YR > 2010) " +
" UNION ALL " +
" SELECT 'Year Between 2000 - 2010' AS YEAR_BUILT, COUNT(*) AS VALUE " +
" FROM STRUCTURE AS STRUCTURE " +
" WHERE (YR BETWEEN 2000 AND 2010) " ;
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand))
{
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable); // Value always returned as 0 .
sqlCommand.Dispose();
return dataTable;
}
}
}
}
.....