当我们必须在C#.Net中使用DBNull.Value,null和“”时?

时间:2022-03-23 14:05:43

I have a little confusion with the following things:

我对以下事情有点困惑:

  1. Null
  2. 空值
  3. DBNull.Value
  4. DBNull.Value
  5. ""
  6. “”

When I use Conditional Statements OR while assigning values, I am a little bit confused with these things. Sometimes it throws error and some times it works. I want to know when I want to use the above things. Are they specific with datatypes? I need your valuable suggestions please.

当我使用条件语句或在分配值时,我对这些事情有点困惑。有时它会抛出错误,有时它会起作用。我想知道什么时候我想用上面的东西。它们是否特定于数据类型?我需要你宝贵的建议。

3 个解决方案

#1


14  

null is one of two things:

null是两件事之一:

  • a reference that doesn't actually point to an object - just a "nothing" indicator (essentially, it is the value 0 as a reference)
  • 一个实际上没有指向对象的引用 - 只是一个“无”指示符(实际上,它是值0作为引用)
  • a Nullable<T> struct, which does not currently have a value (the HasValue property will also return false)
  • 一个Nullable 结构,它当前没有值(HasValue属性也将返回false)

DBNull is specific to some parts of ADO.NET to represent null in the database. I have yet to think of a good reason why they didn't just use regular null here.

DBNull特定于ADO.NET的某些部分,以在数据库中表示null。我还没有想到他们为什么不在这里使用常规null的一个很好的理由。

"" is a string literal with length zero - a perfectly valid, but empty, string. The significance of this is that between null string and a "" string, instance methods like value.Trim() will behave differently; null.Trim() will throw an exception; "".Trim() is just "". In general, using string.IsNullOrEmpty(value) as a test makes this distinction go away.

“”是一个长度为零的字符串文字 - 一个完全有效但空的字符串。这意味着在null字符串和“”字符串之间,像value.Trim()这样的实例方法的行为会有所不同; null.Trim()将抛出异常; “”.Trim()只是“”。通常,使用string.IsNullOrEmpty(value)作为测试会使这种区别消失。

#2


12  

  • null is internal to the language and simply means that the object reference currently doesn't refer to an actual object. Basically, a key difference between value types (int, bool, etc.) and reference types (Object, any class, etc.) is that value types are a concrete value in memory and reference types are a pointer to a representation of the object in memory. That pointer can sometimes point to, well, nothing. In which case it's null. It basically means "No C# object exists here."

    null是语言的内部,只是表示对象引用当前不引用实际对象。基本上,值类型(int,bool等)和引用类型(Object,任何类等)之间的关键区别在于值类型是内存中的具体值,引用类型是指向对象表示的指针在记忆中。那个指针有时可以指向,没什么。在这种情况下,它是null。它基本上意味着“这里没有C#对象。”

  • DBNull.Value is slightly different for the purpose of working with databases. A database column can contain a null value. However, when that data is selected into an object (such as a DataTable) in code, there is an object there to reference as far as the code is concerned. However, that object contains a representation of a null value from the database. Thus, DBNull.Value exists to represent that distinction. It basically means "There is a C# object here which grabbed a value from a database, but that value is null."

    DBNull.Value与使用数据库的目的略有不同。数据库列可以包含空值。但是,当在代码中将该数据选择到对象(例如DataTable)中时,就代码而言,有一个对象可供引用。但是,该对象包含数据库中空值的表示。因此,DBNull.Value存在以表示该区别。它基本上意味着“这里有一个C#对象从数据库中获取一个值,但该值为null。”

  • "" (or string.Empty if you want to use a built-in constant for that) is a valid value. It exists, it's not null, it's in every way a string. It just doesn't have any characters in it. A useful tool for checking this is string.IsNullOrEmpty() which checks for either null or the empty string.

    “”(或string.Empty,如果你想使用内置常量)是一个有效的值。它存在,它不是空的,它在各方面都是一个字符串。它只是没有任何字符。检查这个的有用工具是string.IsNullOrEmpty(),它检查null或空字符串。

#3


1  

null applies to C# null values.

null适用于C#空值。

System.DBNull.value applies to database specific NULL values.

System.DBNull.value适用于特定于数据库的NULL值。

When you use ExecuteScalar() it gives null if column not found or returned, but it column found and value is null there then it returns DBnull, means in a column if value is null, if will return DBnull not null.

当你使用ExecuteScalar()时,如果没有找到或返回列,则它会给出null,但是找到列并且value为null然后返回DBnull,如果value为null则表示在列中,if将返回DBnull而不是null。

#1


14  

null is one of two things:

null是两件事之一:

  • a reference that doesn't actually point to an object - just a "nothing" indicator (essentially, it is the value 0 as a reference)
  • 一个实际上没有指向对象的引用 - 只是一个“无”指示符(实际上,它是值0作为引用)
  • a Nullable<T> struct, which does not currently have a value (the HasValue property will also return false)
  • 一个Nullable 结构,它当前没有值(HasValue属性也将返回false)

DBNull is specific to some parts of ADO.NET to represent null in the database. I have yet to think of a good reason why they didn't just use regular null here.

DBNull特定于ADO.NET的某些部分,以在数据库中表示null。我还没有想到他们为什么不在这里使用常规null的一个很好的理由。

"" is a string literal with length zero - a perfectly valid, but empty, string. The significance of this is that between null string and a "" string, instance methods like value.Trim() will behave differently; null.Trim() will throw an exception; "".Trim() is just "". In general, using string.IsNullOrEmpty(value) as a test makes this distinction go away.

“”是一个长度为零的字符串文字 - 一个完全有效但空的字符串。这意味着在null字符串和“”字符串之间,像value.Trim()这样的实例方法的行为会有所不同; null.Trim()将抛出异常; “”.Trim()只是“”。通常,使用string.IsNullOrEmpty(value)作为测试会使这种区别消失。

#2


12  

  • null is internal to the language and simply means that the object reference currently doesn't refer to an actual object. Basically, a key difference between value types (int, bool, etc.) and reference types (Object, any class, etc.) is that value types are a concrete value in memory and reference types are a pointer to a representation of the object in memory. That pointer can sometimes point to, well, nothing. In which case it's null. It basically means "No C# object exists here."

    null是语言的内部,只是表示对象引用当前不引用实际对象。基本上,值类型(int,bool等)和引用类型(Object,任何类等)之间的关键区别在于值类型是内存中的具体值,引用类型是指向对象表示的指针在记忆中。那个指针有时可以指向,没什么。在这种情况下,它是null。它基本上意味着“这里没有C#对象。”

  • DBNull.Value is slightly different for the purpose of working with databases. A database column can contain a null value. However, when that data is selected into an object (such as a DataTable) in code, there is an object there to reference as far as the code is concerned. However, that object contains a representation of a null value from the database. Thus, DBNull.Value exists to represent that distinction. It basically means "There is a C# object here which grabbed a value from a database, but that value is null."

    DBNull.Value与使用数据库的目的略有不同。数据库列可以包含空值。但是,当在代码中将该数据选择到对象(例如DataTable)中时,就代码而言,有一个对象可供引用。但是,该对象包含数据库中空值的表示。因此,DBNull.Value存在以表示该区别。它基本上意味着“这里有一个C#对象从数据库中获取一个值,但该值为null。”

  • "" (or string.Empty if you want to use a built-in constant for that) is a valid value. It exists, it's not null, it's in every way a string. It just doesn't have any characters in it. A useful tool for checking this is string.IsNullOrEmpty() which checks for either null or the empty string.

    “”(或string.Empty,如果你想使用内置常量)是一个有效的值。它存在,它不是空的,它在各方面都是一个字符串。它只是没有任何字符。检查这个的有用工具是string.IsNullOrEmpty(),它检查null或空字符串。

#3


1  

null applies to C# null values.

null适用于C#空值。

System.DBNull.value applies to database specific NULL values.

System.DBNull.value适用于特定于数据库的NULL值。

When you use ExecuteScalar() it gives null if column not found or returned, but it column found and value is null there then it returns DBnull, means in a column if value is null, if will return DBnull not null.

当你使用ExecuteScalar()时,如果没有找到或返回列,则它会给出null,但是找到列并且value为null然后返回DBnull,如果value为null则表示在列中,if将返回DBnull而不是null。