I. Differences between NULL in SQL Server and DBNULL in. NET and NULL in programming languages
1: NULL in SQL Server
The NULL value in SQL Server IS expressed as "NULL", equivalent to "no input value", in the T-SQL command, to determine whether a value IS NULL, to use "IS NULL ", instead of using "= NULL", insert a new row into a table in the SQL Server database. Fields without input values in the new row are "NULL ", in the data editing area of a table in the SQL Server database, press Ctrl + 0 to change the cell to "null ".
We create a new row and write "Jia Qi" only for the Name field. We will find that the Subject field and the following field are both "NULL ".
2: DBNULL in. NET
ADO. when the NET data provider obtains data from the database, when the field does not have a valid value, that is, the "NULL" mentioned above, will be automatically assigned to the DBNULL value, therefore, NULL in SQL Server is equivalent. DBNULL in. NET.
In. NET, the DBNULL type is a separate class, indicating uninitialized variables or non-existent database columns.
3: For NULL in programming languages
NULL in programming languages indicates that no reference to an object exists. Therefore, NULL in SQL Server and C # is completely different.
Example
For example, if the age value of a student is NULL, it does not mean that the student has no age. Everyone has an age, but is not set or unknown, declare an age variable that can be empty as follows
Int? Age = null;
II. Instances
SqlConnection conn = SqlHelper. GetConnection (Convert. ToInt16 (CustomEnum. DBCallType. Other libraries ));
String sqlText = "SELECT * FROM NameAndSubjectAndGrade where id = 15 ";
SqlDataReader dr = SqlHelper. ExecuteReader (conn, CommandType. Text, sqlText );
If (dr. Read ())
{
If (dr ["Name"] is DBNull)
{
This.txt Name. Text = "";
}
Else
{
This.txt Name. Text = dr ["Name"]. ToString ();
}
If (dr ["Subject"] is DBNull)
{
This.txt Subject. Text = "";
}
Else
{
This.txt Subject. Text = dr ["Subject"]. ToString ();
}
If (dr ["Grade"] is DBNull)
{
This.txt Subject. Text = "";
// You can also write it as follows. DBNULL can be converted to the String type, and the converted value is ""
This.txt Subject. Text = dr ["Grade"]. ToString ();
}
Else
{
This.txt Grade. Text = dr ["Grade"]. ToString ();
}
}