Not only to be outsourced today, but also to the Hongqiao Airport to help implement. Unfortunately, there is no thing to do it again. . :) Just write blog.
It is time to write database operations in these days, it is time to write something about dbnull.
DBNULL is a separate type of SYSTEM.DBNULL in DOTNET. It has only one value dbnull.Value. DBNULL directly inherits Object, so DBNULL is not string, not int, nor datetime. . .
But why DBNULL can represent strings, numbers, or dates in the database? The reason is that DOTNET stores these data (DataRow et al.) Is stored in the form of Object.
For DataRow, its value returned by its ROW [Column] will never be null, or it is the value of the type of Column. Either DBNULL. So row [column] .tostring () This writing will never have nullreferenceException in Tostring.
DBNULL implements Iconvertible. However, in addition to Tostring is normal, other Toxxx will throw errors that cannot be converted.
In the return value of the ExecuteScalar of idbcommand (OLEDBCommand, SqlCommand ...):
SELECT 1 This returned Object is 1Select Null returned to DBNULL.VALUESELECT ISNULL (NULL, 1) Returning is 1Select Top 0 id from table1 This returned value is nullselect isnull (ID, 0) from table1 where 1 = 0 Return The value is NULL
The rule of ExecuteScalar here is to return the data of the first column, the first line. If the first column is not empty, the value of ExecuteScalar directly corresponds to the value of the DOTNET. If there is a first line, but the first list is empty, then returns DBNULL. If you don't have, then ExecuteScalar returns NULL
The rule is like this. An error, which is easy to commit, is confused with the case of DBNULL and NULL, for example::
String username = cmd.executescalar (). TOSTRING ();
Unless you think CMD is executed, there must be at least one line of data, otherwise it will be wrong here.
Alternative or select ID from userTable where username = @ name This SQL statement, if you can't find record, then ExecuteScalar will return null, so don't
INT userid = convert.Toint32 (cmd.executescalar ());
Or you will write SQL statements like this: select isnull (ID, 0) from userTable where username = @ Name
But int useerid = communication.Toint32 (cmd.executescalar ()); still mistaken, because the above statement is not true, it is still not returned.
For IDBDataParameter (OLEDDBPARAMETER, SQLPARAMETER ..), if you null, it is not specified by this parameter, or represents Default. If you are dbnull.value, represent the NULL in SQL
So, if you want to call the stored procedure, there is parameter @val nvarchar (20) = "aabb", then cmd.parameters ["@ Val"]. Value = NULL represents using this default "AABB" while cmd.Parameters "@val"]. value = dbnull.Value represents Null to pass to @Val you can use Convert.Indbnull to determine if a value dbnull. Note Convert.Indbnull (NULL) is False.
Remarks: The above SQL statement is all of the SQLServer2000. The other database is the same behavior, I am uncertain.
(First write here, think of supplementation)