SQL 查询在 SQL Server 上不起作用



我是Web服务的新手,我写了一些简单的查询。他们工作正常,但是这不会返回JSON

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

如果我像

键入它
"select * from doctors WHERE address = @add

它的工作正常

似乎是什么问题?

将%添加到值,而不是参数名称

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

尝试此

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

相关内容

  • 没有找到相关文章

最新更新