ArticleDirectory
- Null, dbnull, String. Empty differences-C # reposted to students who do not know much about it, please take a closer look.
- Description of dbnull:
- Null
- "" And string. Empty
- Convert. isdbnull ()
Null, dbnull, String. Empty differences-C # for those who do not know much about it, please take a closer look at the explanation of dbnull:
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.China Network Management Alliance www_bitscn_com
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.54ne.com
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.
"" 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.
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.China
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.China Network Management Alliance WWW,
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.
Database Operations and dbnull and null Joke website www.zaokaishi.com
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.China
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 ();China
Unless you think that there must be at least one row of data after executing the command, an error will occur here.Joke website www.zaokaishi.com
Or select ID from usertable where username = @ name. If no record is found, executescalar returns NULL, so never
Int userid = convert. toint32 (CMD. executescalar ());Joke website www.zaokaishi.com
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.