在之前的问题中:
我遇到了以下代码问题:
client_group_details.Add(new ClientGroupDetails(
reader.GetString(Col2Index),
reader.GetString(Col3Index)));
我遇到了以下错误:
Data is Null. This method or property cannot be called on Null values.
这个问题可以使用以下代码解决:
client_group_details.Add(new ClientGroupDetails(
reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index)));
我现在遇到了类似的问题,涉及到GetDateTime
和GetInt32
,例如:
client_group_details.Add(new ClientGroupDetails(
reader.GetString(Col2Index),
reader.GetString(Col3Index),
reader.GetDateTime(Col4Index)));
我尝试使用以下方法解决这个问题,但它没有起作用。
client_group_details.Add(new ClientGroupDetails(
reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index),
reader.IsDbNull(Col2Index) ? null : reader.GetDateTime(Col4Index)));
它报错了:
Compiler Error Message: CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'
在寻找解决方案后,我找到了一个链接:Nullable type issue with ?: Conditional Operator。但是当我尝试使用该代码时,一直收到“) expected”错误提示。如何解决这个问题呢?