首页 > 解决方案 > 从字符串“插入到reciptdetails(recip”到类型“integer”的转换无效)

问题描述

从数据库中获取recipt id

        SQL = "select max(reciptid) as MAXID from recipts where 1"
        Dim CMD2 As New OleDb.OleDbCommand
        CMD2.Connection = MyConnection
        CMD2.Transaction = MyTransaction
        CMD2.CommandText = SQL
        Dim ReciptID As Long = CMD2.ExecuteScalar()
        CMD2.Dispose()

插入收据的详细信息

         Dim I As Integer
        For I = 0 To DataGridView2.Rows.Count - 1
            'get the values first


            Dim Barcode As String = DataGridView2.Rows(I).Cells(0).Value
            Dim ItemBuyPrice As Decimal = DataGridView2.Rows(I).Cells(2).Value
            Dim ItemSellPrice As Decimal = DataGridView2.Rows(I).Cells(3).Value
            Dim ItemCount As Integer = DataGridView2.Rows(I).Cells(4).Value

            Dim CMD3 As New OleDb.OleDbCommand
            SQL = "insert into reciptdetails (reciptid, barcode, itemcount, itembuyprice, itemsellprice) values (:0      ,:1       ,:2        ,:3         ,:4        )"
            CMD3.Connection = MyConnection
            CMD3.Transaction = MyTransaction
            CMD3.CommandType = SQL
            CMD3.Parameters.AddWithValue(":0", ReciptID)
            CMD3.Parameters.AddWithValue(":1", Barcode)
            CMD3.Parameters.AddWithValue(":2", ItemCount)
            CMD3.Parameters.AddWithValue(":3", ItemBuyPrice)
            CMD3.Parameters.AddWithValue(":4", ItemSellPrice)

            CMD3.ExecuteNonQuery()
            CMD3.Dispose()

当我在运行 SQL 命令时运行此查询时,我收到此错误 在此处输入图像描述

查询有什么问题吗?

标签: vb.netms-access

解决方案


这是你的错误:

CMD3.CommandType = SQL

OleDbCommand.CommandType需要一个CommandType值,而不是一个字符串。

你可能打算写:

CMD3.CommandText = SQL

我强烈建议打开Option Strict,它允许您在编译时而不是在运行时捕获此类错误。


推荐阅读