Home  >  Article  >  Backend Development  >  Explanation of DBNULL in C#

Explanation of DBNULL in C#

黄舟
黄舟Original
2017-10-05 15:30:372042browse

1 Overview

What do you think is wrong with the following example? If you can find the problem quickly and solve it, then you can skip this article, thank you~~.

1 List<Base_Employee> ltPI = new List<Base_Employee>();
2 DataTable dt = GetBase_UserInfoToDataTable();
3 for (int i = 0; i < dt.Rows.Count; i++)
4    {
5          Base_Employee base_Employee= new Base_Employee();
6          base_Employee.EmployeeId= dt.Rows[i]["EmployeeId"].ToString();//EmployeeId为string类型
7          base_Employee.Age =(int)dt.Rows[i]["Age"];//Age为int类型
8          base_Employee.GraduationDate = (DateTime)dt.Rows[i]["GraduationDate"];//GraduationDate 为DateTime类型
9    }

Think about it for a minute, OK. If you can’t figure it out, you can look down. The marked area in the picture below is the problem.

## Ok, this article is to solve this problem. That is the System.DBNULL type that I want to share with you next

2 Content sharing

2.1 In .NET, the basic commonly used Data types

int, string, char, etc. are basic data types that everyone is familiar with, but most people should be unfamiliar with System.DBNull. However, it solves the above problems. A big idea.

2.2 Common data types in SqlServer

varchar, nvarchar, int, bit, decimal, datetime, etc., basically in .net The data types correspond one to one (varchar and nvarchar both correspond to the string type in .net)

2.3 Initial values ​​of common data types in SqlServer

In .net, when we define a variable, if we do not assign an initial value to it, the system will default to the initial value. For example, the int type defaults to 0, and the string type defaults to string.Empty. In general, different types The default initial values ​​are different; however, in SqlServer, the initial value of almost all variable types is NULL, which is either a user-defined value or the system default value NUL. This is the key to the problem. Taking the int type as an example, when we do not assign a value to INT in the database, its default value is NULL. When this value is assigned to an integer variable in .net, an exception will be thrown.

2.4 What is System.DBNull?

DBNull is a class that inherits Object, and its instance is DBNull.Value, which is equivalent to the NULL value in the data.

2.5 Why can DBNull represent other data types?

In the database, data is stored in objects.

2.6 How to solve the above problem

Add conditional judgment

You can use whether the string type is empty or whether DBNull is equal to NULL to judge


 1 List<Base_Employee> ltPI = new List<Base_Employee>(); 
 2 DataTable dt = GetBase_UserInfoToDataTable(); 
 3 for (int i = 0; i < dt.Rows.Count; i++) 
 4    { 
 5          Base_Employee base_Employee= new Base_Employee(); 
 6          base_Employee.EmployeeId= dt.Rows[i]["EmployeeId"].ToString();//EmployeeId为string类型 
 7          //base_Employee.Age =(int)dt.Rows[i]["Age"];//Age为int类型 
 8           if (dt.Rows[i]["Age"]!=System.DBNull.Value) 
 9                 {
 10                     base_Employee.Age = int.Parse(dt.Rows[i]["Age"].ToString());
 11                     //base_Employee.Age = (int)dt.Rows[i]["Age"];//拆箱
 12                     //base_Employee.Age =Convert.ToInt16( dt.Rows[i]["Age"]);
 13                 }
 14          //base_Employee.GraduationDate = (DateTime)dt.Rows[i]["GraduationDate"];//GraduationDate 为DateTime类型
 15         if (dt.Rows[i]["GraduationDate"].ToString()!="")
 16                 {
 17                     base_Employee.GraduationDate = Convert.ToDateTime(dt.Rows[i]["GraduationDate"]);
 18                     base_Employee.GraduationDate = (DateTime)dt.Rows[i]["GraduationDate"];
 19                 }
 20    }

The above is the detailed content of Explanation of DBNULL in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn