2012-11-16 32 views
0

道歉多址表 - 我是一個新手到Visual Studio(和編程一般)如何從Visual Studio填充提前

我嘗試使用下面的代碼來填充兩個訪問表的業主和物業 - 但我一直收到

「在SQL語句末尾缺少分號(;)」。

有建議?代碼如下:

Dim aConnection As OleDbConnection 
Dim aCommand As OleDbCommand 
Dim aConnectionString, aQuery As String 
Dim Username As String = txtUsername.Text 
Dim Pword As String = TextBox2.Text 
Dim EmailDetails As String = TextBox9.Text 
Dim Question As String = DropDownList2.Text 
Dim Answer As String = TextBox4.Text 

aConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " _ 
& Server.MapPath("AppData/RMT.accdb") 
aConnection = New OleDbConnection(aConnectionString) 
aConnection.Open() 
aQuery = "Insert Into Landlord (Username, email, Security_Question, Security_Answer, Pword, Status) Values ('" & Username & "','" & EmailDetails & "','" & Question & "','" & Answer & "', '" & Pword & "','pending')Into Property Values (26,46,'marysway','MarysRd','Marysville','Cork North')" 
aCommand = New OleDbCommand(aQuery, aConnection) 
aCommand.ExecuteNonQuery() 

aConnection.Close() 
+0

'PWORD&「」,‘待定’)進入屬性值「 - 看起來很奇怪,但'我不是一個訪問專家。作爲一個常規提示:在你的OleDBCommand中使用OleDBParameters,它會使你的代碼更容易閱讀,並且更少出錯(嘗試插入日期;)) – igrimpe

回答

1

我不認爲Microsoft Access的ADO.NET提供程序在同一查詢字符串中支持多個插入語句。然而,只是爲了檢查,你可以嘗試寫

aQuery = "Insert Into Landlord (Username, email, Security_Question, Security_Answer, " & _ 
     "Pword, Status) Values ('" & Username & "','" & EmailDetails & "','" & _ 
     Question & "','" & Answer & "', '" & Pword & "','pending');" & _ 
     "Insert Into Property Values (26,46,'marysway','MarysRd','Marysville','Cork North')" 

注意第一條INSERT語句和第二個之間的分號(順便說一下,第二個INSERT關鍵字是缺少在你原來的代碼)

說,我建議在構建查詢文本傳遞給數據庫引擎時始終使用OleDbCommand的參數集合。
這將避免與文本的問題解析(在你輸入文本單引號打破一切),而且,你不要你的代碼暴露Sql Injection Attacks

Using aConnection = New OleDbConnection(aConnectionString) 
    aConnection.Open() 

    aQuery = "Insert Into Landlord (Username, email, Security_Question, " + 
      "Security_Answer, Pword, Status) Values (?, ?, ?,?,?,'pending')" 
    aCommand = New OleDbCommand(aQuery, aConnection) 
    aCommand.Parameters.AddWithValue("@usr", Username) 
    aCommand.Parameters.AddWithValue("@email", EmailDetails) 
    aCommand.Parameters.AddWithValue("@qst", Question) 
    aCommand.Parameters.AddWithValue("@ans", Answer) 
    aCommand.Parameters.AddWithValue("@pwd", Pword) 
    aCommand.ExecuteNonQuery() 

    aQuery = "Insert Into Property Values (26,46,'marysway','MarysRd','Marysville','Cork North')" 
    aCommand = New OleDbCommand(aQuery, aConnection) 
    aCommand.ExecuteNonQuery() 

End Using 
+0

謝謝爲你的幫助 - 這是給我一個消息,說:「在INSERT INTO語句中的語法錯誤」 - 回到繪圖板我認爲 – Yambo

+0

你是指雙插入語句,還是在第二個例子與分離的語句?在第二種情況下,是第一個插入還是第一個插入第二個失敗? – Steve

+0

其實史蒂夫 - 第二個選項完美工作,非常感謝您的幫助! – Yambo