Study on Null Value assignment of DataTime in asp.net (C,

Source: Internet
Author: User

Study on Null Value assignment of DataTime in asp.net (C,

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


Is ASPNET in the C/S architecture?

ASP. NET developed B/S
C # winfrom is C/S

ASPNET (# c) and DIV + CSS

Asp.net is a platform. I don't know what versions others use. I use 3.5.
The foreground is the *. aspx page. The background is the. cs page. On this. cs page, you can call the background class to call the database.
Div + css is just a way to implement page layout and styles. The page structure is based on whether the master page is used or what is used.
In fact, you don't have to think about how difficult they are. Only get data. Only data is displayed.

If you are working on the frontend, you do not need to consider the background. how do you write your css. as long as it is used on the aspx page. there is no need to confuse html controls and server controls. They only run on the client and on the server. the server control will eventually become html text. so what should you do. because you only do layout and effect. only on the client. so you don't have to worry about that.

Let me give you an intuitive example. A button (the property runat = "server" runs on the server, that is, the server control), which is on your aspx page. how can you add styles to it. finally, when the user clicks it, the Code he executes will be in. the click event of the button in the cs page. after clicking the button, it will automatically execute the click event. The content of the click event is the background service class, data operations, and other operations. isn't that how to associate the front-end page with the back-end page? In fact, the aspx page is only a display page. The. cs page is actually connected to the frontend and backend. This is also an advantage of asp.net. The post-code!

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.