[英]Update Access database using Visual Studio 2015 - VB.net
I am trying to do a simple update to an Access 2016 database. 我正在尝试对Access 2016数据库进行简单更新。 I am using Visual Studio/VB.net.
我正在使用Visual Studio / VB.net。 I have been able to do this already on a different form with no issues using the same type of coding (it's pretty basic, it was for a school project but not anymore).
我已经能够使用相同类型的编码以不同的形式进行此操作,而且没有任何问题(这很基本,这是用于学校项目的,但现在不再可用)。 I have tried two different ways to do this...using the update table adapter, for example:
我尝试了两种不同的方法来执行此操作...例如,使用更新表适配器:
MediatorsListTableAdapter.UpdateMediators(MediatorIDTextBox.Text, MediatorNameTextBox.Text, MaskedTextBox1.Text, MaskedTextBox2.Text, DateTimePicker1.Value,
AvailabilityTextBox.Text, EmailTextBox.Text)
Using that method I always get a notImplemented exception thrown even though I have used a similar type of adapter elsewhere. 使用该方法,即使我在其他地方使用了类似类型的适配器,也总是会抛出notImplemented异常。 Also I tried using a strung method (I know, not ideal):
我也尝试使用串连方法(我知道,不理想):
saveInfo = "UPDATE mediatorsList(mediatorName, email, mediatorPrimaryPhone, mediatorSecondaryPhone, lastMediationDate, availability)
VALUES('" & MediatorNameTextBox.Text & "','" & EmailTextBox.Text & "','" & MaskedTextBox1.Text & "','" & MaskedTextBox2.Text & "',
'" & DateTimePicker1.Value & "','" & AvailabilityTextBox.Text & "', WHERE mediatorID = '" & MediatorIDTextBox.Text & "') "
But this method gives me the error of Syntax Error in UPDATE statement . 但是这种方法给了我UPDATE语句中语法错误的错误 。 Again I have used this method elsewhere with no problems.
我再次在其他地方使用了此方法,没有任何问题。 Below I will post all the code for this form.
下面,我将发布此表单的所有代码。
Imports System.Data
Imports System.Data.Odbc ' Import ODBC class
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class editMediators
Dim NewData As Boolean
Dim objConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ECRDatabase.accdb")
' create functions for save or update
Private Sub runAccessSQL(ByVal sql As String)
Dim cmd As New OleDbCommand
connect() ' open our connection
Try
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
MsgBox("Data Has Been Saved !", vbInformation)
Catch ex As Exception
MsgBox("Error when saving data: " & ex.Message)
End Try
End Sub
Private Sub editMediators_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.MediatorsListTableAdapter.Fill(Me.ECRDatabaseDataSet.mediatorsList) 'loads current mediator information
DateTimePicker1.Value = Today()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'update button
NewData = True
alertMsgBox2()
End Sub
Private Sub alertMsgBox2()
Select Case MsgBox("Yes: Saves Changes," & vbNewLine &
"No: Exits the mediator update window without saving," & vbNewLine &
"Cancel: Returns to the mediator update window.", MsgBoxStyle.YesNoCancel, "Update Mediator Information")
Case MsgBoxResult.Yes
MediatorsListBindingSource.EndEdit()
updateMediator()
'intentionally commented out
'MediatorsListTableAdapter.UpdateMediators(MediatorIDTextBox.Text, MediatorNameTextBox.Text, MaskedTextBox1.Text, MaskedTextBox2.Text, DateTimePicker1.Value,
'AvailabilityTextBox.Text, EmailTextBox.Text)
' Me.Close()
Case MsgBoxResult.No
MediatorsListBindingSource.CancelEdit()
Me.Close()
End Select
End Sub
Private Sub updateMediator()
Dim saveInfo As String
If NewData Then
Dim Message = MsgBox("Are you sure you want to update mediator information? ", vbYesNo + vbInformation, "Information")
If Message = vbNo Then
Exit Sub
End If
Try
'Update mediator information
saveInfo = "UPDATE mediatorsList(mediatorName, email, mediatorPrimaryPhone, mediatorSecondaryPhone, lastMediationDate, availability)
VALUES('" & MediatorNameTextBox.Text & "','" & EmailTextBox.Text & "','" & MaskedTextBox1.Text & "','" & MaskedTextBox2.Text & "',
'" & DateTimePicker1.Value & "','" & AvailabilityTextBox.Text & "', WHERE mediatorID = '" & MediatorIDTextBox.Text & "') "
Catch ex As Exception
End Try
Else
Exit Sub
End If
runAccessSQL(saveInfo)
End Sub
There is obviously something I am missing, though I am not sure it is missing from the code. 尽管我不确定代码中是否缺少某些东西,但是显然我缺少了一些东西。 I checked my database fields and set them to string/text fields just to see if I could get it working.
我检查了数据库字段,并将它们设置为字符串/文本字段,以查看是否可以正常运行。 At one time, I had two 2 phone number fields that were set to to the wrong data type so you could only enter a number per int32 requirements.
一次,我将两个2电话号码字段设置为错误的数据类型,因此您只能按照int32要求输入一个数字。 I actually had one of these methods working/updating the db several months ago but I can't figure out what happened since.
实际上,几个月前,我有一种方法正在工作/更新数据库,但是我不知道此后发生了什么。 I do know Visual Studio gave me some problems which probably contributed but it's been too long to remember what happened.
我确实知道Visual Studio给了我一些可能引起问题的问题,但是记住发生的事情已经太久了。
I am rather lost on what else to try as this seems like it should work one way or another. 我很迷失其他尝试方法,因为这似乎应该以一种或另一种方式起作用。 Any ideas what to look at and/or try??
有什么想法要看和/或尝试吗? Hopefully I can be pointed in the right direction.
希望我能指出正确的方向。
Thanks :) 谢谢 :)
Your update statement is incorrect, the WHERE clause is inside the VALUES() segment, and should be after it. 您的更新语句不正确,WHERE子句位于VALUES()段内,应该在它之后。
Try this instead: (Edited) 尝试以下操作:(已编辑)
saveInfo = "UPDATE mediatorsList SET mediatorName='" & _
MediatorNameTextBox.Text & "', email='" & EmailTextBox.Text & "', .... WHERE " & _
mediatorID = '" & MediatorIDTextBox.Text & "'"
Also be sure to handle the date correctly. 另外,请务必正确处理日期。 I usually force formatting in yyyy/mmm/dd format.
我通常会强制采用yyyy / mmm / dd格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.