gpt4 book ai didi

c# - 检查时间范围是否在 C# 中相交

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:22 25 4
gpt4 key购买 nike

<分区>

我目前正在用 C# 开发一个简单的注册系统,我在考虑是否有一种直接的方法来检查我的输入 session 的特定时间范围(从 - 到)是否与数据库中的时间范围相交。

场景如下:

我的数据库中有一个名为 tblSessionList 的表,它存储每个主题的不同 session ,我想在每次用户添加新 session 时检查是否有特定的 Room已经占据了那个特定的时间范围。从技术上讲,我想检查选择的时间范围是否与 tblSessionList 中的 session 不冲突。

Add Session Form

tblSessionList

 | Subject Code |   Venue    |   Room   |        Date             |  From   |   To    |
| | | | | | |
| CRGOV401 | GT Tower | Room 3D | Thursday, June 19, 2014 | 8:00 AM |10:00 AM |
| CRGOV401 | GT Tower | Room 59 | Sunday, June 29, 2014 | 1:00 PM |3:00 PM |
| GNBNK201 | Main Plaza | HR Hall | Monday, June 30, 2014 | 9:00 AM |11:00 AM |
| GNBNK201 | Main Plaza | HR Hall | Monday, June 30, 2014 | 1:00 PM |3:00 |

我已尝试执行以下操作:

 private void btnAddSession_Click(object sender, EventArgs e)
{
bool proceedCopy = true;

DataTable timeHolder = new DataTable();
SqlDataAdapter selectTime = new SqlDataAdapter("select [From],[To] from tblSessionList where [Venue] = @venue and [Room] = @room", Connection.conn);
selectTime.SelectCommand.Parameters.AddWithValue("@venue", venue.Text);
selectTime.SelectCommand.Parameters.AddWithValue("@room", room.Text);

selectTime.Fill(timeHolder);


for(int i = 0; i < timeHolder.Rows.Count; i++)
{

if(timeHolder[i][0].toString() == From.Text) //checks if the 'From' of the query is the same with the 'From' of the new entry
{
MessageBox.Show("The room is already occupied on that time range");
proceedCopy = false;
break;
}

else if(timeHolder[i][1].toString() == To.Text) //checks if the 'To' of the query is the same with the 'To' of the new entry.
{
MessageBox.Show("The room is already occupied on that time range");
proceedCopy = false;
break;
}
}
if(proceedCopy == true)
//insert code to insert new session to database here
}

这是一个硬编码检查,它只检查时间范围的终点,而不是时间范围本身。我在想 C# 中是否有一个函数可以像 Time only 而不是 DateTime 那样使用。

注意:我数据库中的每一列都是一个 varchar。很抱歉,这是我的第一个系统,有脏代码。

如有任何帮助,我们将不胜感激。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com