简体   繁体   English

当我尝试通过C#访问中运行查询时出现错误

[英]i get error when i try to run query in access through C#

i have this query: 我有这个查询:

SELECT DO3Tbl.CodeDip, DO3Tbl.BarcodeD, Sum(DO3Tbl.Qty) AS Qty,
       Max(DO3Tbl.Mly) AS Mly, [Qty]-[Mly] AS Tot, ABS(Tot) AS TotAbs

when i run this query in access its work's excellent 当我在访问中运行此查询时,它的工作非常出色

but when i run this query in C# code like this: 但是当我使用C#代码运行此查询时,如下所示:

SQL =     SELECT DO3Tbl.CodeDip, DO3Tbl.BarcodeD, Sum(DO3Tbl.Qty) AS Qty,
          Max(DO3Tbl.Mly) AS Mly, [Qty]-[Mly] AS Tot, ABS(Tot) AS TotAbs 
Cmd = new OleDbCommand(SQL, Conn);
            Cmd.ExecuteNonQuery();
            Cmd.Dispose();

i get this error: 我收到此错误:

You tried to execute a query that does not include the specified expression '[Qty]-[Mly]' as part of an aggregate function. 您试图执行不包含指定表达式'[Qty]-[Mly]'作为聚合函数一部分的查询。

There is quite a lot wrong with the query as far as Access is concerned, for example, you are not selecting from any table, also, you cannot use an Alias that is the sames as a field name. 就Access而言,查询存在很多错误,例如,您没有从任何表中进行选择,而且,您不能使用与字段名称相同的别名。 How about: 怎么样:

SELECT DO3Tbl.CodeDip, DO3Tbl.BarcodeD, Sum(DO3Tbl.Qty) AS SumOfQty, Max(DO3Tbl.Mly) AS MaxOfMly, Sum([Qty]-[Mly]) AS Tot, Abs(Tot) AS TotAbs
FROM DO3Tbl
GROUP BY DO3Tbl.CodeDip, DO3Tbl.BarcodeD;

I propose that instead of counting the 我建议不要计算

Tot and TotAbs Tot和TotAb

values directly in the query. 值直接在查询中。

It's better to count them in the code since we are able to avoid any issue related to the query execution order in the SELECT clause. 最好将它们计入代码中,因为我们能够避免任何与SELECT子句中的查询执行顺序有关的问题。

Hence your query would be like that: 因此,您的查询将是这样的:

SELECT DO3Tbl.CodeDip, DO3Tbl.BarcodeD, Sum(DO3Tbl.Qty) AS Qty,
       Max(DO3Tbl.Mly) AS Mly

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试编译时出现C#错误 - C# Error when I try to compile 当我尝试从C#Windows应用程序运行ssis软件包时,我收到一条失败消息。 - I get a failure message when i try to run a ssis package from C# windows app.. 我尝试通过C#向Access数据库插入日期时出现“INSERT INTO语句中的语法错误”错误 - “Syntax error in INSERT INTO statement” error when I try to insert date to Access database by C# 当我尝试通过JS访问ViewData时遇到编译错误 - Getting a compilation error when I try and access ViewData through JS 如何运行通过C#在Access中创建表的查询? - How do I run a query that makes table in Access through C#? 当我尝试以 C# window 形式运行时,.exe 文件出现错误 - .exe file gives error when I try to run it in C# window form 尝试连接数据库时出现“错误:无法初始化OLE”? C# - I get an “Error: Cannot initialize OLE” when I try to connect to a database? C# 尝试连接数据库时出现“错误:无法初始化OLE”? C# - I get an “Error: Cannot initialize OLE” when I try to connect to a database? C# C# - 当我尝试创建构造函数时,出现 CS7036 错误 - C# - When i try to create constructor, i get CS7036 error Unity c# null 在我尝试访问实体组件数据时构建到我的 Iphone 时出现参考错误 - Unity c# null reference error when I build to my Iphone when I try to access an entities component data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM