c# - 防止 SQL 重复预订

标签 c# sql sql-server

我正在尝试防止预订系统(C# 到 SQL Server)中的重复预订。

我正在使用存储过程并尝试了以下操作:

alter proc Datumkoll
    @DatumFrom smalldatetime = null,
    @datumTill smalldatetime = null
as
    select RumsNummer 
    from rum 
    where 
        RumsNummer not in (select rumsnummer 
                           from rumsbokning 
                           where datumfrån >= @datumfrom and datumtill <= @datumTill)

 GO

如果日期是在现有预订中设置的,则此代码不会阻止重复预订

最佳答案

想象一下时间窗口和选项:

不重叠:

[new]
         [existing]

部分重叠:

[new]                 [new]
  [existing]   [existing]

遏制:

    [new]       [-------new---------]
  [existing]         [existing]

您的支票仅处理最后一个;你需要回去考虑其他人。诀窍在于实际上只需要 3 项测试:

  • 新的开始日期位于现有预订中
  • 新的结束日期位于现有预订中
  • 现有预订完全包含在新预订中

这给了我们:

where (datumfrån>=@datumfrom and datumtill <= @datumTill)
or (@datumfrom >= datumfrån and @datumfrom <= datumtill)
or (@datumTill >= datumfrån and @datumfrom <= datumtill)

(另请参阅替代结构的评论)

关于c# - 防止 SQL 重复预订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395573/

相关文章:

c# - 如何创建将结果返回到文本文件的 MySql 数据库连接?

c# - 获取记事本的值并将其放入 c# 字符串中?

c# - 从文本框中获取特定值到 GridView ?

sql-server - int 类型的算术溢出错误,值 = 4957500001.400178

python - 如何解决使用可信连接连接到 MS Sql Server 的 Mac OS 上的票证过期错误

c# - Winnovative Html to pdf Converter 页码在附加文档上不正确

c# - EF中Add-Migration中StartupProjectName参数的用途是什么

mysql - WHERE 列 IN CASE 类型 WHEN

sql - 将 bool 反馈列替换并压缩为 BigQuery 中的单个分数列

sql - 如何通过 SQL 查询从 XML 中的键值对检索值?