環境 單聲道和PostgreSQL,淨MVC中,Windows淨DateTime.Now VS PostgreSQL的時間戳比較
我試圖證明在未來發生的所有事件。
爲了做到這一點,我使用的SQL語句:
NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM dinners WHERE EventDate >= " + DateTime.Now, dinnerConn);
現在,如果我比較DateTime.Now和我EVENTDATE時間戳從數據庫中,我得到以下
(EventDate) 12/18/2010 7:00:00 PM - (DateTime.Now) 10/2/2010 7:59:48 PM
他們似乎很容易比較給我,但每當我運行此查詢,我得到如下:
ERROR: 42601: syntax error at or near "8"
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Npgsql.NpgsqlException: ERROR: 42601: syntax error at or near "8"
Source Error:
Line 106: try
Line 107: {
Line 108: NpgsqlDataReader reader = command.ExecuteReader();
Line 109: while (reader.Read())
Line 110: {
Source File: c:\Documents and Settings\Andrew\My Documents\Visual Studio 2008\Projects\MvcApplication1\MvcApplication1\Models\DBConnect.cs Line: 108
現在,我知道該應用程序的其他功能,因爲如果我要求它將所有晚餐或所有晚餐都大於特定ID或特定晚餐,那麼一切正常,它似乎只是將時間戳與DateTime.Now進行比較。
我知道這很簡單。我究竟做錯了什麼?
sql參數的字符串concatenation是邪惡的。不要這樣做。 – 2010-10-03 00:37:08