我在Excel中的数据库中使用OleDbCommand
运行查询,包含以下列:
name, city, inhabitants
我的代码:
Cmd = new OleDbCommand();
Cmd.Connection = Conn;
Cmd.Parameters.AddWithValue("@city", city);
Cmd.CommandText = "Select City, count(habitants) as h from [Sheet1$] where city = @city group by city";
var Reader = await Cmd.ExecuteReaderAsync();
我收到此错误:
OleDbException:您尝试执行不包含指定表达式“city”的查询作为聚合函数的一部分。
如果它包含city列,为什么会出现此错误?
您可以通过在查询中添加GROUP BY
来解决此错误:
Cmd.CommandText = "Select City, count(habitants) as h from [Sheet1$] where city=@city group by City";
使用FIRST()
函数为city
:
Cmd.CommandText = "SELECT FIRST(city) AS city, COUNT(habitants) AS h FROM [Sheet1$] WHERE city=@city GROUP BY city";