DBNull and Null "reprint"

Source: Internet
Author: User
Tags dotnet

Null is an invalid object reference in. Net.

DBNull is a class. DBNull.Value is the only instance of it. It refers to the value in. NET when the data in the database is empty (<NULL>).

Null indicates that an object's pointer is invalid, that is, the object is an empty object.

DBNull.Value indicates that the value of an object in the database is empty, or uninitialized, that the DBNull.Value object is a pointer to a valid object.

DBNull in Dotnet is a single type of system.dbnull. It has only one value DBNull.Value. DBNull directly inherits Object, so DBNull is not a string, not an int, or a DateTime ...

But why DBNull can represent strings, numbers, or dates in a database? The reason is that the classes that store the data (DataRow, and so on) are stored in the form of object dotnet the data.

For a DataRow, the value returned by its row[column] is never null, or it is the value of the type of column that is specific. Or it's DBNull. So Row[column]. The ToString () notation will never occur at ToString, but it is possible to throw out the NullReferenceException exception.

DBNull implements the IConvertible. However, except that ToString is normal, other toxxx will throw an error that cannot be converted.

In the return value of the executescalar of IDbCommand (Oledbcommand,sqlcommand ...), the situation can be analyzed like this:

Select 1 Returns an object that is 1

Select NULL to return the DBNull.Value

Select IsNull (null,1) returned the 1

Select top 0 ID from table1 This returns a value of NULL

Select IsNull (id,0) from table1 where 1=0 the value returned is null

The ExecuteScalar rule here is to return the first column, the first row of data. If none of the rows are there, then ExecuteScalar returns NULL. If there is a first row, but the first column is empty, then the return is DBNull. If the first row of the first column is not empty, then the executescalar directly corresponds to the value of the dotnet.

This is the rule. One of the easiest mistakes here is to confuse ExecuteScalar return dbnull with NULL, for example:

String Username=cmd. ExecuteScalar (). ToString ();

Unless you think that CMD executes, there must be at least one row of data, otherwise there will be an error.

or select ID from usertable where [email protected] Such an SQL statement, if the record is not found, then ExecuteScalar will return null, so do not

int Userid=convert.toint32 (cmd. ExecuteScalar ());

Or you would write SQL statements like this: Select IsNull (id,0) from usertable where [email protected]

but int userid=convert.toint32 (CMD). ExecuteScalar ()); Will still go wrong, because when the above statement is not true, no rows are returned.

For IDbDataParameter (Oleddbparameter,sqlparameter.) Value, if NULL, indicates that the parameter is not specified or represents default. If DBNull.Value, represents null in SQL

So, if you're going to call a stored procedure with a default parameter @val nvarchar = "AABB",

So cmd. parameters["@val"]. The Value=null delegate uses this default "AABB"

and CMD. parameters["@val"]. The Value=dbnull.value delegate uses null to pass to @val

You can use Convert.isdbnull to determine whether a value is DBNull. Note that convert.isdbnull (null) is false, which means that null is not equal to DBNull.Value.

"Reprint" http://www.cnblogs.com/chen-fan/articles/2171399.html

DBNull and Null "reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.