C# 简单预约系统的SQL查询

C# 简单预约系统的SQL查询,c#,sql,sql-server,asp.net-mvc,C#,Sql,Sql Server,Asp.net Mvc,我有一个简单的预订系统。ReservationObject是一个平面结构,由StartDate、EndDate、Resource和其他属性组成 我正在尝试获取资源使用情况的报告。对于这个报告,我只是简单地查询StartDate,以查找具有报告查询范围的所有预订并计算它们 示例SQL查询是 "SELECT * FROM RequestsQueueObjects WHERE PODObjectID='" +cPOD.PODObjectID + "' and StartDateTime &

我有一个简单的预订系统。ReservationObject是一个平面结构,由StartDate、EndDate、Resource和其他属性组成

我正在尝试获取资源使用情况的报告。对于这个报告,我只是简单地查询StartDate,以查找具有报告查询范围的所有预订并计算它们

示例SQL查询是

"SELECT * 
 FROM RequestsQueueObjects 
 WHERE PODObjectID='" +cPOD.PODObjectID + "' 
 and StartDateTime >= '" + StartDate + "' 
 and StartDateTime <= '" + EndDate + "'";
“选择*
来自RequestsQueueObjects
其中PODObjectID='“+cPOD.PODObjectID+”'
和StartDateTime>='“+StartDate+”'
和开始日期编辑

您要检查日期范围是否重叠。请尝试以下操作:

"SELECT * 
 FROM RequestsQueueObjects 
 WHERE PODObjectID='" +cPOD.PODObjectID + "' 
 and StartDateTime <= '" + EndDate+ "' 
 and EndDateTime >= '" + StartDate + "'";
“选择*
来自RequestsQueueObjects
其中PODObjectID='“+cPOD.PODObjectID+”'
而StartDateTime='“+StartDate+'”;
(结合Steve参数化查询建议)

编辑

您要检查日期范围是否重叠。请尝试以下操作:

"SELECT * 
 FROM RequestsQueueObjects 
 WHERE PODObjectID='" +cPOD.PODObjectID + "' 
 and StartDateTime <= '" + EndDate+ "' 
 and EndDateTime >= '" + StartDate + "'";
“选择*
来自RequestsQueueObjects
其中PODObjectID='“+cPOD.PODObjectID+”'
而StartDateTime='“+StartDate+'”;

(结合Steve参数化查询建议)

使用参数化查询,让数据库引擎计算出您经过的日期。
在您的代码中,您在逗号和之间写入日期,这让数据库引擎根据其本地化规则来翻译它们。当然,这可能会导致完全错误的结果或丢失记录

DateTime startDate = new DateTime(2015, 2, 1);
DateTime endDate = new DateTime(2015, 7, 1, 23, 59, 59); // add time to Include all day

string cmdText = @"SELECT * FROM RequestsQueueObjects 
                   WHERE PODObjectID = @id 
                   and StartDateTime >= @start
                   and StartDateTime <= @end";
using(SqlConnection cn = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand(cmdText, cn))
{
    cn.Open();
    cmd.Parameters.Add("@id", cPOD.PODObjectID);
    cmd.Parameters.Add("@start", StartDate);
    cmd.Parameters.Add("@start", EndDate);
    using(SqlDataReader reader = cmd.ExecuteReader())
    {
        ....
    }
}
DateTime startDate=新的日期时间(2015,2,1);
DateTime endDate=新的日期时间(2015,7,1,23,59,59);//添加时间以包括全天
string cmdText=@“从RequestsQueueObjects中选择*
其中PODObjectID=@id
和StartDateTime>=@start

和StartDateTime使用参数化查询,让数据库引擎计算出您经过的日期。
在代码中,您可以在逗号和之间写入日期,这样数据库引擎就可以根据其本地化规则对其进行翻译。当然,这可能会导致完全错误的结果或丢失记录

DateTime startDate = new DateTime(2015, 2, 1);
DateTime endDate = new DateTime(2015, 7, 1, 23, 59, 59); // add time to Include all day

string cmdText = @"SELECT * FROM RequestsQueueObjects 
                   WHERE PODObjectID = @id 
                   and StartDateTime >= @start
                   and StartDateTime <= @end";
using(SqlConnection cn = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand(cmdText, cn))
{
    cn.Open();
    cmd.Parameters.Add("@id", cPOD.PODObjectID);
    cmd.Parameters.Add("@start", StartDate);
    cmd.Parameters.Add("@start", EndDate);
    using(SqlDataReader reader = cmd.ExecuteReader())
    {
        ....
    }
}
DateTime startDate=新的日期时间(2015,2,1);
DateTime endDate=新的日期时间(2015,7,1,23,59,59);//添加时间以包括全天
string cmdText=@“从RequestsQueueObjects中选择*
其中PODObjectID=@id
和StartDateTime>=@start

和StartDateTime您应该使用具有以下逻辑的参数化查询:

"SELECT * 
         FROM RequestsQueueObjects 
         WHERE PODObjectID= @PODObjectID  
         and ((StartDateTime >= @StartDate and EndDateTime <= @EndDate)
         OR (StartDateTime <= @StartDate and EndDateTime >= @StartDate)
         OR (StartDateTime <= @EndDate and EndDateTime >= @EndDate)
         OR (StartDateTime <= @StartDate and EndDateTime >= @EndDate))
    ";
“选择*
来自RequestsQueueObjects
其中PODObjectID=@PODObjectID

和((StartDateTime>=@StartDate和EndDateTime=@StartDate和StartDateTime您应该使用参数化查询,并具有以下逻辑:

"SELECT * 
         FROM RequestsQueueObjects 
         WHERE PODObjectID= @PODObjectID  
         and ((StartDateTime >= @StartDate and EndDateTime <= @EndDate)
         OR (StartDateTime <= @StartDate and EndDateTime >= @StartDate)
         OR (StartDateTime <= @EndDate and EndDateTime >= @EndDate)
         OR (StartDateTime <= @StartDate and EndDateTime >= @EndDate))
    ";
“选择*
来自RequestsQueueObjects
其中PODObjectID=@PODObjectID

及((StartDateTime>=@StartDate和EndDateTime=@StartDate和StartDateTime
查看两者之间的关系
小心使用
=和=StartDate和StartDateTime
,我也会将该查询更改为使用
参数化查询
嗨,DJ KRAZE,我如何在预订StartDate和预订之间使用BetweendDate.你能给我举个例子吗。
ZOO
是什么阻止你在
SQL示例间进行
Google搜索
@DJKRAZE我会小心地建议使用BETWEEN。这是一个准备以丑陋的方式启动的陷阱。当然,这取决于对这些问题的了解有多深。
查看BETWEEN
小心使用
=and=StartDate和StartDateTime
,我也会将该查询更改为使用
参数化查询ZE,我如何在预订开始日期和预订结束日期之间使用Between。请给我举个例子。
ZOO
是什么阻止了你在
SQL-Between-Examples上进行
Google搜索
请你多做点努力。@DJKRAZE你不应该建议任何人在Between-Examples上使用(请参阅)@DJKRAZE我会小心地建议使用BETWEEN。这是一个准备以丑陋的方式踢出的陷阱。当然,这取决于对这些问题的了解有多深。不。当周期在选定的开始日期之后结束,并且在选定的结束日期之前开始时,它与选定的日期范围重叠。(想想看:)谢谢大家。我用英语写下了这个查询,但不确定它是否是答案…Reservation EndDate或STartDAte>=Report STartDAte…和Reservation STartDAte或Rendate Nop。当周期在所选开始日期之后结束,在所选结束日期之前开始时,它与所选日期范围重叠。(想想看:)谢谢大家。我用英语写下了这个查询,但不确定它是否是答案…Reservation enddate或STartDAte>=Report STartDAte…和Reservation StartDateOREndate