如何使用参数化来写SQL语句?传入的参数根据条件有多次拼接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomerControl;
public partial class productMake_productMakeList : System.Web.UI.Page
{
const string vsKey = "searchCriteria"; //ViewState key
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string s = "where isChecked=0";
searchOrders(s);
}
}
protected void Search_Click(object sender, EventArgs e)
{
AspNetPager1.CurrentPageIndex = 1;
string s = " where ";
string ischecked = dro_isPay.SelectedValue;
if (ischecked == "false")
{
s += "isChecked=0";
}
else
{
s += "isChecked=1";
}
string type = dro_type.SelectedValue;
switch (type)
{
case "1a1":
s += " and whatWork='"+ type +"'";
break;
case "1a2":
s += " and whatWork='" + type + "'";
break;
case "1a3":
s += " and whatWork='" + type + "'";
break;
default:
break;
}
string productname = tex_productName.Text.Trim();
if (productname != "")
{
s += " and productName like '%" + productname + "%'";
}
string workername = tex_name.Text.Trim();
if (workername != "")
{
s += " and workerName like '%" + workername + "%'"; // sWhere " where isChecked=0 and whatWork=1a1 and workerName like '%陈%'" string
}
ViewState[vsKey] = s;
searchOrders(s);
}
void searchOrders(string sWhere)
{
SqlDataSource1.SelectCommand = "select GGPRODUCT.ID,workID,productName,productPCS,price,total,reduce,productDate,GGPRODUCT.remark,checkedDate,workerName from GGPRODUCT left join worker on worker.ID=GGPRODUCT.workID " + sWhere + " order by GGPRODUCT.ID desc";
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
AspNetPager1.RecordCount = dv.Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
Repeater1.DataSource = pds;
Repeater1.DataBind();
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
searchOrders((string)ViewState[vsKey]);
}
}相关推荐
页面传入多个条件——sql语句的拼接
页面传进来传个条件判断的时候,要用到sql语句的拼接才能解决sql的问题,这是一个小例子。
动态拼接sql语句工具类,拼接where后面语句
动态拼接sql语句工具类,拼接where后面语句 配合原生jdbc仿动态sql注入 if (ObjectUtil.isNotEmpty(maxLat)&&ObjectUtil.isNotEmpty(minLat)){ sqlParamList.add(new SqlParam("lat",minLat, SqlOpEnum.GE)); sqlParamList.add(new SqlParam("lat",maxLat, SqlOpEnum.LTE)); } Pair<String, Object[]> stringPair = SqlBuilderUtil.buildCondition(sqlParamList); String sql = String.format(ConstantSql.TEC_TJ_SQL, stringPair.getKey()); log.info("**sql语句:{}",sql);
MyBatis 执行动态 SQL语句详解
大家对mybatis执行任意sql语句都了解,那么MyBatis执行动态SQL语句呢?下面脚本之家小编给大家解答下mybatis执行动态sql语句的方法,非常不错,感兴趣的朋友参考下吧
浅析SQL Server参数化查询
说来惭愧,工作差不多4年了,直到前些日子被DBA找上门让我优化一个CPU占用很高的复杂SQL语句时,我才突然意识到了参数化查询的重要性。 相信有很多开发者和我一样对于参数化查询认识比较模糊,没有引起足够的重视 错误认识1.不需要防止sql注入的地方无需参数化 参数化查询是为了防止SQL注入用的,其它还有什么用途不知道、也不关心,原则上是能不用参数不用参数,为啥?多麻烦,我只是做公司内部系统不用担心SQL注入风险,使用参数化查询不是给自己找麻烦,简简单单拼SQL,万事OK 错误认识2.参数化查询时是否指定参数类型、参数长度没什么区别 以前也一直都觉的加与不加参数长度
防SQL注入 生成参数化的通用分页查询语句
前些时间看了玉开兄的“如此高效通用的分页存储过程是带有sql注入漏洞的”这篇文章,才突然想起某个项目也是使用了累似的通用分页存储过程。


