System.Data.OleDb.OleDbException

时间:2015-01-09 14:08:02

标签: vb.net visual-studio-2010

我尝试连接到.accdb access 2010数据库。

我得到了这个: 类型' System.Data.OleDb.OleDbException'的未处理异常发生在System.Data.dll中 附加信息:标准表达式中的数据类型不匹配。

但我不知道问题出在哪里和什么地方。我该怎么办? 这是我的代码:

Private Sub frm_ViewsOrder_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    cmb_order.DataSource = get_data("SELECT DISTINCT fld_order_no FROM tbl_order")
    cmb_order.DisplayMember = "fld_order_no"    
End Sub 

Private Sub refresh_grid(orderid As String)   
    grd_view.DataSource = get_data("SELECT fld_product_name, fld_bil, fld_price, fld_bil * fld_price as Total FROM tbl_products, tbl_order WHERE tbl_order.fld_product_id = tbl_products.fld_product_id AND tbl_order.fld_order_no = """ & orderid & """ ")

    grd_view.Columns(0).HeaderText = "Product Name"
    grd_view.Columns(1).HeaderText = "Quantity"
    grd_view.Columns(2).HeaderText = "Unit Price (RM)"
    grd_view.Columns(3).HeaderText = "Amount(RM)"

    grd_view.Columns(2).DefaultCellStyle.Format = "N2"
    grd_view.Columns(3).DefaultCellStyle.Format = "N2"

    Dim totalValue As Decimal

    For Each dgvRow As DataGridViewRow In grd_view.Rows
        If Not dgvRow.IsNewRow Then
            totalValue += CDec(dgvRow.Cells(3).Value)
        End If
    Next

    txt_total.Text = String.Format("{0:n2}", totalValue)

End Sub

1 个答案:

答案 0 :(得分:0)

在你的SQL中,我会检查“fld_bil”和“fld_price”都是数字的(就像你没有将两个字符串相乘,它们一起有数字)。我还要确保“fld_order_no”和你的局部变量“ordered”是数字的。 OLEDB的类型异常使我认为它是SQL中的转换错误。

此外,您可能希望参数化查询以防止SQL注入攻击。这很容易做到。在您的命令对象(可能是在您的get_data方法中)中,您可能希望执行以下操作:

cmd.CommandText = "SELECT fld_product_name, fld_bil, fld_price, fld_bil * fld_price as Total FROM tbl_products, tbl_order WHERE tbl_order.fld_product_id = tbl_products.fld_product_id AND tbl_order.fld_order_no = @orderid"
' ordered being your local variable
cmd.Parameters.AddWithValue("@orderid", orderid)

这有助于防止任何人在查询结束时对SQL进行操作。这是一个链接,在VB.Net中有一个更深入的参数化查询示例:

http://www.blakepell.com/2012-02-28-vbnet-parameterized-query-example-and-why-you-should-care