无法弄清楚语法错误是什么

时间:2015-07-15 17:32:05

标签: vb.net ms-access

好的,所以我通过精选案例运行

在案例0中运行:

Sql = "INSERT INTO Devices (" &
           "[Asset Number], [Service Tag], Manufacturer, ModelName, Location, Room, Cart, [Purchase Date], Department, [Device Type]" &
           ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"

                'Query Parameteres.
                Using cmd As New OleDb.OleDbCommand(Sql, dbConn)
                    'Have to guess at column types/lengths here. Use actual types/lengths from your DB
                    cmd.Parameters.Add("@Asset Number", OleDbType.Integer).Value = AssetNum
                    cmd.Parameters.Add("@Service Tag", OleDbType.VarChar, 50).Value = ServTag
                    cmd.Parameters.Add("@Manufacturer", OleDbType.VarWChar, 255).Value = Manu
                    cmd.Parameters.Add("@Model", OleDbType.VarWChar, 50).Value = Model
                    cmd.Parameters.Add("@Location", OleDbType.VarWChar, 30).Value = Location
                    cmd.Parameters.Add("@Room", OleDbType.VarChar, 5).Value = Room
                    cmd.Parameters.Add("@Cart", OleDbType.VarChar, 2).Value = Cart
                    cmd.Parameters.Add("@Purchase Date", OleDbType.Date).Value = PurchDate
                    cmd.Parameters.Add("@Department", OleDbType.VarWChar, 50).Value = Department
                    cmd.Parameters.Add("@Device Type", OleDbType.VarChar, 255).Value = DeviceType
                    cmd.ExecuteNonQuery()
                End Using

在案例1中,它运行:

 Sql = "INSERT INTO MVNTA (" &
    "[Serial Number], [Ethernet Mac], [Wlan Mac], Imei, ESN, HEX, DEC, Phone, [NTA Tag], Manufacturer, Model)" &
    ")VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"

            'Query Parameteres.
            Using cmd As New OleDb.OleDbCommand(Sql, dbConn)
                'Have to guess at column types/lengths here. Use actual types/lengths from your DB

                cmd.Parameters.Add("@Serial Number", OleDbType.VarChar, 255).Value = ServTag
                cmd.Parameters.Add("@Ethernet Mac", OleDbType.VarChar, 255).Value = Ether
                cmd.Parameters.Add("@WLan Mac", OleDbType.VarChar, 255).Value = WLan
                cmd.Parameters.Add("@IMEI", OleDbType.VarChar, 255).Value = IMEI
                cmd.Parameters.Add("@ESN", OleDbType.VarChar, 255).Value = ESN
                cmd.Parameters.Add("@Hex", OleDbType.VarChar, 255).Value = HEX
                cmd.Parameters.Add("@DEC", OleDbType.VarChar, 255).Value = DEC
                cmd.Parameters.Add("@Phone", OleDbType.VarWChar, 255).Value = Phone
                cmd.Parameters.Add("@NTA Tag", OleDbType.Integer).Value = AssetNum
                cmd.Parameters.Add("@Manufacturer", OleDbType.VarWChar, 255).Value = Manu2
                cmd.Parameters.Add("@Model", OleDbType.VarWChar, 50).Value = Model2
                cmd.ExecuteNonQuery()
            End Using

案例1有语法错误,但是当我比较它们时,我找不到问题。 我比较了数据库中的长度,它们是正确的。虽然我不完全理解varchar vs varwchar,但我认为问题不在于此。案例0正在插入到Devices表中。

我还将语句直接引入数据库,修复了一些小问题,然后运行了。将它放回我的应用程序代码后,返回了语法错误。

1 个答案:

答案 0 :(得分:3)

OleDb下的

HEXDECproblem words。将这些名称括在方括号中。

并删除Mark spotted

的额外括号
Sql = "INSERT INTO MVNTA (" &
    "[Serial Number], [Ethernet Mac], [Wlan Mac], Imei, ESN, [HEX], [DEC], Phone, [NTA Tag], Manufacturer, Model)" &
    " VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"