vb.net中UPDATE语句访问数据库中的语法错误

时间:2013-09-03 11:43:52

标签: vb.net ms-access

我刚刚开始学习VB.net几周。我想创建一个表单并将数据从文本框发送到ms访问数据库(* .accdb)文件中的特定单元格。但我写的代码给出了以下错误:

  

UPDATE语句中的语法错误。

我已经检查过几本书并花了几个小时上网,但没有回答!

    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim cnn1 As New OleDb.OleDbConnection
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=  
           E:\Ebook\hararat\GUI\Heat Exchanger Designer\heat.accdb"
    con.Open()
    sql = "SELECT * FROM flow1"
    da = New OleDbDataAdapter(sql, con)
    da.Fill(ds, "flow1")
    Dim cb As New OleDb.OleDbCommandBuilder(da)
    ds.Tables("flow1").Rows(1).Item(1) = "name"
    da.Update(ds, "flow1")
    con.Close()

4 个答案:

答案 0 :(得分:3)

您需要使用OleDbCommandBuilder的.QuotePrefix.QuoteSuffix属性将表和字段名称包装在方括号中。也就是说,而不仅仅是

Dim cb As New OleDb.OleDbCommandBuilder(da)

你需要做

Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"

这将生成表单

的UPDATE语句
UPDATE [TableName] SET [ColumnName]= ...
如果表名或任何字段名恰好是Access SQL中的reserved words,则必须使用

答案 1 :(得分:0)

试试这个

dim sqlupdate as string = "UPDATE tablename SET column_name = '" & textname.text & "' WHERE column_name = '" & textname.text & "'"

使用以下列名时有时会发生错误:用户名,密码,日期,时间以及此类型的更多内容,请尝试避免使用这些列名,因为这可能会导致有关更新表的问题。允许您更新这种用[和]括起来的列名称,所以它是这样的:[用户名],[日期]等所以语法可能是这样的:

UPDATE tablename SET [Username] = '" & textname.text & "' WHERE column_name = '" & textname.text & "'"

我的代码是这样的:

     Open_Con()
            Dim sqlUpdate As String
            Dim sqlUpdatePass As DialogResult
            sqlUpdate = "UPDATE tblAccounts SET [Password] = '" & txtRPassword.Text & "' WHERE [Username] = '" & txtUsername.Text & "'"
            sqlCmd = New OleDbCommand(sqlUpdate, sqlCon)
            Try
                sqlUpdatePass = MessageBox.Show("Are you sure to save this changes?", "Save changes?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If sqlUpdatePass = vbYes Then
                    sqlCmd.ExecuteNonQuery()
                    MsgBox("Changes are now saved", MsgBoxStyle.Information, "New password has been set.")
                    Call ClearAll()
                    Me.Hide()
                Else
                    Exit Sub
                End If
            Catch ex As Exception
                MsgBox("Could not perform this task because " & ex.Message, MsgBoxStyle.Exclamation, "Error")
            End Try
            sqlCmd = Nothing
            sqlCon.Close()

希望这些事情提到上面的代码可以帮助你解决问题。祝你有个美好的一天,快乐的编码:)

答案 2 :(得分:0)

dim sqlupdate as string="UPDATE [tablename] SET [column_name] = '"& textname.text &"' WHERE [column_name] = '"& textname.text &"';"

通过用方括号括起属性,它似乎工作我已经尝试过,它可以工作

答案 3 :(得分:0)

Imports System.Data.OleDb
Imports System.Data
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database2DataSet.identitas' table. You can move, or remove it, as needed.
        Me.IdentitasTableAdapter.Fill(Me.Database2DataSet.identitas)
    End Sub

    Public Sub clean()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
    End Sub

    Public Sub read()
        Call openconn()
        str = "select * from identitas"
        dtadapter = New OleDbDataAdapter(str, con)
        Dim dg As New DataTable
        dg.Clear()
        dtadapter.Fill(dg)
        dgv.DataSource = dg
    End Sub

    Public Sub create()
        Call openconn()
        str = "insert into identitas values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "') "
        cmd = New OleDbCommand(str, con)
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        MsgBox("data lebet")
        read()
        clean()
    End Sub

    Public Sub update()
        Call openconn()
        str = "UPDATE identitas SET [Nama] = '" & TextBox2.Text & "',[Alamat] = '" & TextBox3.Text & "', [No] = '" & TextBox4.Text & "' where [NIK] = '" & TextBox1.Text & "'"
        cmd = New OleDbCommand(str, con)
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        MsgBox("data ter ubah")
        clean()
        read()
    End Sub

    Public Sub delete()
        Call openconn()
        str = "delete from identitas where NIK = '" & TextBox1.Text & "'"
        cmd = New OleDbCommand(str, con)
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        clean()
    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Me.Close()
    End Sub

    Private Sub btnc_Click(ByVal sender As System.Object, ByVal e As     System.EventArgs) Handles btnc.Click
        create()
    End Sub

    Private Sub btnr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnr.Click
        read()
    End Sub

    Private Sub btnclean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclean.Click
        clean()
    End Sub

    Private Sub btnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnd.Click
        Dim pesan As String = MsgBox("yakin mau hapus = " & TextBox1.Text & "?", MsgBoxStyle.YesNo)
        If pesan = vbYes Then
            delete()
        End If
        read()
    End Sub

    Private Sub btnu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnu.Click
        update()
    End Sub

End Class