SQL NULL and DBNULL in. NET and NULL analysis for programming languages

Source: Internet
Author: User

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 ();
                     }
                 }

 

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.