Nullable GUID

≡放荡痞女 提交于 2019-12-18 02:02:14

问题


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'."


回答1:


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

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 of NULL for the value so I'd recommend the same approach when building parameters.




回答2:


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




回答3:


same as Greg Beech's answer

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



回答4:


You have to cast null to a nullable Guid, this how it worked for me :

myRecord.myGuidCol = (myGuid == null) ? (Guid?)null : myGuid.Value



回答5:


Try System.Guid.Empty where you want it to be null




回答6:


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.




回答7:


or:

    internal static T CastTo<T>(object value)
    {
        return value != DBNull.Value ? (T)value : default(T);
    }



回答8:


You can use a helper method:

public static class Ado {
    public static void SetParameterValue<T>( IDataParameter parameter, T? value ) where T : struct {
        if ( null == value ) { parameter.Value = DBNull.Value; }
        else { parameter.Value = value.Value; }
    }
    public static void SetParameterValue( IDataParameter parameter, string value ) {
        if ( null == value ) { parameter.Value = DBNull.Value; }
        else { parameter.Value = value; }
    }
}



回答9:


If you are into extension methods...

/// <summary>
/// Returns nullable Guid (Guid?) value if not null or Guid.Empty, otherwise returns DBNull.Value
/// </summary>
public static object GetValueOrDBNull(this Guid? aGuid)
{
  return (!aGuid.IsNullOrEmpty()) ? (object)aGuid : DBNull.Value;
}

/// <summary>
/// Determines if a nullable Guid (Guid?) is null or Guid.Empty
/// </summary>
public static bool IsNullOrEmpty(this Guid? aGuid)
{
  return (!aGuid.HasValue || aGuid.Value == Guid.Empty);
}

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.




回答10:


Guid? _field = null;
if (myValue!="")//test if myValue has value
{
_field = Guid.Parse(myValue)
}


来源:https://stackoverflow.com/questions/211436/nullable-guid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!