2013-05-02 123 views
1

我有一個簡單的asp.net表單供個人填寫,而我在此特定頁面上使用的代碼可以在其他幾個位置使用,但此頁面正在給出我是個問題。嘗試向Access數據庫中插入新記錄時出現SQL語法錯誤

這就是說在INSERT語句中有語法錯誤。你能看到它有什麼問題嗎?

單擊事件代碼:

cmdInsert.CommandText = "INSERT INTO Position (Com_ID, Stu_ID, Pos_StartDate, Pos_Type, Pos_Description, Pos_Title) VALUES (' " & ddlCompany.SelectedValue & " ', ' " & ddlStudent.SelectedValue & " ', #" & CalStartDate.SelectedDate.Date & "#, ' " & ddlPositionType.SelectedValue & " ', ' " & txtDescription.Text & " ', ' " & txtPositionTitle.Text & " ');" 

'MsgBox(cmdInsert.CommandText) 
cmdInsert.CommandType = CommandType.Text 
cmdInsert.Connection = cnnOLEDB 
cmdInsert.ExecuteNonQuery() 

txtPositionTitle.Text = "" 
txtDescription.Text = "Record inserted." 
CalStartDate.SelectedDates.Clear() 
cmdInsert.Dispose() 

從表單線與在數據庫中的數據類型中捕獲的數據。有任何想法嗎?

這裏的堆棧跟蹤:

[OleDbException (0x80040e14): Syntax error in INSERT INTO statement.] 
    System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +1081356 
    System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +247 
    System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +194 
    System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +58 
    System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +167 
    System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +113 
    Tiger_Recruiting.WebForm3.btnSaveStudentRecord_Click(Object sender, EventArgs e) in C:\Users\Garrett\Downloads\Tiger Recruiting(3)\Tiger Recruiting(3)\Tiger Recruiting(3)\Tiger Recruiting\Tiger Recruiting\position.aspx.vb:28 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +141 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +149 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +39 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +37 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +87 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4225 
+0

我看不出有什麼毛病查詢。但是,最好定義一個保存查詢的字符串變量,而不是直接將其分配給'CommandText'。這將幫助你判斷語法的錯誤。 – shahkalpesh 2013-05-02 18:53:42

+0

我會指責Pos_StartDate,訪問需要特定格式的日期。 - 你可以發佈一個由連接產生的INSERT語句的例子嗎? – Chris 2013-05-02 18:58:39

+0

@shahkalpesh,這可能是一個好主意。你的意思是定義它像「@StartDate」或類似的東西?我已經看到人們聲明每個字段而不是使用CommandText。 – Sev09 2013-05-02 19:07:19

回答

2

單詞位置在MS-訪問噴氣保留關鍵字。
你使用它的表名因此,你需要用方括號

cmdInsert.CommandText = "INSERT INTO [Position] ....... 

的一部分從這個,你的代碼是非常糟糕的封裝它。不要使用字符串cancatenation來構建sql命令。
如果在輸入中有單引號或者您有其他字段需要輸入值的特定格式,則此練習會導致語法錯誤。但最糟糕的是SQL注入Look here for a funny explanation

的所以你的代碼應該寫這樣的問題:

cmdInsert.CommandText = "INSERT INTO [Position] (Com_ID, Stu_ID, Pos_StartDate, Pos_Type, " + 
         "Pos_Description, Pos_Title) VALUES " + 
         "(?,?,?,?,?,?)" 
cmdInsert.Parameters.AddWithValue("@p1",ddlCompany.SelectedValue) 
cmdInsert.Parameters.AddWithValue("@p2",ddlStudent.SelectedValue) 
cmdInsert.Parameters.AddWithValue("@p3",CalStartDate.SelectedDate.Date) 
cmdInsert.Parameters.AddWithValue("@p4",ddlPositionType.SelectedValue) 
cmdInsert.Parameters.AddWithValue("@p5",txtDescription.Text) 
cmdInsert.Parameters.AddWithValue("@p6",txtPositionTitle.Text) 
cmdInsert.Connection = cnnOLEDB 
cmdInsert.ExecuteNonQuery() 
+0

就是這樣!我不敢相信這很簡單。感謝您的幫助,我真的很感激。我一直使用stackoverflow解決我遇到的問題,但這是我問的第一個問題。不失望。我也使用了你所建議的參數。再次感謝。 – Sev09 2013-05-02 19:56:44

+0

很高興能有所幫助。 – Steve 2013-05-02 19:57:56

相關問題