select 使用 asp.net mv 在 ado.net 中使用 where 子句查询
select query with where clause in ado.net using asp.net mv
this is my select query with where clause every time I call it the datatable is empty and list count is 0 please correct my select query or ado.net code. I want to get data from the SQL server from these parameters but I don't know my SQL query is wrong or the ado.net code is wrong
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 --------------------------------------------------------------------------------^-----------^
this is my select query with where clause every time I call it the datatable is empty and list count is 0 please correct my select query or ado.net code. I want to get data from the SQL server from these parameters but I don't know my SQL query is wrong or the ado.net code is wrong
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 --------------------------------------------------------------------------------^-----------^