0

I am new to web services and I have written some simple queries. They work just fine, but this one doesn't return json

SqlDataReader dr;
SqlCommand cmd = new SqlCommand("SELECT * FROM doctors WHERE address LIKE %@add%", conn);
cmd.Parameters.AddWithValue("@add", address);

dr = cmd.ExecuteReader();

If I type it like

"select * from doctors WHERE address = @add

it works fine

What seems to be the problem?

7
  • 9
    like '%' + @add + '%' Commented May 8, 2017 at 13:25
  • Wrap %@add% inside single quotes Commented May 8, 2017 at 13:26
  • 1
    @ganeshran That won't work since @add is a parameter. The OP needs to do what Alex has. Commented May 8, 2017 at 13:27
  • You have to add the wildcards like Alex said or add them to address before creating the parameter. Commented May 8, 2017 at 13:29
  • what is address? Is it a string or an object? Commented May 8, 2017 at 13:30

2 Answers 2

1

Add the % to the value, not the parameter name

var address = "%1 main street%";

SqlDataReader dr;
SqlCommand cmd = new SqlCommand("SELECT * FROM doctors WHERE address LIKE @add", conn);
cmd.Parameters.AddWithValue("@add", address);
dr = cmd.ExecuteReader();
Sign up to request clarification or add additional context in comments.

Comments

0

try this

SqlDataReader dr;
SqlCommand cmd = new SqlCommand("SELECT * FROM doctors WHERE address LIKE '%@add%'", conn);
cmd.Parameters.AddWithValue("@add", address);
dr = cmd.ExecuteReader();

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.