如何使用参数化来写SQL语句?传入的参数根据条件有多次拼接

dydxf 2010-06-02 11:52:31
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]);
}


}
...全文
310 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuxh81 2010-06-02
  • 打赏
  • 举报
回复
我自认为我正确使用了参数化查询,且多条件查询做得很方便

有兴趣的话
留下QQ,晚上发一段源码给你参考一下
bing300 2010-06-02
  • 打赏
  • 举报
回复
if (workername != "")
{
s += " and workerName like '%" + workername + "%'"; // sWhere " where isChecked=0 and whatWork=1a1 and workerName like '%陈%'" string

}
就可以把变量workername 设置成SQL变量,假如workername 为空,加入SQL语句中,不影响效果。
antiking 2010-06-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 foren_whb 的回复:]
这段其实是在搞笑!!!!

C# code

switch (type)
{
case "1a1":
s += " and whatWork='"+ type +"'";
break;
case "1a2":
s……
[/Quote]
超级超级。。。。。。
string type = dro_type.SelectedValue;
yuxh81 2010-06-02
  • 打赏
  • 举报
回复
上面那段switch代码确实有点那个……

言归正传,我觉得楼主提的是两个问题:
1:如何sql参数化查询
底层封装好,逻辑层调用时指明参数就行
2:多条件查询如何实现
都是这样拼接的,不过可以做得更清晰一些

丰云 2010-06-02
  • 打赏
  • 举报
回复
这段其实是在搞笑!!!!

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;
}
lirenniao 2010-06-02
  • 打赏
  • 举报
回复
sql语句 里面 也有 if else的
YISUKU_LEE 2010-06-02
  • 打赏
  • 举报
回复
参数化不就是command对象么
要么写sp(存储过程)
要么使用command对象,都是参数化的具体表现。
且,sql是可以使用 if...else 和 case when 的

至于

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;
}



费解啊,
dydxf 2010-06-02
  • 打赏
  • 举报
回复
switch (type)
这段代码还没有写全,下面还有一些其他数据库字段的查询没有写进去。
我的QQ是78655870.
谢谢楼上的

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧