Description of dbnull type

Source: Internet
Author: User
Tags dotnet
This class is used to indicate that no known value exists (usually used in database applications ). Program ).

In database applications, empty objects are valid values of fields. This class distinguishes between null values (empty objects) and uninitialized values (dbnull. Value instances ). For example, a table can contain records with uninitialized fields. By default, these uninitialized fields have dbnull values.

This class can also be used to differentiate vt_null variables (associated with empty objects) and vt_empty variables (associated with dbnull. Value instances) in COM InterOP ).

Dbnull is never equal to any value.

Dbnull is a separate class, which means that the class can only have one instance. The unique instance is dbnull. value.
"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.

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 1
The 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 the first row of the first column is not empty, executescalar directly corresponds to the DOTNET value. If the first row exists but the first column is empty, dbnull is returned. If no row exists, executescalar returns NULL.

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 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.

Note: all the preceding SQL statements refer to sqlserver2000.

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.