使用Visual Basic 2010更新MS Access数据库时出错

时间:2015-11-01 23:08:05

标签: vb.net ms-access

此代码无效。弹出错误消息说:

  

查询exprssion SubName = Gussing Game中的语法错误(缺少运算符)。

这是我的代码请帮忙。我需要此代码来更新Microsoft Access数据库。

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Try
        Dim Str As String
        Con.Open()

        Str = "update vb set AssignDate="
        Str += """" & txtADate.Text & """"
        Str += "where SubName = "
        Str += txtAName.Text.Trim()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Con.Close()
        Con.Open()

        Str = "update vb set DueDate="
        Str += """" & txtDDate.Text & """"
        Str += "where SubName = "
        Str += txtAName.Text.Trim()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Con.Close()
        Con.Open()

        Str = "update vb set Weight="
        Str += """" & txtWeight.Text & """"
        Str += "where SubName = "
        Str += txtAName.Text.Trim()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Con.Close()
        Con.Open()

        Str = "update vb set Reference="
        Str += """" & txtReference.Text & """"
        Str += "where SubName = "
        Str += txtAName.Text.Trim()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Con.Close()
        Con.Open()

        Str = "update vb set Comment="
        Str += """" & txtComment.Text & """"
        Str += " where SubName ="
        Str += txtAName.Text.Trim()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Con.Close()
        Con.Open()

        Str = "update vb set Statues="
        Str += """" & txtStatues.Text & """"
        Str += " where SubName ="
        Str += txtAName.Text.Trim()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Con.Close()

        Dst.Clear()
        Dad = New OleDbDataAdapter("SELECT * FROM vb ORDER BY SubName", Con)
        Dad.Fill(Dst, "assignment")
        MessageBox.Show("Record Updated!", "Caution", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Catch ex As Exception
        MsgBox(ex.Message & "," & ex.Source)
    End Try
    Con.Close()
End Sub

1 个答案:

答案 0 :(得分:1)

您没有引用比较SubName的字符串标量:

    Str = "update vb set AssignDate="
    Str += """" & txtADate.Text & """"
    Str += "where SubName = "
    Str += txtAName.Text.Trim()  ' should be """"&txtAName.Text.Trim()&""""

我会跳过一个极其糟糕的查询方式。