关键的两句:
if (cityTypes == "city") { sqlw = sqlw + " AND ltrim(rtrim(cropsType.city))='" + swheres.Trim() + "' "; } else { sqlw = sqlw + " AND SUBSTRING(ltrim(rtrim(cropsType.town)),1,2)='" + swheres.Trim() + "' "; }
ltrim(rtrim())是SQL语句中去除文本前后的空格的函数,ltrim()去除文本前的空格,rtrim()去除文本后的空格,
object.Trim()是C#中的去除文本前后的空格的函数
string swheres = context.Request["swhere"]; string sqlw = null; string specieTypes = context.Request["stype"]; string cityTypes = context.Request["cType"]; sqlw = " SELECT * FROM cropsType INNER JOIN cropMenu ON cropMenu.LallId = cropsType.LallId "; if ((!String.IsNullOrEmpty(swheres)) && (swheres!="福建"))//strWhere.Trim()!="" { if (cityTypes == "city") { sqlw = sqlw + " AND ltrim(rtrim(cropsType.city))='" + swheres.Trim() + "' "; } else { sqlw = sqlw + " AND SUBSTRING(ltrim(rtrim(cropsType.town)),1,2)='" + swheres.Trim() + "' "; } //if (cityTypes == "city") { sqlw = sqlw + " AND cropsType.city like '%" + swheres.Substring(0, 2).Trim() + "%'";} //else { sqlw = sqlw + " AND cropsType.town like '%" + swheres.Substring(0, 2).Trim() + "%'"; } } if (!String.IsNullOrEmpty(specieTypes))//strWhere.Trim()!="" { sqlw = sqlw + " AND cropMenu.Lcrop='" + specieTypes.Trim() + "' "; } DataTable dtType = SqlAdapter(sqlw); string jsonType = DataTableJson(dtType);//转换成json数据格式 context.Response.Write(jsonType);