我得到一個「語法錯誤在INSERT INTO語句,但我想不出爲什麼。語法錯誤INSERT INTO語句中同時使用日期時間
我比較了聲明,別人我做的工作找,能找到沒有結構上的區別,唯一的區別是,在這一個,我試圖在我的MS Access數據庫中保存DateTime
我有我的數據庫字段設置爲日期/時間的一般格式, MM/DD/YY HH:MM:SS AM(或PM)。我的日期時間格式設置相同。
當我設置一箇中斷點並按照它,可以看到正在傳遞的DateTime格式是正確的並且與MS Access中的一般格式相匹配。
我確定這個問題對我來說是正確的,但如果我能看到它,我將會感到厭煩。這裏是我的代碼:
//method to save user input to database
public static void SaveData(ProgramLoginBOL busObject)
{
try
{
String sSQLCommand = "INSERT INTO ProgramLogin (" +
"UserName, DateTime) VALUES ('" + busObject.UserName +
"','" + busObject.DtDate + "')";
if (aConnection.State == ConnectionState.Closed)
{
aConnection.Open();
}
OleDbCommand cmd = aConnection.CreateCommand();
cmd.CommandText = sSQLCommand;
// Execute the SQL command
cmd.ExecuteNonQuery();
aConnection.Close();
MessageBox.Show("Data Saved");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
MessageBox.Show("Error! Data was not saved.");
}
}
正如我所說,調試表明busObject.DtDate
匹配的MS訪問日期/時間格式來解決問題
您有一個SQL注入漏洞。你需要使用參數。 – SLaks 2012-04-27 15:36:26
謝謝你的幫助。 – 2012-04-27 15:45:41