Description of DBNull, Null, and String. Empty in C #

Source: Internet
Author: User

1. Explanation of DBNull:
This class is used to indicate that no known value exists (usually in database applications ).
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.
For Data-intensive applications that access SQL databases, the System. Data. SqlTypes class must be used. These classes have inherent support for null values.
DBNull is a separate type in DotNet. This class can only have unique instances, DBNULL. value, DBNull is used only to indicate the strings, numbers, or dates in the database. Why does it indicate the reason for the classes (such as DataRow) That DotNet stores the data) all store data 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.


2. Null
The null keyword is a text value that does not reference any object's null reference. Null is the default value of the reference type variable. Then, only the referenced variable can be NULL. If int I = null, it is not possible because Int Is of the value type.
"Null" means the object reference is invalid in. NET, when you retrieve a NULL value from the Database, it is a valid value. NET, and it is represented by System. DBNull. value
Null is used to determine the Reference invalidate.

 
3. "And String. Empty
Both of these are empty strings, with a focus on the difference between string str1 = "" and string str2 = null. After this definition, str1 is an empty string, A null string is a special string, except that the value of this string is null and has an accurate point in memory. string str2 = null. After this definition, only a string class reference is defined. str2 does not point to any place. If it is not instantiated before use, an error is returned.


4. Convert. IsDBNull ()
Convert. IsDBNull () indicates whether the specified object is of the DBNull type, that is, whether the object is DBNULL. The return value is True or Flase.
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 [url = mailto: username = @ name] username = @ name [/url]. 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 [url = mailto: username = @ name] username = @ name [/url]
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.

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.