oracle C#/Oracle10g = null vs DBNull.Value vs String.Empty

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3503237/
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-09-18 21:17:15  来源:igfitidea点击:

C#/Oracle10g = null vs DBNull.Value vs String.Empty

c#oracleado.netnulldbnull

提问by CJM

I'm making my first forays into Accessing Oracle from C#. I've discovered that that Oracle doesn't like VarCharparameters to have value of null(C#). I would have hoped that there would be some implicit conversion, but I appreciate the difference.

我第一次尝试从 C# 访问 Oracle。我发现 Oracle 不喜欢VarChar参数值为null(C#)。我本来希望会有一些隐式转换,但我很欣赏这种差异。

So I need to trap these null values and supply DBNull.Valueinstead, surely? The most obvious method was to use the coalesce operator ??:

所以我当然需要捕获这些空值并提供DBNull.Value吗?最明显的方法是使用合并运算符??

param myParm = myObject.MyProperty ?? DBNull.Value;

Except it doesn't accept a value of System.DBNull... so I have to use:

除了它不接受 ... 的值,System.DBNull所以我必须使用:

param myParm = myObject.MyProperty ?? DBNull.Value.ToString();

...which is surely the same as:

...这肯定是一样的:

param myParm = myObject.MyProperty ?? String.Empty;

..which also works.

..这也有效。

But I always understood that according to ANSI SQL rules, an empty string ("") != a NULL value... yet it appears to be in Oracle.

但我一直明白,根据 ANSI SQL 规则,空字符串 ("") != NULL 值......但它似乎在 Oracle 中。

Anyway, my question is, what is the best, practical or theoretical, way to handle the case of null string values? Is there a slick alternative that I haven't identified? Or is this just another idiosyncrasy of Oracle that we just accept?

无论如何,我的问题是,处理空字符串值的最佳、实用或理论方法是什么?是否有我尚未确定的巧妙替代方案?或者这只是我们接受的另一种 Oracle 特质?

回答by James Curran

An empty string ("") does, if fact NOT equal a NULL value, but that is because NULL is not equal to anything (not even another NULL) (which is why you say IS NULLin an SQL statement instead of = NULL.

如果事实不等于 NULL 值,则空字符串 ("") 确实如此,但那是因为 NULL 不等于任何东西(甚至不等于另一个 NULL)(这就是您IS NULL在 SQL 语句中说而不是= NULL.

NULL means "No Value", and an string that is empty has no value, so Oracles designers decided that there is no difference between an empty string and NULL.

NULL 的意思是“No Value”,空的字符串没有值,所以 Oracle 的设计者认为空字符串和 NULL 没有区别。

param myParm = myObject.MyProperty ?? DBNull.Value;fails because both sides of the ??must be the same type. MyPropertyI'll assume is a string, while DBNull.Valueis aDBNullobject.

param myParm = myObject.MyProperty ?? DBNull.Value;失败,因为 的两边??必须是相同的类型。 MyProperty我假设是一个字符串,DBNull.Value而是一个DBNull对象。

回答by msarchet

There is a much simpler solution here since the ??will not work and String.Emptywill not work for your null value.

这里有一个更简单的解决方案,因为它??不起作用,String.Empty也不适用于您的空值。

param myParm;

if (myObject.MyProperty == null)
{
   myParm  = DBNull.Value;
}
else 
{
   myParm = myObject.MyProperty;
}

It might not be as 'slick' as the null-coalescer but it should work.

它可能不像 null-coalescer 那样“光滑”,但它应该可以工作。

回答by stevemegson

If you set the value to null, .NET won't specify the parameter at all in the generated SQL. Therefore, one option is to specify a default value of NULLfor the parameter in Oracle. That means that setting the value to nullor DBNull.Valuehave the same effect. One passes no value and NULLwill be assumed, while the other explicitly passes a value of NULL.

如果将该值设置为null,.NET 将根本不会在生成的 SQL 中指定参数。因此,一种选择是为NULLOracle 中的参数指定默认值。这意味着将值设置为nullDBNull.Value具有相同的效果。一个不传递任何值NULL并将被假定,而另一个显式传递一个值NULL

Obviously that assumes that you can modify the database, and that you don't already need a different default value for that parameter.

显然,假设您可以修改数据库,并且您不需要该参数的不同默认值。