0

I have this code

Dim q = New SqlCommand("SELECT * FROM [ddddd] WHERE ([Day] >= %XX ) AND ([Day] <= %YY )")
q.Parameters.Add(New SqlParameter("XX", rangeFrom.Value))
q.Parameters.Add(New SqlParameter("YY", rangeTo.Value))

q.Connection = dbc

Dim rd = q.ExecuteReader()
' ...

It throws a SqlException on the

Dim rd = q.ExecuteReader()

line with message

Incorrect syntax near 'XX'

Why?

1
  • what are the % signs for? Commented Mar 26, 2016 at 19:09

1 Answer 1

4

SQL Server parameters need to be prefixed with a @ (not a % as you've used it):

SELECT * 
FROM [ddddd] 
WHERE [Day] >= @XX AND [Day] <= @YY

and then also use the leading @ when declaring the parameters:

q.Parameters.Add(New SqlParameter("@XX", rangeFrom.Value))
q.Parameters.Add(New SqlParameter("@YY", rangeTo.Value))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.