我正在尝试将带有时间和分钟的日期插入到我的SQL Server中,该SQL Server具有date类型的列。
但我得到以下内容:
There was an error parsing the query. [ Token line number = 1,Token line offset = 65,Token in error = 10 ]
这是我的代码:
Dim dateComplete As Date = CDate(Me.TB_Date.Text & " " & TXT_timeInHour.Text & ":" & TXT_timeInMin.Text)
MsgBox(dateComplete)
Dim MyConnexion As SqlCeConnection = New SqlCeConnection("Data Source=C:\Users\W8\Documents\aa.sdf;")
Dim Requete As String = "Insert into toto(titre, chien, dateIn) values (99," & swimTotal & ", " _
& dateComplete & ")"
Dim Commande As New SqlCeCommand(Requete, MyConnexion)
MyConnexion.Open()
Commande.ExecuteNonQuery()
我尝试使用format(dateComplete,“yyyy-DD-MM”),但它没有给我相应的SQL Server Management日期。
谢谢。
答案 0 :(得分:3)
这是使用参数化查询的众多原因之一
Dim Requete As String = "Insert into toto(titre, chien, dateIn) " & _
"values (99, @swim, @dtcom)"
Using MyConnexion = New SqlCeConnection("....")
Using Commande = New SqlCeCommand(Requete, MyConnexion)
Comande.Parameters.AddWithValue("@swim", swimTotal)
Comande.Parameters.AddWithValue("@dtcom", dateComplete)
MyConnexion.Open()
Commande.ExecuteNonQuery()
End Using
End Using
通过这种方式,在数据库表中插入日期的作业由数据库引擎执行,该数据库引擎更好地了解如何处理日期,数字和字符串。不要忘记,使用参数化查询,避免任何可能从恶意用户创建Sql Injection攻击。