oledb例外未得到处理

时间:2013-10-21 17:38:18

标签: vb.net ms-access

我得到的oledb异常未处理,我无法找到原因,任何人都可以为我注意

我BOLD和Italic错误代码

,例外显示如下查询表达式中的语法错误(缺少运算符)'产品名称=''主席'。

感谢

Imports System.Data.OleDb

Public Class Form1

    'declare the veriables

    Dim itemName As String
    Dim itemprice, average, rows, index, totalPrice
    Dim foundItem As Boolean
    Dim conectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Education\BIT\Assainment\L1S2\VB\Assesment 2\Stock.accdb"
    Dim reader As OleDbDataReader
    Dim olDataConnection As New OleDbConnection(conectionString)
    Dim olCommand As OleDbCommand


    Private Sub Form1_Load(ByVal sender As System.Object, e As EventArgs) Handles MyBase.Load
        'loding data to the combo box

        totalPrice = 0
        rows = 0

        'start the connection
        olDataConnection.Open()
        olCommand = New OleDbCommand("SELECT * FROM Stock", olDataConnection)
        reader = olCommand.ExecuteReader()
        While (reader.Read())
            comDataStock.Items.Add(DirectCast(reader("Product Name"), String) & " -Rs. " & DirectCast(reader("Price"), Integer) & " /= ")
            totalPrice = totalPrice + DirectCast(reader("Price"), Integer)
            rows = rows + 1
        End While

        Try
            average = totalPrice / rows
            lblAverResult.Text = "Rs. " & average.ToString() & " /="
        Catch ex As Exception
            MessageBox.Show("Somethingg Wrong ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        olDataConnection.Close()

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        olDataConnection.Open()
        olCommand = New OleDbCommand("DELETE FROM Stock WHERE Product Name=''" & comDataStock.SelectedItem.ToString().Split("-").GetValue(0).ToString() & "'", olDataConnection)
        reader = olCommand.ExecuteReader()
        olDataConnection.Close()
        comDataStock.Items.Clear()
        totalPrice = 0
        rows = 0

         olDataConnection.Open()
        olCommand = (New OleDbCommand("SELECT FROM Stock ", olDataConnection))
        reader = olCommand.ExecuteReader()

        While (reader.Read())
            comDataStock.Items.Add(DirectCast(reader("Price"), String) & " -Rs. " & DirectCast(reader("Price"), Integer) & " /= ")
            totalPrice = totalPrice + DirectCast(reader("Price"), Integer)
            rows = rows + 1

        End While

        Try
            average = totalPrice / rows
            lblAverResult.Text = " Rs. " & average.ToString() & " /="
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        olDataConnection.Close()

    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

如果您的名称包含空格的字段(例如Product Name),则必须将该名称括在方括号中。

olCommand = New OleDbCommand("DELETE FROM Stock WHERE [Product Name]='" & comDataStock.SelectedItem.ToString().Split("-").GetValue(0).ToString() & "'", olDataConnection)