Access数据库中的错误更新sql语句[VB2010]

时间:2018-06-22 02:10:33

标签: vb.net

我的更新代码不起作用。当我运行它时,此行显示错误 str =“ Update Table1 set Mining Allowance ='”&Mining_AllowanceTextBox.Text&“',Basic ='”&BasicTextBox.Text&“',Accomodation =”&AccomodationTextBox .Text&“ WHERE ID =”&IDTextBox&“” 。错误消息是未为类型'String'和'System.Windows.Forms.TextBox'定义运算符'&'。
下面是我的代码。请解决此错误。

Imports System.Data.OleDb

Public Class Salary_Classification
    Dim provider As String
    Dim datafile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection


     Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
            datafile = "F:\EmployeeTB.mdb"
            connString = provider & datafile
            myConnection.ConnectionString = connString
            myConnection.Open()
            Dim str As String
            str = "Update Table1 set Mining Allowance='" & Mining_AllowanceTextBox.Text & "' ,Basic = '" & BasicTextBox.Text & "',Accomodation = " & AccomodationTextBox.Text & " WHERE ID =" & IDTextBox & ""
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

            Try
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                myConnection.Close()
                IDTextBox.Clear()

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

End Class

1 个答案:

答案 0 :(得分:0)

检查每个字段的数据类型,并将任何OleDbType参数调整为正确的数据类型。即使出现错误,Using语句也会关闭并处理您的对象。

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\EmployeeTB.mdb")
            Using cmd As OleDbCommand = New OleDbCommand("Update Table1 set Mining Allowance=?,Basic = ?,Accomodation = ? WHERE ID =?;", myConnection)
                cmd.Parameters.Add("@MiningAllowance", OleDbType.VarChar).Value = Mining_AllowanceTextBox.Text
                cmd.Parameters.Add("@Basic", OleDbType.VarChar).Value = BasicTextBox.Text
                cmd.Parameters.Add("@Accomodation", OleDbType.VarChar).Value = AccomodationTextBox.Text
                cmd.Parameters.Add("@ID", OleDbType.VarChar).Value = IDTextBox.Text
                Try
                    myConnection.Open()
                    cmd.ExecuteNonQuery()
                    myConnection.Close()
                    IDTextBox.Clear()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End Using
        End Using
    End Sub