可空GUID [英] Nullable GUID

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

问题描述

在我的数据库,表中的之一,我有允许空值GUID列。我有一个GUID的方法?参数,在表中插入一个新的数据行。然而,当我说myNewRow.myGuidColumn = myGuid我得到以下错误:无法隐式转换类型'?的System.Guid'到'的System.Guid'

In my database, in one of the table I have a GUID column with allow nulls. I have a method with a Guid? parameter that inserts a new data row in the table. However when I say myNewRow.myGuidColumn = myGuid I get the following error: "Cannot implicitly convert type 'System.Guid?' to 'System.Guid'."

推荐答案

在ADO.NET API有一些问题,当涉及到处理空值类型(也就是说它根本无法正常工作)。我们已经没有任何与它的问题结束,因此在得出结论,最好手动设置值设置为null,如:

The ADO.NET API has some problems when it comes to handling nullable value types (i.e. it simply doesn't work correctly). We've had no end of issues with it, and so have arrived at the conclusion that it's best to manually set the value to null, e.g.

myNewRow.myGuidColumn = myGuid == null ? (object)DBNull.Value : myGuid.Value



这是ADO.NET应该处理痛苦的额外的工作,但它似乎并没有这样做可靠(即使在3.5 SP1)。这至少可以正常工作。

It's painful extra work that ADO.NET should handle, but it doesn't seem to do so reliably (even in 3.5 SP1). This at least works correctly.

我们还看到问题与传递空值类型为SqlParameters如果生成的SQL包含关键字默认而不是 NULL 所以我建立参数时,建议相同的方法。

We've also seen issues with passing nullable value types to SqlParameters where the generated SQL includes the keyword DEFAULT instead of NULL for the value so I'd recommend the same approach when building parameters.

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

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