Difference between DBNull and Null

Source: Internet
Author: User
Tags mscorlib
The DBNull class indicates a value that does not exist. This class cannot be inherited. Namespace: System Assembly: mscorlib (in mscorlib. dll) DBNull class indicates a non-existent value. For example, in a database table, a column in a row may not contain any data. That is, this column is regarded as nonexistent, not just a value. One indicates that it does not exist.

The DBNull class indicates a value that does not exist. This class cannot be inherited. Namespace: System Assembly: mscorlib (in mscorlib. dll) DBNull class indicates a non-existent value. For example, in a database table, a column in a row may not contain any data. That is, this column is regarded as nonexistent, not just a value. One indicates that it does not exist.

DBNull class

Indicates a value that does not exist. This class cannot be inherited.

Namespace: System

Assembly: mscorlib (in mscorlib. dll)

The DBNull class indicates a non-existent value. For example, in a database table, a column in a row may not contain any data. That is, this column is regarded as nonexistent, not just a value. A DBNull object indicating a column that does not exist. In addition, COM interoperability uses the DBNull class to differentiate VT_NULL variables (indicating nonexistent values) and VT_EMPTY variables (indicating unspecified values ).

DBNull is a separate class, which means that only one DBNull object exists. DBNull:. Value indicates a unique DBNull object. DBNull:. Value can be used to explicitly allocate nonexistent values to database fields. However, most ADO. NET data providers automatically allocate DBNull values when fields do not have valid values. You can pass the Value retrieved from the database field to the DBNull. Value. Equals method to determine whether the field Value is DBNull. However, some languages and database objects provide some methods to determine whether the database field value is DBNull ::. value. these methods include the IsDBNull function of Visual Basic, Convert ::. isDBNull method, DataTableReader ::. isDBNull method and IDataRecord ::. isDBNull method.

Do not confuse the nullNothingnullptrnull reference (Nothing in Visual Basic) concept with the DBNull object. In an object-oriented programming language, nullNothingnullptrnull reference (Nothing in Visual Basic) indicates that there is no reference to an object. DBNull indicates uninitialized variables or non-existent database columns.

Remarks

Example

The following example calls the DBNull. Value. Equals method to determine whether the database field in the contact database has a valid Value. If a valid value exists, the field value is appended to the string output in the tag.

C # usage

Private void OutputLabels (DataTable dt)

{

String label;

// Iterate rows of table

Foreach (DataRow row in dt. Rows)

{

Int labelLen;

Label = String. Empty;

Label + = AddFieldValue (label, row, "Title ");

Label + = AddFieldValue (label, row, "FirstName ");

Label + = AddFieldValue (label, row, "MiddleInitial ");

Label + = AddFieldValue (label, row, "LastName ");

Label + = AddFieldValue (label, row, "Suffix ");

Label + = "/n ";

Label + = AddFieldValue (label, row, "Address1 ");

Label + = AddFieldValue (label, row, "AptNo ");

Label + = "/n ";

LabelLen = label. Length;

Label + = AddFieldValue (label, row, "Address2 ");

If (label. Length! = LabelLen)

Label + = "/n ";

Label + = AddFieldValue (label, row, "City ");

Label + = AddFieldValue (label, row, "State ");

Label + = AddFieldValue (label, row, "Zip ");

Console. WriteLine (label );

Console. WriteLine ();

}

}

Private string AddFieldValue (string label, DataRow row,

String fieldName)

{

If (! DBNull. Value. Equals (row [fieldName])

Return (string) row [fieldName] + "";

Else

Return String. Empty;

}

For new database programming, we may have some questions about "null". For example, if all the data in a table created by programming is displayed as, the text is manually added and deleted and becomes blank; A string-type field, which is clearly not filled in, but not equal to ""; use ADO. NET value from the database, every encounter error ...... This requires a correct understanding. . NET and SQL Server ".

1. The true null value, that is, "No input value", can appear in most types of fields (if there are no other constraints). It is expressed as null in SQL server, displayed as, the method manually entered in the SQL server Enterprise Manager is Ctrl + 0. it is in. NET corresponds to System. DBNull. value. in the T-SQL command, to determine whether a value is null, to use "is null" instead of "= null"; to handle a null value there is an ISNULL function, it replaces null with the specified value. use ADO. the null value obtained from the database cannot be automatically converted to a null string or a Nothing. You must manually check the value if System is obtained. DBNull. value is assigned to the Data Object Nothing or other custom meaningful values.

2. A null string (zero-length string) appears only in a string type (such as nvarchar) field. It is expressed as ''in SQL server and displayed as blank, manually enter a cell in the SQL server Enterprise Manager. It is in. NET corresponds to System. String. Empty, which is commonly used "". There is no difference between processing null strings in T-SQL commands and processing general strings. The Null String obtained from the database using ADO. NET is no different from the general string.

About DBNull

DBNull in DotNet is a separate type of System. DBNull. It only has 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. so row [column]. toString () 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 IDbCommand (OleDbCommand, SqlCommand ...) In the returned values of ExecuteScalar, the situation can be analyzed as follows:

The returned object in select 1 is 1 select null, and DBNull is returned. value select isnull (null, 1) returns 1 select top 0 id from table1. The returned Value 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 in 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 indicates DEFAULT. If it is DBNull. Value, it indicates NULL in SQL

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.