可空诠释列在DataSet中 [英] Nullable Int Column in DataSet

查看:202
本文介绍了可空诠释列在DataSet中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正与.NET的强类型数据集,并有一个可为空INT列(和一个可空日期时间列以及)的表。

I'm working with .NET strongly-typed datasets and have a table with a nullable int column (and a nullable DateTime column as well).

显然有一个与数据集设计是$有这些数据类型的空列p $ pvents的错误。设计者只允许抛出异常为空值的默认行为。不幸的是,当数据库中使用可空数据类型,空值是一个合法的值,而是试图从中检索数据行此值时,将导致抛出异常。

Apparently there is a bug with the dataset designer that prevents having nullable columns on these data types. The designer only allows "throw exception" as the default behavior for null values. Unfortunately, when using a nullable data type in the database, a null value IS a legitimate value but results in a thrown exception when attempting to retrieve this value from a data row.

我已经看到了这个问题的几个新闻组发布,但还没有看到任何像样的解决方法,此问题。

I've seen several newsgroup postings about this problem but have yet to see any decent workarounds to this issue.

我很想听听其他人如何处理这个问题。

I'd love to hear how others have dealt with this problem.

感谢。

推荐答案

为DBNull基本上淋漓尽致的发挥,以应对非空值类型,pre .NET 2.0。由于ADO.NET的设计,有没有办法可以避开DBNull的,除非你选择了一个更直接的方法。为DBNull是内置ADO.NET的核心,所以你必须要学会忍受的是,如果你想继续使用它。

DBNull was basically brought in to play to deal with non nullable value types, pre .NET 2.0. Due to the design of ADO.NET, there's no way you can avoid DBNull, unless you chose a more direct approach. DBNull is built-in the core of ADO.NET, so you'll have to learn to live with that, if you want to keep using it.

如果您提供依托通用System.Data命名空间的你自己的数据传输对象,而不是,您可以检查(在阅读中的结果与数据读取器),如果该值为空,但你需要一些方法来生成强类型对象和映射,因为这是很乏味的工作。

If you provide your own data transport objects instead of relying on the generic System.Data namespace, you can check (while reading in the results with a data reader) if the value is null, but you'll need some way to generate strongly typed objects and mappings because that's really tedious work.

要我所知的范围内,为DBNull内置在ADO.NET的设计,如果您使用的是凝聚(正常化)为DBNull和空来构建你的应用程序的最佳途径。基本上,提供一种拦截的DBNull并返回实际的空引用值是否为DBNull自己DbConvert类。这是一个最低要求,但一旦这样做了,你就会有更少的DBNull值漂浮担心。

To the extent of my knowledge, DBNull is built in to the design of ADO.NET and the best way to build your apps if you use that, is to coalesce (normalize) DBNull and null. Basically, provide your own DbConvert class which intercepts DBNull and returns an actual null reference if the value is DBNull. This is a minimal requirement, but as soon as that's done you'll have less DBNull values floating around to worry about.

这篇关于可空诠释列在DataSet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆