“DataSet 不支持 System.Nullable”,在将可空数据类型分配给表列时发生错误。

huangapple go评论78阅读模式
英文:

"DataSet does not support System.Nullable" while assigning a nullable datatype to a table column

问题

我需要在我的 DataTable 的某些列中放入空值。空值并不意味着 DBNull 值,而是严格的空值。因此,为了实现这个目的,我将列的数据类型分配如下:

myTable.Columns.Add("MyCol", typeof(decimal?));

但是编译器报错说 "DataSet does not support System.Nullable<>"。

是否有任何绕过这个问题的方法?我不想在插入表格值时用零代替空值,像这样:

myRow["MyCol"] = val == null ? 0m : val;

因为在我的程序中,插入的值是否为 null 对结果有影响。我也不能在插入值时用 DBNull 代替 null,像这样:

myRow["MyCol"] = val == null ? DBNull.Value : (decimal?)val;

因为编译器报错 "error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'decimal?'。

我搜索了一下这个问题,并找到了两个类似的案例:

https://bytes.com/topic/c-sharp/answers/514233-adding-nullable-datetime-column-datatable-does-not-work

https://www.tek-tips.com/viewthread.cfm?qid=1542262

然而,这两个案例都没有一个适用于我的解决方法。

因此,我唯一能想到的选项是在值为 null 时不分配值:

if (val != null) myRow["MyCol"] = val;

这是唯一的方法吗?而且是否真的没有办法使 DataSet 支持 System.Nullable,以便以下代码成为有效?

myTable.Columns.Add("MyCol", typeof(decimal?));
英文:

I need to put empty values in some column of my DataTable. Empty values does not mean DBNull values but strict nulls. So, for this purpose I assign column's datatype as follows:
myTable.Columns.Add(&quot;MyCol&quot;, typeof(decimal?));
But the compiler says "DataSet does not support System.Nullable<>".

Is there any way around this? I don't want to substitute zeroes for nulls while inserting values in the table, like this:
myRow[&quot;MyCol&quot;] = val == null ? 0m : val;
because it makes a difference in my program whether inserted value has been null or not. I also cannot substitute DBNulls for nulls while inserting values, like this:
myRow[&quot;MyCol&quot;] = val == null ? DBNull.Value : (decimal?)val;
because compiler says "error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'decimal?'

I googled for this issue and found just two similar cases:
https://bytes.com/topic/c-sharp/answers/514233-adding-nullable-datetime-column-datatable-does-not-work
https://www.tek-tips.com/viewthread.cfm?qid=1542262
Both, however, do not have a solution that would work for me.

So, the only option I could invent was NOT assigning the value when it is null:

if (val != null) myRow[&quot;MyCol&quot;] = val;

Is this the only way? And is there really no way to make DataSet support System.Nullable, so that code
myTable.Columns.Add(&quot;MyCol&quot;, typeof(decimal?));
be valid?

答案1

得分: 1

感谢JHBonarius,我从其中一个回答中得到了主要思路:在System.Data.DataTable中将列定义为可空。思路是将一切都强制转换为对象。首先,无需将表列的AllowDBNull属性设置为true,因为默认情况下它为true。其次,我在这行代码myRow[&quot;MyCol&quot;] = val == null ? DBNull.Value : (decimal?)val;上遇到了编译器错误CS0173。解决方法是将两个部分都强制转换为对象,像这样:myRow[&quot;MyCol&quot;] = val == null ? (object)DBNull.Value : (object)val 这样可以无错误地运行,并将DBNull值插入表列。就是这样。

英文:

Thanks to JHBonarius, I got the main idea from one of those answers: Define a column as nullable in System.Data.DataTable The idea is to cast everything to object. First of all, there is no need to set table column's AllowDBNull property to true, since it is true by default. Second, I stuck with the line of code myRow[&quot;MyCol&quot;] = val == null ? DBNull.Value : (decimal?)val; because of compiler error CS0173. The solution here is to cast both parts to object, like this: myRow[&quot;MyCol&quot;] = val == null ? (object)DBNull.Value : (object)val This works without error, and DBNull value is inserted in the table column. That's it.

huangapple
  • 本文由 发表于 2023年8月10日 17:28:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定