这是我最后的工作代码。它从一串字符串中保存数据。使用,
作为分隔符,我将它们添加到数组中。即使它具有空值,也不会在数据库中保存任何内容。
Dim query As String = "INSERT INTO tblGPSRoutes" & _
"(UTCTime,Status,Latitude,NSIndicator,Longitude,EWIndicator," & _
"SpeedOverGround,CourseOverGround,UTCDate,MagneticVariation,EWIndicatorB,Mode,Cheksum)" & _
" VALUES " & _
"(@utctime,@status," & _
"@lat,@nsindicator," & _
"@long,@ewindicator," & _
"@sog,@cog,@utcdate,@magnet," & _
"@ewindicatorb,@mode,@checksum)"
Using cmd As New OleDbCommand(query, con) 'myconnection - is your connection object'
With cmd.Parameters
.AddWithValue("@utctime", txt(1))
.AddWithValue("@status", txt(2))
.AddWithValue("@lat", txt(3))
.AddWithValue("@nsindicator", txt(4))
.AddWithValue("@long", txt(5))
.AddWithValue("@ewindicator", txt(6))
.AddWithValue("@sog", txt(7))
.AddWithValue("@cog", txt(8))
.AddWithValue("@utcdate", txt(9))
.AddWithValue("@magnet", txt(10))
.AddWithValue("@ewindicatorb", txt(11))
.AddWithValue("@mode", txt(12))
.AddWithValue("@checksum", txt(13))
End With
cmd.ExecuteNonQuery()
这是我编辑的代码,因为我需要解析latlon
数据..这就是标题出现的地方..你能看出什么是错的吗?
Dim la As Double = Double.Parse(txt(3).Substring(0, 2)) + Double.Parse(txt(3).Substring(2)) / 60.0
Dim lo As Double = Double.Parse(txt(5).Substring(0, 3)) + Double.Parse(txt(5).Substring(3)) / 60.0
Dim query As String = "INSERT INTO tblGPSRoutes" & _
"(UTCTime,Status,Latitude,NSIndicator,Longitude,EWIndicator," & _
"SpeedOverGround,CourseOverGround,UTCDate)" & _
" VALUES " & _
"(@utctime,@status," & _
"@lat,@nsindicator," & _
"@long,@ewindicator," & _
"@sog,@cog,@utcdate)"
Using cmd As New OleDbCommand(query, con) 'myconnection - is your connection object'
With cmd.Parameters
.AddWithValue("@utctime", txt(1))
.AddWithValue("@status", txt(2))
If txt(3) IsNot Nothing Then
.AddWithValue("@lat", la)
Else
.AddWithValue("@lat", "No Data") 'is this possible?'
End If
.AddWithValue("@nsindicator", txt(4))
If txt(5) IsNot Nothing Then
.AddWithValue("@long", lo)
Else
.AddWithValue("@long", "No Data")
End If
.AddWithValue("@ewindicator", txt(6))
.AddWithValue("@sog", txt(7))
.AddWithValue("@cog", txt(8))
.AddWithValue("@utcdate", txt(9))
End With
cmd.ExecuteNonQuery() 'error here..
End Using
我在想,也许是因为我删除要插入预先制作的访问数据库的数据量会引发它?正如您所看到的,我切断了 code2 中的字段数量,因为发送给我的字符串出现了一些错误,并且它们并不那么重要,因此我删除了它们所在的代码插入在数据库中,尽管它们的字段仍在那里。这是我的第一个假设(但我认为不是真的)
无论如何,我昨天没有测试过,所以我想到了。我的GPS模块几乎没有信号,所以我无法彻底测试,(字符串来自它,所以如果它不起作用,我也是)