使用Visual Studio 2015更新Access数据库 - VB.net

时间:2017-03-31 15:42:19

标签: vb.net visual-studio-2015 ms-access-2016

我正在尝试对Access 2016数据库进行简单更新。我正在使用Visual Studio / VB.net。我已经能够以不同的形式做到这一点,使用相同类型的编码没有任何问题(它非常基本,它适用于学校项目但不再是)。我尝试了两种不同的方法...使用更新表适配器,例如:

            MediatorsListTableAdapter.UpdateMediators(MediatorIDTextBox.Text, MediatorNameTextBox.Text, MaskedTextBox1.Text, MaskedTextBox2.Text, DateTimePicker1.Value,
            AvailabilityTextBox.Text, EmailTextBox.Text)

使用该方法我总是会抛出 notImplemented 异常,即使我在其他地方使用了类似类型的适配器。我也尝试使用串联方法(我知道,不理想):

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 & "') "

但是这个方法在UPDATE语句中给出了语法错误的错误。我再次在其他地方使用这种方法没有问题。下面我将发布此表格的所有代码。

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

显然我缺少一些东西,但我不确定代码中是否遗漏了它。我检查了我的数据库字段并将它们设置为字符串/文本字段,以查看是否可以使其正常工作。有一段时间,我有两个2个电话号码字段设置为错误的数据类型,因此您只能输入每个int32要求的数字。几个月前,我实际上有其中一种方法正在使用/更新数据库,但我无法弄清楚自那以后发生了什么。我确实知道Visual Studio给了我一些可能有所贡献的问题,但它记忆太久了。

我很遗憾还要尝试其他什么,因为这似乎应该以某种方式起作用。任何想法看什么和/或尝试?希望我能指出正确的方向。

谢谢:)

1 个答案:

答案 0 :(得分:0)

您的更新语句不正确,WHERE子句在VALUES()段内,应该在它之后。

试试这个: (编者)

saveInfo = "UPDATE mediatorsList SET mediatorName='" & _
    MediatorNameTextBox.Text & "', email='" & EmailTextBox.Text & "', .... WHERE " & _
    mediatorID = '" & MediatorIDTextBox.Text & "'"

还要确保正确处理日期。我通常强制格式化为yyyy / mmm / dd格式。