Convert: values of the datetime type in SqlServer and values of the DateTime type in c #

Source: Internet
Author: User
Research on the null values of datetime type in SqlServer and the null values of DateTime in c # In SqlServer2000: The default value is 1900-01-0100:00:00, in C #, the null value of DateTime is the minimum value of 0001-01-0100:00:00. For database insertion, the date value updated to NULL is somewhat less as expected. Ratio

Study on the null values of datetime type in SqlServer and the null values of DateTime in c # In SqlServer 2000, that is, the default value is: 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 NULL is somewhat less as expected. Ratio

Research on null values of datetime type in SqlServer and null values of DateTime in c #

The default value of datetime in SqlServer 2000 is.

: 00, the null value of DateTime in C # is the minimum value 0001-01-01 00:00:00.

The date value for database insertion and update to NULL is somewhat less as expected. For example

I inserted a null value. I also took out a null value, but SqlServer gave me one.

Default value. A little helpless. How to coordinate the two has brought me a lot of trouble.

The reason for this is: DateTime, Int, and Double are both value types and must be

There is a value.

Solution:

1. From the database perspective: Change the field type to custom string type.

The string format is the date style. This method is simple. It is actually a string type.

Returns the date regular expression for the inserted string.

2. This method is used to search from the Internet:

Http://treexyz.spaces.live.com/blog/cns! 6667864a1ffeabc7!

300. entry Processing of null data type when C # and SQL Server data types are not defined

What should I do when they are consistent?

For example, if a DateTime field in DB can be empty, it must be able to use

DateTime value assignment, you can also assign the SQL Server null value.

In the class corresponding to the Model

Property data type is set to object.

When the Value is assigned, it is determined 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 number is required.

Data conversion.

This is for DA to operate on the database. If this field is used as a parameter, you only need to get

The value can be directly assigned to salParameter. (If the get of the model is not converted, it is determined in DA.

Whether to convert the disconnections. Because DBNull and DateTime of C # can be assigned

SqlDBType. DateTime; otherwise, the null Value obtained in SQL is derived from DBNUll. Value.

Currently, it cannot be assigned to the DateTime of C.

Forum has a major feature, that is, when calling the DB. cs method, the parameter type is several

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 transmits a null Parameter

In many cases, parameters are always determined on the receiver side. Some parameters are null and written to the database;

However, if some parameters are null, they have other meanings and need to be processed by the conditional branch code.

. I'm used to parameter initialization first. If the same method is used in different cases, the value is increased.

Add a parameter for judgment. I think it is not very useful to make the parameter null.

Solution, because it will cause some unknown conditional branches, and will create for future upgrades and Maintenance

Potential danger.

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. But in the program, especially

In object-oriented programming, three-tier architecture issues are added. Implementation is quite

Inconvenience. So I am trying to find a way to implement it skillfully.

Recommended.

3.

The simplest method:

Assign a default date to the page

When assigning values to parameters in SQL statements, you can determine whether the parameter value is equal to the default date. If the value is equal to the parameter value

Value assignment DBNull. Value

Http://blog.csdn.net/jxqvip/article/details/6565044

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.