Explanation of DBNull in C #

Source: Internet
Author: User
1 Overview

What do you think is wrong with the following example? If you can quickly find the problem 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 is a string type 7          base_employee.age = (int) dt. rows[i]["age"];//age for int type 8          base_employee.graduationdate = (DateTime) dt. rows[i]["Graduationdate"];//graduationdate is datetime type 9    }

Think of a minute, OK, if you do not want to come out, you can look down, the mark is the question place.

OK, this article is to solve the problem. The type of system.dbnull to share with you next

2 content Sharing

2.1 Common basic data types in. Net

Int,string,char are familiar with the basic data types, but most people should be more unfamiliar with system.dbnull, however, it is a big idea to solve the above problems.

2.2 Common data types in SQL Server

Varchar,nvarchar,int,bit,decimal,datetime, etc., basically corresponds to data type one by one in. NET (varchar and nvarchar correspond to string types in. net)

2.3 Initial values for common data types in SQL Server

In. NET, when we define a variable, if it is not assigned an initial value, then the system defaults to the default value, such as the type int default to 0,string type by default to string. Empty, in general, the default initial values for different types are different; however, in SQL Server, the initial value of almost all variable types is null, either for a user-defined value or for the system default value of NUL. The key to the problem is here, in the case of the int type, when we do not assign a value to int in the database, its default value is NULL, and when the value is assigned to the shaping variable in. NET, an exception is thrown.

What is 2.4 system.dbnull?

DBNull is a class that inherits object, in fact an example of DBNull.Value, which is equivalent to a null value in the data.

2.5 Why DBNull can represent other data types?

In a database, the data store is stored as an object.

2.6 How to solve the above problems

Add conditional judgment

Can be judged by whether the string type is empty, or whether DBNull is equal to null


 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 is a string type 7//base_employee.age = (int) dt. rows[i]["age"];//age for int type 8 if (dt. rows[i]["Age"]!=system.dbnull.value) 9 {Ten base_employee.age = Int. Parse (dt. Rows[i]["Age"]. ToString ()); One//base_employee.age = (int) dt. rows[i]["Age"];//unboxing//base_employee.age =convert.toint16 (dt. Rows[i]["Age"]); //base_employee.graduationdate = (DateTime) dt. rows[i]["Graduationdate"];//graduationdate is a datetime type of (DT). rows[i]["Graduationdate"]. ToString () = "" ") (Base_employee.graduationdate = Convert.todatetimE (dt. rows[i]["Graduationdate"]); Base_employee.graduationdate = (DateTime) dt. rows[i]["Graduationdate"]; 19} 20}
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.