Requirements: When registering a user, for the age, address, name and other fields if the customer does not want to fill in, this indicates that the value of these fields is "Do not know",
However, if the database is stored according to the general view of 0, or "" null, this is not required, therefore, the need to deposit the database should be null
At this point, the use of DBNull is required
When storing data this uses//Gets the control input character string name = Txtname.text;string Age = txtage.text;string height = txtheight.text;// If no user has entered a name, the Text property is an empty string, object Objname;if (name). length<=0) {objname = Dbnull.value;//dbnull.value used to represent values in the database}else{objname = name;} Object Objage;if (age. length<=0) {objage = DBNull.Value;} else{objage = age;}
This uses the DataRow row = table when reading the data. Rows[0];string name;if (row["name"]==dbnull.value) {name=null;} else{name = (string) row["name"];} Int? Age;//int age not, because int is not NULL, plus? Number that represents a nullable int type if (row["age"]==dbnull.value) {age = null;} else{age = (int) row[' age ';} int Height = (int) row["Height"];
Microsoft MSDN explains the following:
DBNull class represents a value that does not exist. For example, in a table in a database, a column in a row might not contain any data. that is , the column is considered to not exist at all, and not just without a value. a DBNull object that represents a column that does not exist. In addition, COM Interop uses the DBNull class to differentiate between VT_NULL variables (indicating nonexistent values) and VT_EMPTY variables (indicating unspecified values).
The DBNull type is a separate class, which means that only one DBNull object exists. DBNull. The Value member represents a separate DBNull object. DBNull. value can be used to explicitly assign non-existent values to database fields, but most of the ADO data providers automatically assign DBNull values when the fields do not have valid values. you can determine whether the field value is a DBNull value by passing the value retrieved from the database field to the DBNull.Value.Equals method. However, some languages and database objects provide methods that make it easier to determine whether a database field value is DBNull. Value. These methods include the IsDBNull function of Visual Basic,Convert. IsDBNull method,DataTableReader. IsDBNull methods and IDataRecord. IsDBNull method.
Do not confuse the null concept in an object-oriented programming language with a DBNull object. in an object-oriented programming language,null represents a reference to a missing object. DBNull represents an uninitialized variable or a nonexistent database column.
C#z about accessing the database null and 0