C# ADO.NET:空值和 DbNull —— 有没有更有效的语法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:37:37  来源:igfitidea点击:

C# ADO.NET: nulls and DbNull -- is there more efficient syntax?

c#ado.netnullnullabledbnull

提问by Stewart Johnson

I've got a DateTime?that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so:

我有一个DateTime?我正在尝试使用DbParameter. 我正在像这样创建参数:

DbParameter datePrm = updateStmt.CreateParameter();
datePrm.ParameterName = "@change_date";

And then I want to put the value of the DateTime?into the dataPrm.Valuewhile accounting for nulls.

然后我想将 the 的值DateTime?放入dataPrm.Valuewhile 占nulls 中。

I thought initially I'd be clever:

我最初以为我会很聪明:

datePrm.Value = nullableDate ?? DBNull.Value;

but that fails with the error

但由于错误而失败

Operator '??' cannot be applied to operands of type 'System.DateTime?' and 'System.DBNull'

操作员 '??' 不能应用于“System.DateTime?”类型的操作数 和'System.DBNull'

So I guess that only works if the second argument is a non-nullable version of the first argument. So then I went for:

所以我想这只有在第二个参数是第一个参数的不可为空版本时才有效。然后我去了:

datePrm.Value = nullableDate.HasValue ? nullableDate.Value : DBNull.Value;

but that doesn't work either:

但这也不起作用:

Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'System.DBNull'

无法确定条件表达式的类型,因为“System.DateTime”和“System.DBNull”之间没有隐式转换

But I don't want to convert between those types!

但我不想在这些类型之间转换!

So far the only thing I can get to work is:

到目前为止,我唯一可以开始工作的是:

if (nullableDate.HasValue)
  datePrm.Value = nullableDate.Value;
else
  datePrm.Value = DBNull.Value;

Is that really the only way I can write this? Is there a way to get a one-liner using the ternary operator to work?

这真的是我写这篇文章的唯一方式吗?有没有办法使用三元运算符获得单线工作?

Update:I don't really get why the ?? version doesn't work. MSDN says:

更新:我真的不明白为什么 ?? 版本不起作用。MSDN 说:

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

这 ??如果不为空,运算符返回左操作数,否则返回右操作数。

That's exactly what I want!

这正是我想要的!

Update2:Well it was kind of obvious in the end:

更新2:嗯,最后很明显:

datePrm.Value = nullableDate ?? (object)DBNull.Value;

采纳答案by Stewart Johnson

Ah ha! I found an even more efficient solution than @Trebz's!

啊哈!我找到了比@Trebz 更有效的解决方案!

datePrm.Value = nullableDate ?? (object)DBNull.Value;

回答by dnolan

It would work if you used

如果你使用它会起作用

datePrm.Value = nullableDate.HasValue ? (object)nullableDate.Value : DBNull.Value;

回答by Dan

I think the error with your second attempt is due to nullableDate.Value and DBNull.Value being different types and the ternary operator needing to pick one type to return in both cases. I don't have the environment to test this but I think this should work for you:

我认为您第二次尝试的错误是由于 nullableDate.Value 和 DBNull.Value 是不同的类型,并且三元运算符需要选择一种类型才能在这两种情况下返回。我没有环境来测试这个,但我认为这应该适合你:

datePrm.Value = nullableDate.HasValue ? (object)nullableDate.Value : (object)DBNull.Value;

回答by Darren Kopp

The way that I do it, is I have a static utility class that just goes through and checks to see if the parameter value is null, then i set the value to do DBNull. I just do that before i call the Execute.

我这样做的方式是,我有一个静态实用程序类,它只是通过并检查参数值是否为空,然后我将值设置为执行 DBNull。我只是在调用 Execute 之前这样做。

回答by Pop Catalin

If you're using C# 3.0 you can create an extension method to do this easy:

如果您使用的是 C# 3.0,您可以创建一个扩展方法来轻松完成此操作:

public static class DBNullableExtensions
{
    public static object ToDBValue<T>(this Nullable<T> value) where T:struct
    { 
        return value.HasValue ? (object)value.Value : DBNull.Value;
    }
}


class Program
{
    static void Main(string[] args)
    {
        int? x = null;

        Console.WriteLine(  x.ToDBValue() == DBNull.Value );
    }
}

回答by Gian Marco Gherardi

If you are using SQLServer, the System.Data.SqlTypesnamespace contains some utility classes that avoid the annoying type casting. For example instead of this:

如果您使用 SQLServer,System.Data.SqlTypes命名空间包含一些实用程序类,以避免烦人的类型转换。例如,而不是这样:

var val = (object) "abc" ?? DBNull.Value;

you can write this:

你可以这样写:

var val = "abc" ?? SqlString.Null;