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;
}
..