【问题标题】:error syntax in insert into插入中的错误语法
【发布时间】:2013-12-27 02:06:03
【问题描述】:

至于我的问题,这是该死的代码..

Dim txt() As String
Dim _updateFlag As Boolean

Public Sub save()
    Dim query = String.Empty
    If Not _updateFlag Then
        query = "INSERT INTO tblGPSRoutes(Time,Latitude,Longitude,Elevation,Accuracy,Bearing,Speed)"
        query &= " VALUES ('" & txt(0) & "','" & txt(1) & "','" & txt(2) & "','" & txt(3) & "','" & txt(4) & "','" & txt(5) & "','" & txt(6) & "')"
        databaseFunctions.ExecuteQuery(query)
        MessageBox.Show("Data Saved Successfully.")
    Else
        query = String.Empty
        query = "UPDATE tblGPSRoutes SET Time = '" & txt(0) & "', Latitude = '" & txt(1) & "', Longitude = '" & txt(2) & "', Elevation = '" & txt(3) & "', Accuracy = '" & txt(4) & "', Bearing = '" & txt(5) & "', Speed = '" & txt(6) & "' WHERE ID = " & Integer.Parse(textbox1.Text)
        databaseFunctions.ExecuteQuery(query)
        MessageBox.Show("Data Updated Successfully.")
    End If
End Sub

它以我的其他形式工作。我只是在这里使用它,将值从 .Text 更改为数组中的值。
我从Try Catch“插入到语句中的语法错误”中得到了错误

我错过了什么吗?
顺便说一句,我使用 OleDB
更新:这些是 RichTextBox.Split(","c)
的值 04T16:18:42Z,14.524379,121.000830,60.700001,25.000000,350.299988,11.750000
在做save()之前,我在a上试过-

For each word in txt
    MessageBox.Show(word)
Next

它完美地展示了他们每个人的分离。我的理论错了吗?

【问题讨论】:

  • 由于您没有使用参数化查询,因此 [txt(n)] 值之一可能包含单引号。
  • 由于我们看不到数组中的内容,其中之一可能是空白、null、无、包含保留字、非法字符或不良业力
  • 更新了它。请检查
  • 调试!在已完成的查询字符串上设置断点,将其复制/粘贴到 SQL 管理工作室或您使用的任何工具中,并检查数据库正在谈论的语法错误...
  • 停止在 SQL 中使用文字并使用正确的参数!是的,是不是又快又容易,却会给你带来各种问题。

标签: sql .net vb.net oledb


【解决方案1】:

我知道参数化查询需要更多时间来设置,但它们最终会为您节省时间。这是一个如何使用它们的示例。

  Private Sub Insert()
    Dim connectionString = ""   ''your connection string
    Dim commandText = "INSERT INTO tblGPSRoutes([Time],Latitude,Longitude,Elevation,Accuracy,Bearing,Speed) VALUES (?,?,?,?,?,?,?)"

    Using cnn As New OleDb.OleDbConnection(connectionString)
      Using cmd As New OleDb.OleDbCommand(commandText, cnn)
        With cmd.Parameters
          '' since you are using OleDb the parameter names don't matter. 
          '' Just make sure they are entered in the same order as your sql   
          .AddWithValue("@Time", txt(0))                        
          .AddWithValue("@Latitude", txt(1))
          .AddWithValue("@Longitude", txt(2))
          .AddWithValue("@Elevation", txt(3))
          .AddWithValue("@Accuracy", txt(4))
          .AddWithValue("@Bearing", txt(5))
          .AddWithValue("@Speed", txt(6))
        End With

        Try
          cnn.Open()
          cmd.ExecuteNonQuery()
        Catch ex As Exception
          '' handle the exception
        End Try
      End Using
    End Using
  End Sub 

【讨论】:

  • 这里的罪魁祸首是Time。它是 MS Access 的保留字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多