使用asp.net mv在ado.net中选择带有where子句的查询



这是我的选择查询,带有where子句。每次调用它时,数据表都是空的,列表计数为0。请更正我的选择询问或ado.net代码。我想从SQL服务器获得这些参数的数据,但我不知道我的SQL查询是错误的,或者ado.net代码是错误的

public List<AdsModel> GetAds(string _location, Int64 _maxprice, Int64 _minprice, int _maxarea, int _minarea)
{
connection();
List<AdsModel> AdsModelList = new List<AdsModel>();
SqlCommand cmd = new SqlCommand("select * from propertydata_tbl where Location like '%@location%' and Price >= @minprice and Price <= @maxprice and Area >= @minarea and Area <= @maxarea", con);
cmd.Parameters.AddWithValue("@location", _location);
cmd.Parameters.AddWithValue("@minprice", _minprice);
cmd.Parameters.AddWithValue("@maxprice", _maxprice);
cmd.Parameters.AddWithValue("@minarea", _minarea);
cmd.Parameters.AddWithValue("@maxarea", _maxarea);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
AdsModelList.Add(
new AdsModel
{
id = Convert.ToInt32(dr["Id"]),
price = Convert.ToInt64(dr["Price"]),
location = Convert.ToString(dr["Location"]),
area = Convert.ToInt32(dr["Area"]),
postdate = Convert.ToString(dr["Postdate"]),
titlelink = Convert.ToString(dr["Titleline"]),
adlink = Convert.ToString(dr["Adlink"])
}
);
}
}
return AdsModelList;
}
ADO不插入字符串,并将单引号内的所有内容视为发送到SQL的字符串文字。您应该从以下文字中提取@location
SqlCommand cmd = new SqlCommand("select * from propertydata_tbl where Location like '%' + @location + '%' and Price >= @minprice and Price <= @maxprice and Area >= @minarea and Area <= @maxarea", con);
// Here --------------------------------------------------------------------------------^-----------^

最新更新