Asp.net's tips on reading Null from a database

Source: Internet
Author: User

Its function is very simple, that is to say, first look for the field named Name in the database, and then make a judgment. If its value is null, then the value of number is added 1.

For example, we need to pay attention to the blacklist.
Copy codeThe Code is as follows:
Int number = 0;
String connstr = @ "Data Source =. \ SQLEXPRESS; AttachDbFilename = C: \ Users \ Desktop \ UML Extension \ MyPratices \ WebServices \ App_Data \ Database1.mdf; Integrated Security = True; User Instance = True ";
Using (SqlConnection conn = new SqlConnection (connstr ))
{
Conn. Open ();
String str = "select * from Test ";
Using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd. CommandText = str;
SqlDataReader dr = cmd. ExecuteReader ();
While (dr. Read ())
{
String name = dr ["Name"]. ToString ();
DateTime dt = dr. GetDateTime (dr. GetOrdinal ("InputDate "));
<STRONG> if (dr ["Name"] = null) {number + = 1 ;}</STRONG>}
}
}
If (number = 0)
Label1.Text = "there is no value whose Name is null ";
Else
Label1.Text = "Name indicates the number of null values:" + number;

Then, let's look at the data in my data:

In our Test table, there are three pieces of data, and the value of the Name field of the third piece of data is null. At the beginning, I naturally think that the output will be in the Label: the number of null values for Name is: 1.
Many stories tell us that it is easy to draw conclusions at will. For example, I have come to this conclusion.
Unexpectedly, the value of Null as Name was not found?
Why? At the beginning, I wondered why. Then, set the breakpoint and start F5 to start debugging...
:

No? It does not recognize. That is to say, if it is in the database (at least in SQL? I don't know yet) the null value in oracle is actually not null, And it deceives you with superficial phenomena.
However, we can see its type: object {System. DBNull} from the above. We can find a clue here.
Right, start from the type, and then I read some knowledge about DBNull on the Internet. Specifically, you can view the official website on Baidu.
Once you know that the problem is of the type, it is okay to correct it. The core only needs to be changed as follows:
Copy codeThe Code is as follows:
If (dr ["Name"] = DBNull. Value)
{
Number + = 1;
}

OK, and then it can be correctly identified.
There is another method, in fact, is similar, don't change the changes:
Copy codeThe Code is as follows:
If (Convert. IsDBNull (dr ["Name"])
{
Number + = 1;
}

..

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.