有人可以幫我弄清楚爲什麼我收到此錯誤嗎?我在我的電腦上有相同的代碼,它工作正常,但在我的朋友的筆記本電腦上它不會工作。我們有相同的SQL Server日期時間格式。我已經嘗試將字符串日期格式轉換爲與SQL Server日期格式相同,但它仍然不起作用。將nvarchar數據類型轉換爲日期時間數據類型導致超出範圍值.3
編輯:我不好,我發佈了錯誤的代碼。
DateTime paymentCashDate = Convert.ToDateTime(txtPaymentCash.Text).AddDays(1).AddSeconds(-1);
DateTime orderDate = Convert.ToDateTime(lblDate.Text);
if (orderDate < paymentCashDate)
{
cmd.CommandText = "UPDATE Orders SET [email protected], [email protected], [email protected], [email protected] WHERE [email protected] AND Status='Pending'; " +
"UPDATE OrderDetails SET [email protected] WHERE [email protected] AND Status='Pending';";
cmd.Parameters.AddWithValue("@Status", "Approved");
cmd.Parameters.AddWithValue("@PaymentStatus", "Payment Accepted");
cmd.Parameters.AddWithValue("@PaymentDate", paymentCashDate.ToString());
cmd.Parameters.AddWithValue("@PaymentAmount", txtAmountCash.Text);
cmd.Parameters.AddWithValue("@OrderNo", orderNo);
cmd.ExecuteNonQuery();
con.Close();
addProject(orderNo);
Response.Redirect("Default.aspx");
}
else
{
errorPayment.Visible = true;
}
如果它是DB中的日期時間,爲什麼要傳遞'string'?另外,爲什麼要將'DateTime'對象格式化爲字符串來進行比較,尤其是因爲您沒有使用24小時的時間? – juharr
你應該看看[我們可以停止使用AddWithValue()了嗎?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)並停止使用'.AddWithValue()' - 它可能會導致意想不到的和令人驚訝的結果... –
可能有情況下,您傳遞無效日期,如'2017-06-31',這也會導致同樣的錯誤,同時轉換到datetime。 –