简体   繁体   中英

SQL query doesn't work on SQL Server

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?

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();

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM