当我尝试执行此SQL查询时:
savInto.CommandText = "update onCommands set warnDate =#" & movDate.Value.ToString("MM/dd/yyyy")
& "#,updateDate =#" & Date.Now.ToShortDateString
& "#,transportCompany ='" & Trim(Company.Text)
& "' where ID =" & moveID
我收到此错误:
Incorrect syntax near '#'
答案 0 :(得分:4)
主要问题是您尝试将参数作为SQL本身的一部分提供。虽然是这样做的方式(使用撇号而不是#
),但这通常是一个坏主意:
相反,您应该使用参数化SQL并指定参数的值。类似的东西:
savInto.CommandText = "update onCommands set warnDate = @warnDate" &
", updateDate = @updateDate, transportCompany = @transportCompany" &
" where ID=@moveID"
savInto.Parameters.Add("@warnDate", SqlDbType.DateTime).Value = movDate
savInto.Parameters.Add("@updateDate", SqlDbType.DateTime).Value = Date.Now
savInto.Parameters.Add("@transportCompany", SqlDbType.NVarChar).Value = Trim(Company.Text)
savInto.Parameters.Add("@moveID", SqlDbType.NVarChar).Value = moveID
答案 1 :(得分:1)
你为什么使用#character?您使用的是什么数据库(sql server,oracle,mysql ...)
试试这个:
savInto.CommandText = "update onCommands set warnDate ='" & movDate.Value.ToString("MM/dd/yyyy") & "',updateDate ='" & Date.Now.ToShortDateString & "',transportCompany ='" & Trim(Company.Text) & "' where ID =" & moveID