数据绑定中的可为空数据类型?

发布于 2024-10-10 22:05:06 字数 136 浏览 3 评论 0原文

我在 C# 应用程序中使用类型化数据集与数据库进行通信。我的数据库支持并允许许多记录上存在空值。但是,尝试通过数据集访问空值似乎会导致强制转换异常。

是否可以使属性可为空(至少是可以在数据库中存储空值的属性)?如果不是的话为什么要这样设计呢?

I'm using Typed Data sets in my C# application to communicate with the database. My database supports and allows null values on many of the records. However, it seems that trying to access a nulled value through the dataset results in a Cast exception.

Is it possible to make the properties nullable (atleast the ones that can store a null value in the database)? If not why was it designed this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

善良天后 2024-10-17 22:05:07

是否可以使属性可为空

否,生成器工具不支持此操作。

如果不是的话为什么要这样设计?

数据集可追溯到 Fx 1.1 ,可空值类型可追溯到 Fx 2.0

当 Fx2 发布时,决定不更改类型化数据集框架(可能是出于成本和时间原因)。

从那时起,数据集框架就没有更新过,我认为在某个地方有一个官方声明,它们处于生命周期结束阶段。

Is it possible to make the properties nullable

No, the Generator tool does not support this.

If not why was it designed this way?

The Dataset dates back to Fx 1.1 , nullable value types to Fx 2.0

When Fx2 was released it was decided not to change the Typed Dataset framework (for cost and time reasons presumably).

The Dataset framework has not been updated since, I think there is an official statement somewhere they're in an end-of-life stage.

地狱即天堂 2024-10-17 22:05:07

In this answer - Typed DataSet nullable support they (MSFT) says type dataset doesn't support nullable values in dot net 4.0

一抹淡然 2024-10-17 22:05:07

是否可以使属性可为空
(至少可以在数据库中存储空值)?

虽然您无法在类型化 DataSet 中存储可为 null 的值类型,但您可以使数据集列接受 DBNull 值 (column.AllowDBNull = true; )并创建一对辅助函数来为您进行翻译。

    public static T? DBToNullable<T>(object dbValue) where T: struct 
    {
        if (dbValue == DBNull.Value)
            return null;
        else
            return (T)dbValue;
    }

    public static object NullableToDB<T>(T? value) where T: struct
    {
        if (value.HasValue)
            return (object)(T)value;
        else
            return DBNull.Value;
    }

然后可以像这样使用它们:

    int? value = ....
    DataRow row = ....

    row["MyDataColumn"] = NullableToDB(value);

    value = DBToNullable<int>(row["MyDataColumn"]);

这应该会稍微减轻疼痛。

如果不是的话为什么要这样设计?

它可能是出于历史原因而设计成这种方式的,这些历史原因与数据库 NULL 值(含义:该值未知)和 C# null 引用(含义:该参考尚未分配)。当然,在可为空值类型添加到 C# 后,这些含义发生了变化,但那时类型化 DataSet 的船已经起航了。

Is it possible to make the properties nullable
(atleast the ones that can store a null value in the database)?

Although you cannot store nullable value types in a typed DataSet, you can make the data set columns accept DBNull values (column.AllowDBNull = true;) and create a pair of helper functions that will do that translation for you.

    public static T? DBToNullable<T>(object dbValue) where T: struct 
    {
        if (dbValue == DBNull.Value)
            return null;
        else
            return (T)dbValue;
    }

    public static object NullableToDB<T>(T? value) where T: struct
    {
        if (value.HasValue)
            return (object)(T)value;
        else
            return DBNull.Value;
    }

They can then be used like this:

    int? value = ....
    DataRow row = ....

    row["MyDataColumn"] = NullableToDB(value);

    value = DBToNullable<int>(row["MyDataColumn"]);

That should ease the pain somewhat.

If not why was it designed this way?

It was probably designed this way for historical reasons that relate to the conceptual difference between a database NULL value (meaning: the value is unknown) and a C# null reference (meaning: this reference hasn’t yet been assigned). Of course, those meanings changed after nullable value types were added to C#, but by then the typed DataSet ship had already sailed.

鲜肉鲜肉永远不皱 2024-10-17 22:05:07

可以在 .NET 中创建 Nullable 值类型。您可以只使用 Nullable generic 或只是放置 ?使值类型可为空。我不确定设计者是否会为类型化数据集自动生成它们。请看
http://xtremebytes.blogspot.com/2010/12/nullable.html供参考

It is possible to create Nullable value types in .NET. You can just use Nullable generic or just put ? to make the value type nullable. I am not sure if the designer generates them automatically for a typed dataaset. Please look at
http://xtremebytes.blogspot.com/2010/12/nullable.html for reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文