语法错误:“[0]”运算符后缺少操作数。

XYZuse 2019-01-26 04:58:10
我有个Access数据库,里面有三张表,现在使用内连接查询出某个字段(StartTim)中大于某个值的所有数据,结果如下:


因为是使用内连接多表查询,结果中有一些重复内容的字段,我在 C# 代码里面需要统计图中 StartTim 字段下 等于 43477.5736132407 这个浮点数的行数有多少,因为数据量太大,不想每次都从数据库里查询,先把查询的结果存在一张 DataTable (dt) 中,然后用 dt.Compute("COUNT("StartTim")", "StartTim = dt.Rows[0][\"StartTim\"]") 这种方式和 dt.Select("StartTim = dt.Rows[0][1]").Count() 都试过,一直报错 : 语法错误:“[0]”运算符后缺少操作数。

这是我的测试代码,刚接触C# 还不熟练,请大佬们帮忙看下是哪里的问题。

using(OleDbConnection conn = GetOleDbConnection())
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(queryStr, conn);
OleDbDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);

DataTable dt = new DataTable();
dt.Load(rd);
rd.Close();
conn.Close();
Console.WriteLine(dt.Rows[0]["StartTim"].ToString()); //43477.5736132407

double a = Convert.ToDouble(dt.Rows[0]["StartTim"]);

//int b = Convert.ToInt32(dt.Select("StartTim = 43477.5736132407").Count()); //运行正常

//int x = Convert.ToInt32(dt.Compute("count(StartTim)", "StartTim = dt.Rows[0][1]")); //报错

int y = Convert.ToInt32(dt.Select("StartTim = dt.Rows[0][1]").Count()); //报错

Console.WriteLine(string.Format("{0}\n{1}", a, y));

Console.ReadKey();
}
...全文
396 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
XYZuse 2019-01-26
  • 打赏
  • 举报
回复
已经解决了,原来是 select 里面变量的 单引号 和 双引号 的原因!
XYZuse 2019-01-26
  • 打赏
  • 举报
回复
不知道为什么看不到图片,我打出来前面一部分数据

ID StartTim
745 43477.5736132407
740 43477.5736132407
741 43477.5736132407
742 43477.5736132407
743 43477.5736132407
744 43477.5736132407
746 43477.5736132407
705 43477.5716532755
710 43477.5716532755
709 43477.5716532755
708 43477.5716532755
706 43477.5716532755
711 43477.5716532755
707 43477.5716532755
701 43477.5696920486
699 43477.5696920486
700 43477.5696920486

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧