如何比较Asp.net中的日期。 [英] How To Compare Date In Asp.net.

查看:96
本文介绍了如何比较Asp.net中的日期。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我为Indian Gas Cylinder Agency制作了一个asp.net项目。在这里,用户可以预订燃气





缸在线。但这是一个条件,用户一年内不能预订9个以上的气瓶。





如果他试图预订气瓶9点以后他应该收到一条消息。





怎么做?

解决方案

除了其他字段之外,您还需要在表格中包含以下两个字段:

1. number_cylinder(int type)假设用户每次预订可以购买多个字段

2. date_of_booking(日期时间类型)

当用户提交新预订时,运行sql查询以将number_cylinder与当前年份相加,参见示例:

从bookingtable中选择sum(number_cylinder)userid =somevalue
和year(date_of_booking)= year(getdate())


两个解决方案:

1.如果您想要每次预订的新记录超过:

添加列名称,如 BookingYear 。现在,每列用户手册中的新条目将在此列中显示当前日期年份。现在在保存或预订之前运行以下查询:



 选择 * < span class =code-keyword>来自 table1 其中 BookingYear = '  2014' 

(比如2014年)。



现在如果是行。此查询的计数大于等于9,而不是显示消息。



2.如果您想要一年一行,那么将列添加到表中,如'CylinderBookCount' 。每次用户通过增加此栏中的金额来制作一本书就可以了解这个表格。



现在在预订气瓶之前添加支票,如



  table1中选择 CylinderBookCount  其中 UserID = '  CopyOrAnyUniqueIDOfUser' BookingYear =   2014' 





现在,如果此返回列的值大于等于9,则显示消息。


您应该从该数据库表中为该用户选择该年份的所有预订数据,然后测试计数,比如在我使用EF的下一个代码中:



 DateTime now = DateTime.Now; 
var searchResults = dataContext.UserBookings.Where(c = > c.UserID == userID&& c.StartDate > = now);
//
if (searchResults.Count + newAmount < = 9
返回 true ; // 您可以启用新订单!


Hello guys,

I making one asp.net project for Indian Gas Cylinder Agency. Here A user can book gas


cylinder online. But here is one condition, a user can not book more than 9 cylinders in a year.


If he attempt to book a cylinder after 9th. He should get a message.


How to do this??

解决方案

You will need to have these 2 fields in the table in addition to other fields:
1. number_cylinder (int type) assuming user can buy more than one per booking
2. date_of_booking (datetime type)
When a user submits a new booking, run a sql query to sum the number_cylinder by current year, see example:

select sum(number_cylinder) from bookingtable where userid="somevalue" 
and year(date_of_booking) = year(getdate()) 


Two Solution:
1.If you want new record every time for booking than:
Add a column name like BookingYear. Now every-time user book cylinder new entry will be made with current date's year in this column. Now run following query before saving or booking:

Select * from table1 where BookingYear = '2014'

(like for the year of 2014).

Now if the Rows.count for this query is greater than equal to 9, than show message.

2.If you want 1 row for a year then add column to table like 'CylinderBookCount'. Every time user make a book uodate this table by increasing the amount in this column.

Now before booking cylinder add check like

Select CylinderBookCount from table1 where UserID = 'CopyOrAnyUniqueIDOfUser' BookingYear = '2014' 



Now if this return column haveing value then greater than equal to 9, than show message.


You should select all booking data from your database table for that user for that year then test the count, like in the next code where I am using EF:

DateTime now = DateTime.Now;
var searchResults = dataContext.UserBookings.Where(c => c.UserID == userID && c.StartDate >= now);
//
if(searchResults.Count  + newAmount <= 9)
  return true; //You can enable the new order! 


这篇关于如何比较Asp.net中的日期。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆