“无法隐式转换类型“System.Guid”? 到“System.Guid”。 - 可为空的 GUID
在我的数据库中,在其中一个表中,我有一个允许空值的 GUID 列。 我有一个带有指南的方法吗? 在表中插入新数据行的参数。 但是,当我说 myNewRow.myGuidColumn = myGuid
时,我收到以下错误:
Cannot implicitly convert type 'System.Guid?' to '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'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
ADO.NET API 在处理可为空值类型时存在一些问题(即它根本无法正常工作)。 我们遇到了无穷无尽的问题,因此得出的结论是最好手动将值设置为 null,例如,
ADO.NET 应该处理这是一项痛苦的额外工作,但它似乎并没有这样做可靠(即使在 3.5 SP1 中)。 这至少可以正常工作。
我们还看到了将可为空值类型传递给 SqlParameters 的问题,其中生成的 SQL 包含关键字
DEFAULT
而不是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.
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.
We've also seen issues with passing nullable value types to SqlParameters where the generated SQL includes the keyword
DEFAULT
instead ofNULL
for the value so I'd recommend the same approach when building parameters.好的; myGuidColumn 是如何定义的,myGuid 是如何定义的?
如果 myGuid 是
Guid?
且 myGuidColumn 是Guid
,则错误是正确的:您需要使用myGuid.Value
或(Guid)myGuid
获取值(如果为 null,则会抛出异常),或者如果为 null,则使用myGuid.GetValueOrDefault()
返回零 guid。如果 myGuid 是
Guid
并且 myGuidColumn 是Guid?
,那么它应该可以工作。如果 myGuidColumn 是
object
,您可能需要DBNull.Value
而不是常规的 null。当然,如果该列确实可以为空,您可能只想确保它是 C# 代码中的
Guid?
;-pOK; how is myGuidColumn defined, and how is myGuid defined?
If myGuid is
Guid?
and myGuidColumn isGuid
, then the error is correct: you will need to usemyGuid.Value
, or(Guid)myGuid
to get the value (which will throw if it is null), or perhapsmyGuid.GetValueOrDefault()
to return the zero guid if null.If myGuid is
Guid
and myGuidColumn isGuid?
, then it should work.If myGuidColumn is
object
, you probably needDBNull.Value
instead of the regular null.Of course, if the column is truly nullable, you might simply want to ensure that it is
Guid?
in the C# code ;-p与格雷格·比奇的回答相同
same as Greg Beech's answer
您必须将
null
转换为nullable Guid
,这对我来说是这样的:You have to cast
null
to anullable Guid
, this how it worked for me :在您希望它为空的地方尝试 System.Guid.Empty
Try System.Guid.Empty where you want it to be null
如果您想避免在 C# 代码中使用可空 GUID(就我个人而言,我经常发现使用可空类型很麻烦),您可以在某处提前分配 Guid.Empty 到数据库中为 null 的 .NET 数据。 这样,您就不必费心处理所有 .HasValue 内容,只需检查
myGuid != Guid.Empty
即可。If you want to avoid working with nullable GUIDs in your c# code (personally, I often find it cumbersome to work with nullable types) you could somewhere early assign Guid.Empty to the .NET data which is null in the db. That way, you don't have to bother with all the .HasValue stuff and just check if
myGuid != Guid.Empty
instead.或者:
or:
您可以使用辅助方法:
You can use a helper method:
如果您喜欢扩展方法...
那么您可以说:
myNewRow.myGuidColumn = myGuid.GetValueOrDBNull();
注意:当
myGuid == Guid.Empty
时,这将插入 null,如果您想允许为空,您可以轻松调整该方法您专栏中的指南。If you are into extension methods...
Then you could say:
myNewRow.myGuidColumn = myGuid.GetValueOrDBNull();
NOTE: This will insert null when
myGuid == Guid.Empty
, you could easily tweak the method if you want to allow empty Guids in your column.