插入的错误语法

时间:2013-12-27 02:06:04

标签: sql .net vb.net oledb

至于我的问题,这是一个怪异的代码..

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()之前我在 -

上试了一下
For each word in txt
    MessageBox.Show(word)
Next

它完美地展示了它们中的每一个。我的理论错了吗?

1 个答案:

答案 0 :(得分: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 
相关问题