錯誤:動態SQL生成的更新命令沒有針對不返回任何鍵列信息MS ACCESS - 需要讀取和更新
我試圖解決以下problem-讓我申請到的SelectCommand支持寫回我的數據庫。該應用程序正在擊中上面列出的例外標題。我一直在閱讀有關數據庫的CRUD操作 - http://www.homeandlearn.co.uk/NET/nets12p7.html
我知道從其他閱讀我已經做到了,我需要一個主鍵。有人可以解釋一下當你使用數據集和數據適配器時,這是如何相關的?這個錯誤與我的實際數據庫或內存數據集有關嗎? 我也意識到我已經創建了基於一個查詢的數據集。當我達到未找到票證條件時,是否需要另一個查詢(INSERT)?
Private Sub BtnQuery_Click(sender As Object, e As EventArgs) Handles BtnQuery.Click
sql = "SELECT [Ticket ID] AS Ticket_ID , [Foundstone] AS Foundstone, [ID] AS ID FROM [Table MAIN] WHERE ([Ticket Days OverDue] >= 0)" 'define the query
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "MAIN")
If ds.Tables("MAIN").Rows.Count > 0 Then
TxtRows.Text = ds.Tables("MAIN").Rows.Count
maxrows = Val(TxtRows.Text.ToString)
End If
For i = 0 To maxrows - 1
If i >= 0 Then
result = ds.Tables("MAIN").Rows(i).Item("Ticket_ID")
WebBrowser1.Navigate("https://fs-enterprise.my.private.url/remediation/ticket.exp?ticket=" & result)
Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
WebBrowser1.AllowNavigation = True
'<div id="MessageGood_0" class="mvm-status-message msm-msg msm-msg-img" style="background-image:url(/images/fam/cross.png);">The specified ticket does not exist, or is not currently available.</div>
For Each el As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If (el.GetAttribute("id").Equals("MessageGood_0")) Then
TxtTicket.Text = ds.Tables("MAIN").Rows(i).Item("Ticket_ID")
'Try
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "[" 'allows update if table name or field is a reserved word in MS Access
cb.QuoteSuffix = "]" 'allows update if table name or field is a reserved word in MS Access
ds.Tables("MAIN").Rows(i).Item("Foundstone") = "Not Found"
da.Update(ds, "MAIN")
da.UpdateCommand = cb.GetUpdateCommand()
MsgBox("Ticket Not Found")
'Catch ex As Exception
'MsgBox(ex.Message.ToString, , "Error")
'End Try
Else
'<input class="boldbutton" type="button" value="Verify" onclick="this.form.knob.value='ReqVerify';this.form.verify.value=1;this.form.submit()"></td>
TxtTicket.Text = ds.Tables("MAIN").Rows(i).Item("Ticket_ID")
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("INPUT")
If (element.GetAttribute("value").Equals("Verify")) Then
element.InvokeMember("click")
End If
Next
End If
Next
End If
Next
MessageBox.Show("All Tickets Have Been Processed")
Me.Close()
con.Close()
End Sub
主鍵需要在您訪問數據庫表('main'在此設置例)。您不應該需要單獨的查詢來添加新行。您可以將該行添加到您的內存數據集中,然後進行更新。 – theduck
@theduck。謝謝,我明白主鍵需要放在我正在訪問的表格中。我試圖在我的腦海中澄清這個問題 - 密鑰是否設置在實際的數據庫或數據集(數據庫的內存表示)中?我不需要添加任何額外的數據行,只需回寫到已存在行中沒有數據的列。 –
主鍵需要在實際的數據庫表上設置,而不是在內存表示中。如果您不需要添加新行,您應該能夠更新數據集並將更改提交回數據庫。 – theduck