How to judge whether a field retrieved from the database is null

Source: Internet
Author: User
If the name field is null in the database, use Data. Tables [0]. Rows [0] ["alarmflag"]! = NULL The result is True , The reason isAlarmflagThe field is read as system. dbnull. WhereAlarmflag is tinyint type The correct method is Data. Tables [0]. Rows [0] ["alarmflag"]! = System. dbnull. Value You can also use the following method Data. Tables [0]. Rows [0] ["alarmflag"]. Tostring ()! = "" Appendix: NULL indicates a null object or pointer-in C)
System. dbnull indicates empty data (represented by the database in C) Data. Tables [0]. Rows [0]. isnull ("time ")? Datetime. Now: Convert. todatetime (data. Tables [0]. Rows [0] ["time"]) WhereTimeDatetime type Data. Tables [0]. Rows [0]. isnull ("circle ")? 0: Convert. toint32 (data. Tables [0]. Rows [0] ["circle"]) WhereCircleInt type

Msdn: do not include object-orientedProgramming LanguageInNullReference (in Visual BasicNothing)The concept is confused with the dbnull object. In an object-oriented programming language,NullReference (in Visual BasicNothing)Indicates that there is no reference to an object. Dbnull indicates uninitialized variables or non-existent database columns. Appendix:How to correctly understand null in SQL

In SQL statements, the null value is different from the space in the character column, the zero in the number, and the null ASCII character in the character column. when the DBMS finds a null value in a column, it translates it into undefined or unavailable. DBMS cannot make null assumptions in a column, nor can it assume that the null value is equal to null. The possible cause for a column to become null is: (1), and the value does not exist; (2), the value is unknown; (3), the column is unavailable to the table. therefore, the null value should be treated as an operator rather than a value. when a DBMS finds a null value in a column in a row of a table, the DBMS knows that the data is lost or unavailable.

In a word, null indicates that the value is "unknown" because it may not exist or be unavailable, or because the value is unknown.

"Null" in various environments (I don't know how to name these things, so it's a null value for the time being !) A simple explanation:

1: True null values (null in SQL)

It is equivalent to "no input value" and can appear in most types of fields (if there are no other constraints). It is expressed as null in SQL Server and displayed, manually enter Ctrl + 0 in the SQL Server Enterprise Manager. It corresponds to system. dbnull. Value in. net. In the T-SQL command, to determine whether a value is not a null value, use "is null" instead of "= NULL"; to handle a null value, there is an isnull function that replaces null with the specified value. Use ADO. the null value obtained from the database cannot be automatically converted to a null string or a nothing. You must manually check the value if system is obtained. dbnull. value is assigned to the Data Object nothing or other custom meaningful values.

2: a null string (zero-length string) appears only in a string type (such as nvarchar) field. It is expressed as ''in SQL Server and displayed as blank, manually enter a cell in the SQL Server Enterprise Manager. It corresponds to system. String. Empty in. net, which is commonly used "". There is no difference between processing a null string in the T-SQL command and processing a general string. The Null String obtained from the database using ADO. NET is no different from the general string.

3. VB. net; it actually corresponds to C #. NET Null (note that this null is C #. net, rather than SQL Server), they are in. net is a null reference value that does not reference any object.

In SQL Server, it may be stored as a true null value (for example, when updating a field value of the string type) according to different context environments ), it is also possible to call the custom default values in SQL Server (for example, to pass a stored procedure parameter with a default value), or it may be caused by the failure of type conversion. net exception. Therefore, when using ADO. Net to store data to SQL Server, you must be careful when using nothing.

4. The difference between the undefined value and null is:

Generally, the undefined value can be used in the following cases:

(1) The object property does not exist,

(2) declared variables but never assigned values.

Release the reference and use NULL for operations such as variable value in the trace result.

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.