Assign null value to datatime in Asp.net (C #)

Source: Internet
Author: User

 

Research on null values of datetime type in sqlserver and null values of datetime in C #

In sqlserver 2000, the default value of datetime is 00:00:00. in C #, the null value of datetime is the minimum value of 0001-01-01 00:00:00. For database insertion, the date value updated to a null value is somewhat less than expected. For example, I inserted a null value. I also took out a null value, but sqlserver gave me a default value. A little helpless. How to coordinate the two has brought me a lot of trouble.

The cause is that datetime, Int, and double are both value types and must have a value.

Solution:

1. From the database perspective: Change the field type to custom string type. The string format is date style. This method is simple. It is actually a string type. during insertion, the inserted string is judged based on the regular expression of the date.

 

2. This method is searched from the Internet out of: http://treexyz.spaces.live.com/blog/cns! 6667864a1ffeabc7! 300. How does the entry process NULL data types? What should I do when C # is different from the SQL server data type definition?

For example, if a datetime field in dB can be empty, it must be assigned a value using the datetime value of C # And the null value of SQL Server.

The solution is to set the Data Type of the property corresponding to this field to object in the class corresponding to the model.

When the value is assigned, it is determined that dbnull. value is assigned if the value is null.

If dbnull. value is obtained, null is displayed. Otherwise, a data conversion is required.

This is for Da to operate on the database. If this field is used as a parameter, you only need to assign the get value to salparameter. (If the get of the model is not converted, the DA must determine whether to convert the data. Because dbnull and datetime of C # can be assigned to sqldbtype. datetime; otherwise, the null value obtained in SQL is generated by dbnull. value and cannot be assigned to datetime of C.

Forum has a major feature: when calling the DB. Cs method, the parameter types are almost all objects. In SP, the format of each parameter is carefully defined and converted by SQL itself. For null parameters, define = NULL in SP. Forum has many cases of passing a null parameter. It is always judged by the receiver. Some parameters are written to the database as null, but some parameters have other meanings if they are null, conditional branch code is required for processing. I am used to parameter initialization first. If the same method is used in different cases, a parameter is added for judgment. I think it is not a good way to make the parameter null, because it may cause some meaningless conditional branches, which may cause potential risks for future upgrades and maintenance.

 

3. Insert the following statement:

Insert into employees (lastname, firstname, hiredate) values ('hu', 'shuai', null ),

Insert into employees (lastname, firstname, hiredate) values ('hu', 'shuaishuai ','')

There are obviously different results. It can be implemented in SQL statements. However, in programming, especially in Object-Oriented Programming, three-tier architecture issues are added. It is inconvenient to implement. So I am trying to find a clever way to implement it, and I hope you can give me some suggestions.

3.

The simplest method:

Assign a default date to the page

In an SQL statement, when the parameter value is assigned, determine whether the parameter value is equal to the default date. If the parameter value is equal to dbnull. Value

 

 

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.