【发布时间】:2015-02-04 18:05:30
【问题描述】:
我是一个新手程序员,我看过其他类似的帖子,他们说问题通常是由于使用了保留字,但我确信我没有使用任何。 sql 在访问中运行良好并创建记录(使用变量 v_sqlquery 中的 sql,但是在 VB 中出现以下错误“INSERT INTO 语句中的语法错误”。谁能帮助我,我花了几个小时试图解决这个问题?提前谢谢。
Dim v_SQLquery As String 'stores the sql query
Dim v_command As OleDbCommand 'passes command to db
Dim v_results As OleDbDataReader
Dim v_custID As Integer
Dim v_balance As String
Dim v_purchase As String
Dim v_date As Date
Dim v_time As Date
Dim v_transID As Integer
v_custID = txt_custID.Text
v_balance = txt_custbalance.Text
v_purchase = txt_puramount.Text
v_date = DateValue(Now) 'the current date
v_time = TimeValue(Now) 'the current time
If v_purchase = "" Then
MsgBox("Please enter the purchase amount.")
ElseIf v_purchase > v_balance Then
MsgBox("There are not enough funds in the account to complete this transaction.")
v_SQLquery = "INSERT INTO Transaction (Trans_type, Trans_amount, Trans_date, Trans_time, Cust_ID) VALUES ('P','" & v_purchase & "','" & v_date & "','" & v_time & "','" & v_custID & "');"
v_command = New OleDbCommand(v_SQLquery, v_connection)
v_results = v_command.ExecuteScalar
End If
v_connection.close()
【问题讨论】:
-
如果 v_purchase 或 v_custID 中有单引号 (') 会发生什么?什么输入触发了这个错误?
-
改为使用参数,看看错误是否消失。
-
您是否在调试模式下停止了代码,以便准确查看构建后的 v_SQLquery 的样子?你能发布它的样子吗?可能是查询没有正确构建。
-
啊! 停止连接你的SQL,并使用参数,它不仅为你避免了这个exact问题,而且它可以防止SQL注入和恶意用户。
标签: sql vb.net ms-access oledb insert-into