- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个查询,我在其中尝试获取总和值,但我得到了 InvalidCastException。
我的查询是:
SELECT e.clockNr,e.firstName,e.LastName,e.unionName,i.points
FROM (
SELECT employee.clockNr AS clockNr,
employee.firstName AS firstName,
employee.lastName AS lastName,
Unions.name AS unionName
FROM employee,Unions
WHERE employee.active=1 AND employee.unionId = unions.id
GROUP BY employee.clockNr
) e LEFT JOIN (
SELECT infraction.clockNr AS clockNr,
CAST(SUM(Infraction.points) AS SIGNED) AS points
FROM infraction
WHERE infraction.infractionDate >=@startDate
AND infraction.infractionDate <=@endDate
GROUP BY infraction.clockNr
) i ON e.clockNr = i.clockNr
ORDER BY e.clockNr ASC
它是错误的'点数'列。我已将 CAST 添加到 SIGNED,但这没有帮助。
我读专栏的方式是:
int iGetPoints = Convert.ToInt32(reportReader["points"]);
还试过:
int iGetPoints = (int)reportReader["points"];
但两者都会引发 InvalidCastException。该查询已在 PHPMyAdmin 中进行测试并且可以正常工作。
任何人都可以看到我做错了什么或给我提示在哪里寻找吗?
最佳答案
因为 points
列是左连接的一部分,所以它可以为空。我假设这就是问题所在。您需要测试 null 以避免强制转换异常:
// Note: this is for DataTableReader; see below for MySQL data reader
int iGetPoints = 0;
if (!reportReader.IsDBNull(reportReader.DBOrdinal("points"))) {
iGetPoints = Convert.ToInt32(reportReader["points"]);
}
IsDBNull
方法需要列名的索引(它不会与名称一起使用),因此调用 DBOrdinal
以从名称中获取索引.
注意:上面的答案适用于“通用”System.Data.DataTableReader
类,但不适用于 MySQL 数据读取器。 Gerard 在下面的评论中发布了 MySQL 读者所需的更改。它们是:
int iGetPoints = 0;
if (reportReader["points"] != DBNull.Value) {
iGetPoints = Convert.ToInt32(reportReader["points"]);
}
关于C# MySQL 求和 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18132481/
所以我有一个名为 OutputInformation 的类,我想存储它然后在另一台计算机上读取以检索数据。 我正在使用二进制序列化。 [Serializable()] public class Out
我一直在使用 Visual Studio 2010、C#。 我从事这个项目已经将近一年了。在其中,我使用存储在单独的 XML 文件中的信息来构建图像。这个过程已经为我工作了数百次,没有任何问题。 我已
我得到一个 InvalidCastException我不明白为什么。 这是引发异常的代码: public static void AddToTriedList(string recipeID) {
我有一个执行数据转储的 exe。 exe 将根据配置动态获取 DLL,并将类对象传递给它。 DLL 有一个用它编译的此类的副本,并且可以在调试下看到数据,没有问题作为对象。但是,当我尝试将它转换到类里
我基本上想做的是将从 Web 服务获取的对象转换为与我的 Entity Framework 对象兼容的对象。我虽然通过创建一个同时应用于 Web 服务对象和 EF 对象的接口(interface)来做
我有一个查询,其结果如下所示 Blue OrangeRed Black Green Silver Red 我打算从这个结果集中创建一个 System.Drawing.Color 对象的列表。当我运行该
我有一个 GridView ,当双击一条记录时,我希望它为该特定记录打开一个新的详细 View 表单。 例如,我创建了一个 Customer 类: using System; using System
我目前对我遇到的问题感到非常困惑。我正在为另一个提供公共(public) .NET API 的应用程序编写插件。我创建了一个名为 Room 的类,我使用 PropertyGrid 允许用户查看和编辑
我在具有自定义类的字典和列表中遇到了该异常。示例: List dsa = (List)Session["Display"]; 当我使用 Session 时,转换工作了 10-20 次..然后它开始抛
我在 Entity Framework Core 1.1 中遇到此查询的问题: var leaves = _context.Proposal.OfType() .Inc
当我在 NET Framework 4.7.2 中创建新的 WPF 应用程序时,将菜单添加到 MainWindow 时,每当您单击菜单然后单击菜单以外的位置将其关闭时,我都会收到 InvalidCas
我的一个程序集包含以下“提供者”类型: 我还有一个 XML 文件,它使用 DeviceInfoProvider 基类保存提供者信息。简化版本如下所示: 在运行时,我将 XML 字段映射到我的变
我有一个扩展方法,我在 WinForms 中使用了多年,但自从我尝试在新的 WPF 项目中使用它后就没有了。方法: public static String GetDescription(this E
我在以下代码的运行时收到“InvalidCastException”: 我的 C# WinForm 代码包含一个组合框,它使用以下代码从数据库中填充: public void PopulateComp
下面的代码给了我一个 InvalidCastException,说明我不能在 foreach 循环中从源类型转换到目标类型。我尝试通过该方法传递多个不同的通用集合,但我总是收到此错误。我不知道为什么。
(问题的真正标题应该是“为什么我得到一个‘无法转换类型为‘System.Runtime.CompilerServices.TaskAwaiter`1[System.Runtime.CompilerSe
我在 C# 中有以下代码片段 List list = new List() { 1, 23, 5, 3, 423, 3 }; var query = list.Cast().S
这个特定问题更多的是试图找出编译器消息背后的原因,而不是解决问题,但我希望它没问题。 假设我们有: class Foo { public static explicit operator Fo
我想统计某个时间段之间的记录数。 我使用 Npgsql 准备查询,然后添加变量部分(出于安全原因): lCommand.CommandText = "select count(*) from
Closed. This question is off-topic。它当前不接受答案。
我是一名优秀的程序员,十分优秀!