[英]Comparing two datetimes to check for a clash but seems to be confusing MM/DD with DD/MM C# ASP.NET
我正在嘗試檢查數據庫是否存在沖突,正在使用閱讀器進行讀取並調低值。 我的值是10/12/2018 15:00:00
所以我將其存儲為@moduleStartTime
我的下一個查詢使用上述日期時間以及是否在輸入的任何其他日期之間檢查數據庫。
我遇到的問題是沒有沖突,所以這不應該表明存在沖突。 我發現它返回的沖突的日期時間為12/10/2018 15:00:00
好像在搜索中的某處,它正在反轉DD / MM
這是我的代碼
//Reading to get all modules student is enrolled on
string checkclash = "SELECT * FROM cModuleTimes INNER JOIN cStudentModule ON cModuleTimes.ModuleID = cStudentModule.ModuleID WHERE cStudentModule.StudentID=@studentid AND @date BETWEEN[StartTime] AND[EndTime] AND ModTimeID <> @modtimeid";
SqlCommand myCommandclash = new SqlCommand(checkclash, myConnectionclash);
myCommandclash.Parameters.AddWithValue("@date", moduleStartTime);
myCommandclash.Parameters.AddWithValue("@courseid", courseid);
myCommandclash.Parameters.AddWithValue("@studentid", user);
myCommandclash.Parameters.AddWithValue("@modtimeid", moduletocompareid);
//create a sqldatareader object that asks for dats from a table
SqlDataReader rdrreadclash = myCommandclash.ExecuteReader();
if (rdrreadclash.HasRows)
{
while (rdrreadclash.Read())
{
string getname = rdrreadclash["ModTimeID"].ToString();
string gettime = rdrreadclash["StartTime"].ToString();
Warning.Text = "There appears to be a clash with this event..<br><br> <br> <b>Would you like to continue?</b> <br><br>";
}
ViewClash.Visible = true;
YesContinue.Visible = true;
FinishMod.Visible = false;
}
myConnectionclash.Close();
我嘗試了幾次轉換,但是收到一個字符串,該字符串未被識別為DateTime的問題。
如果有人對我如何防止這種沖突的出現有任何答案,我將不勝感激。
謝謝。
moduleDateTime
當前為字符串,您需要將其解析為DateTime
對象,其中解析操作具有所需的正確MM / DD順序。
DateTime dateAsDateTime = DateTime.ParseExact(moduleStartTime, "dd/MM/yyyy HH:mm:ss", null);
myCommandclash.Parameters.AddWithValue("@date", dateAsDateTime);
然后,您必須將返回的日期時間格式化為所需格式的字符串。
string gettime = ((DateTime)rdrreadclash["StartTime"]).ToString("dd/MM/yyyy HH:mm:ss ");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.