我没有错误,但是:“ OleDbException是查询表达式中未处理的语法错误(缺少运算符)”如何解决此问题?

时间:2018-08-20 09:05:11

标签: visual-studio-2010 ms-access ms-access-2003

我是使用Visual Stuido 2010 C#和Microsft Access 2007创建应用程序的新手。我打算创建一个应用程序,用户可以在其中向数据库添加数据(MS-Access)。但是我收到一个错误,指出“查询表达式中的语法错误(缺少运算符)”。我真的找不到我的代码有什么问题。我正在使用面板来访问表单的第2页。

我从Sub Button1“ rd = cmd.ExecuteReader”得到了问题 这是我向数据库添加数据的代码:

Imports System.Data.OleDb

Public Class Form1
    Dim conn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim str As String
    Dim cmd As OleDbCommand
    Dim rd As OleDbDataReader
    Dim tanya As String

    Sub koneksi()
        str = "provider=microsoft.jet.oledb.4.0;data source=Database1.mdb"
        conn = New OleDbConnection(str)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
    End Sub


    Sub tampilkan()
        da = New OleDbDataAdapter("select * from tb_bpl", conn)
        ds = New DataSet
        da.Fill(ds, "tb_bpl")
        DataGridView1.DataSource = ds.Tables("tb_bpl")
        DataGridView1.Columns(1).Width = 250
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call koneksi()
        Call tampilkan()
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub
    Private Sub bersih()
        TextBox1.Clear()
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
        TextBox10.Text = ""
        TextBox11.Text = ""
        TextBox12.Text = ""
        TextBox13.Text = ""
        TextBox14.Text = ""
        TextBox15.Text = ""
        TextBox16.Text = ""
        TextBox17.Text = ""
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cmd = New OleDbCommand("select * tb_bpl where periode='" + TextBox1.Text + "'", conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then 'jika data/baris tidak ditemukan
            Dim simpan As String = "insert into tb_bpl(periode, reviewer, pemeriksa, paraf1, paraf2, y1, kkp1, ket1, y2, kkp2, ket2, y3, kkp3, ket3, y4, kkp4, ket4) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & TextBox13.Text & "','" & TextBox14.Text & "','" & TextBox15.Text & "','" & TextBox16.Text & "','" & TextBox17.Text & "')"
            cmd = New OleDbCommand(simpan, conn)
            cmd.ExecuteNonQuery()
            MsgBox("Data Baru Tersimpan", vbInformation, "Pemberitahuan!!!")
            Call bersih()
        Else
            MsgBox("Data Sudah Ada", vbInformation, "Maaf!!!")
        End If
        tampilkan()

    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        cmd = New OleDbCommand("select * from tb_bpl where periode='" & TextBox1.Text & "'", conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            Dim sqledit As String = "update tb_bpl set reviewer='" & TextBox2.Text & "',pemeriksa='" & TextBox3.Text & "',paraf1='" & TextBox4.Text & "',paraf2='" & TextBox5.Text & "',y1='" & TextBox6.Text & "' ,kkp1='" & TextBox7.Text & "' ,ket1='" & TextBox8.Text & "',y2='" & TextBox9.Text & "',kkp2='" & TextBox10.Text & "',ket2='" & TextBox11.Text & "',y3='" & TextBox12.Text & "',kkp3='" & TextBox13.Text & "',ket3='" & TextBox14.Text & "',y4='" & TextBox15.Text & "',kkp4='" & TextBox16.Text & "',ket4='" & TextBox17.Text & "'  where periode='" & TextBox1.Text & "'"
            cmd = New OleDbCommand(sqledit, conn)
            cmd.ExecuteNonQuery()
        End If
        MsgBox("Data Terubah", vbOKOnly, "Sukses")
        Call bersih()
        Call tampilkan()

    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        cmd = New OleDbCommand("select * from tb_bpl where periode='" & TextBox1.Text & "'", conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            tanya = MsgBox("Anda Yakin Akan Menghapus Data?", vbYesNo, "Perhatian")
            If tanya = vbYes Then
                Dim sqlhapus As String = "delete * from tb_bpl where periode='" & TextBox1.Text & "'"
                cmd = New OleDbCommand(sqlhapus, conn)
                cmd.ExecuteNonQuery()
                MsgBox("Data Terhapus", vbOKOnly, "Sukses")
                Call bersih()
            End If
        End If
        Call tampilkan()

    End Sub

    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        cmd = New OleDbCommand("select * from tb_bpl where periode='" & TextBox1.Text & "'", conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            TextBox2.Text = rd.Item("reviewer")
            TextBox3.Text = rd.Item("pemeriksa")
            TextBox4.Text = rd.Item("paraf1")
            TextBox5.Text = rd.Item("paraf2")
            TextBox6.Text = rd.Item("y1")
            TextBox7.Text = rd.Item("kkp1")
            TextBox8.Text = rd.Item("ket1")
            TextBox9.Text = rd.Item("y2")
            TextBox10.Text = rd.Item("kkp2")
            TextBox11.Text = rd.Item("ket2")
            TextBox12.Text = rd.Item("y3")
            TextBox13.Text = rd.Item("kkp3")
            TextBox14.Text = rd.Item("ket3")
            TextBox15.Text = rd.Item("y4")
            TextBox16.Text = rd.Item("kkp4")
            TextBox17.Text = rd.Item("ket4")
        End If

    End Sub

End Class

0 个答案:

没有答案