SQL 空格的处理
关键的两句:
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);
作者:爱ai优酷
出处:http://www.cnblogs.com/aiyouku/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。