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

Source: Internet
Author: User

In sqlserver 2000, the null value of datetime is the default value 00:00:00. in C #, the null value of datetime is the minimum value 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 from the internet search out: http://treexyz.spaces.live.com/blog/cns! 6667864a1ffeabc7! 300. Entry
processing of NULL data type
what should I do when C # is different from SQL Server's data type definition?
for example, if the field in the DB can be null, it must be assigned a value using the datetime value of C # and can be assigned a null value of SQL Server.
the solution is to set the property data type of the field to object in the class corresponding to the model.
when the value is assigned, it is judged that dbnull. value is assigned if the value is null.
when the value is set, it is determined that 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 both the dbnull and datetime values of C # can be assigned to sqldbtype. datetime; otherwise, the null values obtained in SQL appear in dbnull. value and cannot be assigned to the datetime values 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 always judges the parameter at the receiver. Some parameters are null and written to the database. However, if some parameters are null, they have other meanings and require a condition branch.CodeTo process. 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. HoweverProgram, Especially in Object-Oriented Programming, some problems with the layer-3 architecture 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.

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.