此代码计算 MSSQL 表中日期介于今天和今天 + 8 天之间的记录数,但它不起作用;它返回值 0
,但 2
是正确答案。
如果我将 DateTime.Now.AddDays
更改为 7
或更小,它会正常工作。
//Ordre klar til bestilling
command.CommandText = "SELECT COUNT(*) from bestillinger WHERE udlevering BETWEEN @date and @dateadd";
command.Parameters.AddWithValue("@date", DateTime.Now.ToString("dd/MM/yyyy"));
command.Parameters.AddWithValue("@dateadd", DateTime.Now.AddDays(+8).ToString("dd/MM/yyyy"));
con.Open();
command.ExecuteNonQuery();
string result0 = command.ExecuteScalar().ToString();
con.Close();
MessageBox.Show(result0);
if (result0 != "0")
{
bestillingToolStripMenuItem.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF1919");
}
最佳答案
不要将日期/时间视为字符串。只是:
command.Parameters.AddWithValue("@date", DateTime.Now);
command.Parameters.AddWithValue("@dateadd", DateTime.Now.AddDays(8));
问题很可能是日期/时间格式。
请注意,您实际上无缘无故地执行了两次;您可以删除 command.ExecuteNonQuery()
。
最后,不要将整数视为字符串:
int count = (int)command.ExecuteScalar();
if(count != 0) { .... }
关于c# - 将天数添加到 DateTime 返回值为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26000114/