C # difference between DBNULL and NULL,

Source: Internet
Author: User

C # difference between DBNULL and NULL ],

Null is an invalid object reference in. net.
DBNull is a class. DBNull. Value is its unique instance. It indicates the value in. net when the data in the database is NULL (<NULL>.
Null indicates that the pointing of an object is invalid, that is, the object is empty.
DBNull. Value indicates that the Value of an object in the database is null or not initialized. The DBNull. Value object is a valid object.
DBNull in DotNet is a separate type of System. DBNull. It has only one Value, DBNull. Value. DBNull directly inherits the Object, so DBNull is not a string, not an int, or DateTime...
But why does DBNull indicate a string, number, or date in the database? The reason is that the classes (such as DataRow) used by DotNet to store the data are in the form of objects.
For DataRow, the value returned by its row [column] is never null, or it is a specific value of the column type. Or DBNull. Therefore, the row [column]. ToString () Statement will never cause NullReferenceException in ToString, but it is possible to throw an exception in the subscript out of bounds.
DBNull implements IConvertible. However, except that ToString is normal, other ToXXX will throw an error that cannot be converted.
In the returned values of ExecuteScalar of IDbCommand (OleDbCommand, SqlCommand...), we can analyze the situation as follows:
The returned object in select 1 is 1.In this case, select null returns DBNull. Value.Select isnull (null, 1) returns 1The return value of select top 0 id from table1 is null.Select isnull (id, 0) from table1 where 1 = 0 the returned value is null.
Here, the ExecuteScalar rule is to return the data of the first column and the first row. If no row exists, ExecuteScalar returns null. If the first row exists but the first column is empty, DBNull is returned. If the first row of the first column is not empty, ExecuteScalar directly corresponds to the DotNet value.
The rule is like this. One easy mistake here is to confuse ExecuteScalar's returned DBNull with null, for example:
String username = cmd. ExecuteScalar (). ToString ();
Unless you think that there must be at least one row of data after executing the command, an error will occur here.
Or select id from usertable where username = @ name. If no record is found, ExecuteScalar returns null, so never
Int userid = Convert. ToInt32 (cmd. ExecuteScalar ());
Or you will write the SQL statement: select isnull (id, 0) from usertable where username = @ name
However, int userid = Convert. ToInt32 (cmd. ExecuteScalar (); still has an error. Because the preceding statement is not valid, no rows are returned.
For the Value of IDbDataParameter (OleDDbParameter, SqlParameter ..), if it is null, it indicates that this parameter is not specified or it represents DEFAULT. If it is DBNull. Value, it indicates NULL in SQL
Therefore, if you want to call the stored procedure, there is the default parameter @ val nvarchar (20) = "AABB ",Then cmd. Parameters ["@ val"]. Value = null indicates that the default "AABB" is used"Cmd. Parameters ["@ val"]. Value = DBNull. Value indicates that NULL is used to pass to @ val.
You can use Convert. IsDBNull to determine whether a value is DBNull. Note that Convert. IsDBNull (null) is false, that is, null and DBNull. Value are different.

Related Article

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.