Nullable GUID

后端 未结 10 2196
北荒
北荒 2020-12-15 04:36

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 s

10条回答
  •  轮回少年
    2020-12-15 04:44

    OK; how is myGuidColumn defined, and how is myGuid defined?

    If myGuid is Guid? and myGuidColumn is Guid, then the error is correct: you will need to use myGuid.Value, or (Guid)myGuid to get the value (which will throw if it is null), or perhaps myGuid.GetValueOrDefault() to return the zero guid if null.

    If myGuid is Guid and myGuidColumn is Guid?, then it should work.

    If myGuidColumn is object, you probably need DBNull.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

提交回复
热议问题