“无法隐式转换类型“System.Guid”? 到“System.Guid”。 - 可为空的 GUID

发布于 2024-07-07 02:36:44 字数 222 浏览 10 评论 0原文

在我的数据库中,在其中一个表中,我有一个允许空值的 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 技术交流群。

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

发布评论

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

评论(10

秋凉 2024-07-14 02:36:44

ADO.NET API 在处理可为空值类型时存在一些问题(即它根本无法正常工作)。 我们遇到了无穷无尽的问题,因此得出的结论是最好手动将值设置为 null,例如,

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

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.

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.

还给你自由 2024-07-14 02:36:44

好的; 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? ;-p

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

这个俗人 2024-07-14 02:36:44

与格雷格·比奇的回答相同

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

same as Greg Beech's answer

myNewRow.myGuidColumn = (object)myGuid ?? DBNull.Value
骷髅 2024-07-14 02:36:44

您必须将 null 转换为 nullable Guid,这对我来说是这样的:

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

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

myRecord.myGuidCol = (myGuid == null) ? (Guid?)null : myGuid.Value
风为裳 2024-07-14 02:36:44

在您希望它为空的地方尝试 System.Guid.Empty

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

甚是思念 2024-07-14 02:36:44

如果您想避免在 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.

故事灯 2024-07-14 02:36:44

或者:

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

or:

    internal static T CastTo<T>(object value)
    {
        return value != DBNull.Value ? (T)value : default(T);
    }
单调的奢华 2024-07-14 02:36:44

您可以使用辅助方法:

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; }
    }
}

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; }
    }
}
半窗疏影 2024-07-14 02:36:44

如果您喜欢扩展方法...

/// <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);
}

那么您可以说:
myNewRow.myGuidColumn = myGuid.GetValueOrDBNull();

注意:当 myGuid == Guid.Empty 时,这将插入 null,如果您想允许为空,您可以轻松调整该方法您专栏中的指南。

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.

无边思念无边月 2024-07-14 02:36:44
Guid? _field = null;
if (myValue!="")//test if myValue has value
{
_field = Guid.Parse(myValue)
}
Guid? _field = null;
if (myValue!="")//test if myValue has value
{
_field = Guid.Parse(myValue)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文