Study on Datatime Assignment value in ASP (C #)

Source: Internet
Author: User

A study of NULL values for datetime types in SQL Server and Null values for DateTime in C #

The null value for datetime in SQL Server 2000, which is the default value of 1900-01-01 00:00:00,c#, is the minimum value of 0001-01-01 00:00:00, for database insertions, A date value that is updated to a null value is somewhat inferior to what one might wish. For example, I inserted a null value I took out the null value, but SQL Server gave me a default value. A little helpless. I had a lot of trouble with how to reconcile them.

This is caused by the fact that DateTime and int,double are both value types and must have a value.

The solution:

1. From the database perspective: Change the type of the field to a custom string format, which is a date style. This method is relatively simple, in fact, is the string type, insert the string to make the date of the regular judgment.

2. This method is searched from the internet out of: http://treexyz.spaces.live.com/blog/cns!6667864a1ffeabc7!300.entry processing of data type NULL when C # and SQL What do I need to do when the server data type definition is inconsistent?

For example, a DateTime, when the field in DB can be empty, requires that it be able to assign both a DateTime in C # and a null value for SQL Server.

The workaround I take is to set the data type of the property that corresponds to the field to object in the model corresponding class.

When the value is assigned, it is determined that the null value is assigned to DBNull.Value.

When the value is taken, it is judged that NULL is displayed if the DBNull.Value is taken, otherwise a data conversion is performed.

This is for DA in the case of a DB operation, if this field is used as an argument, simply assign the get to the value directly to Salparameter. (If the model's get is not converted, the DA is determined whether to convert.) Because C # 's DBNull and datetime are assignable to Sqldbtype.datetime, the null value taken in SQL is DBNull.Value and cannot be assigned to C # DateTime.

One big feature of forum is that when calling DB.cs's method, the type of the parameter is almost object. The format of each parameter is carefully defined in the SP and converted by SQL itself. For nullable parameters, define the =null in the SP. The forum passes a null parameter in a lot of cases, always in the receiver to determine the parameters, some parameters are null to write to the database, but some parameters if NULL is other meaning, you need conditional branch code to handle. And I'm used to initializing parameters first, and if there are different scenarios, the same method is used to add a parameter to judge. I think it's not a good idea to make the argument null, because it can cause some unclear conditional branching, potentially dangerous for future upgrades and maintenance.

3. The following statements enable insertion:

Insert into Employees (lastname,firstname,hiredate) VALUES (' Hu ', ' Shuai ', null),

Insert into Employees (lastname,firstname,hiredate) VALUES (' Hu ', ' Shuaishuai ', ')

Have a distinctly different result. Can be implemented on SQL statements. But in the program, especially object-oriented program design, in addition to the three-tier architecture of some problems. is inconvenient to achieve. So I'm trying to find a way to do it smartly, and I hope you give some advice.

Three.

The simplest method:

Assign a default date to the page

Determines whether the parameter value is equal to the default date if the parameter is assigned a value in an SQL statement DBNull.Value

Study on Datatime Assignment value in ASP (C #)

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.