通过拼接SQL字符串实现多条件查询
一、通过拼接SQL字符串的方法的好处是:
1、方便查询条件的扩展。
2、简化业务逻辑的判断。
二、例子:
1、界面设计
2、点击查询的代码
/// <summary>
/// 按条件查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtSearch_Click_1(object sender, EventArgs e)
{
List<string> wherelist = new List<string>();
List<SqlParameter> parametlist = new List<SqlParameter>(); if (CbMzh.Checked)
{
wherelist.Add("mzh=@mzh");
parametlist.Add(new SqlParameter("@mzh", TbMzh.Text));
}
if (CbXm.Checked)
{
wherelist.Add("xm like '%'+@xm+'%'");
parametlist.Add(new SqlParameter("@xm", TbXm.Text));
}
if (CbJbmc.Checked)
{
wherelist.Add("jbzd like '%'+@jbzd+'%'");
parametlist.Add(new SqlParameter("@jbzd", TbJbmc.Text));
}
if (CbJzlb.Checked)
{
wherelist.Add("jzlb=@jzlb");
parametlist.Add(new SqlParameter("@jzlb", ComBoxJzlb.Text));
}
if (CbJzks.Checked)
{
wherelist.Add("jzks=@jzks");
parametlist.Add(new SqlParameter("@jzks", TbJzks.Text));
}
if (CbJzys.Checked)
{
wherelist.Add("jzys=@jzys");
parametlist.Add(new SqlParameter("@jzys", TbJzys.Text));
}
if (CbJzrq.Checked)
{
wherelist.Add("jzrq between @start and @end");
string start = DpStart.Value.ToString("yyyy-MM-dd") + " 00:00:00";
string end = DpEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59";
parametlist.Add(new SqlParameter("@start", start));
parametlist.Add(new SqlParameter("@end", end));
}
if (CbJbmcjqcx.Checked)
{
wherelist.Add("jbzd = @jbzd");
parametlist.Add(new SqlParameter("@jbzd", TbJbmc1.Text));
}
string whereSql = string.Join(" and ", wherelist);
string sql = "select mzh,xm,xb,csny,age,pcid,jzks,jzys,jbzd,zddm,gzdw,jtzz,lxfs,jzlb,jzrq,xy from his_mzjzrz";
if (wherelist.Count > )
{
sql = sql + " where " + whereSql + " order by jzrq desc";
}
else
{
MessageBox.Show("请选择检索条件!");
return;
}
DataTable dt = MsSqlHelper.ExecuteDataTable(sql, parametlist.ToArray());
MzrzGridView.DataSource = dt;
toolStripStatusLabel2.Text = string.Format("共检索到{0}条记录", dt.Rows.Count.ToString());
toolStripProgressBar1.Minimum = ;
toolStripProgressBar1.Maximum = dt.Rows.Count;
for (int i = ; i <= dt.Rows.Count; i++)
{
toolStripProgressBar1.Value = i;
} }
通过拼接SQL字符串实现多条件查询的更多相关文章
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
以前拼接的写法 set @sql=' select * from table where 1=1 ' if (@addDate is not null) set @sql = @sql+' and a ...
- PL/SQL Developer 使用中文条件查询时无数据的解决方法
PL/SQL Developer 使用中文条件查询时无数据,这是由于字符集的不一致导致的. 执行以下sql命令:select userenv('language') from dual; 显示:SIM ...
- SQL 中的多条件查询
在应用程序开发中,多条件查询是个经常遇到的情况,最简单最麻烦的方法是把所有的可能情况都考虑到,但是无疑是繁琐的,而且很容易漏掉可能的情形,下面是SQL语句实现多条件查询的情况 select * fro ...
- PL/SQL Developer 使用中文条件查询时无数据的解决方法(转)
原文地址: PL/SQL Developer 使用中文条件查询时无数据的解决方法 PL/SQL Developer 使用中文条件查询时无数据,这是由于字符集的不一致导致的. 执行以下sql命令:sel ...
- 【批量加入】-拼接sql字符串
如今做的一个项目须要用到批量加入,可是封装的底层没有这种方法,所以自食其力,自己来写.我们用的是拼接sql字符串的方法来实现功能. 详细实现流程:首先将须要的数据存储到实体的list中,然后将这个li ...
- 六、SQL语句进行多条件查询,并解决参数为空的情况
一.SQL语句进行多条件查询,并解决参数为空的情况 QueryEntity query; var whereSql = new StringBuilder("Where 1=1") ...
- 【批量添加】-拼接sql字符串 标签: 批量添加 2015-12-13 17:49 2070人阅读 评论(33)
现在做的一个项目需要用到批量添加,但是封装的底层没有这个方法,所以自食其力,自己来写.我们用的是拼接sql字符串的方法来实现功能. 具体实现流程:首先将需要的数据存储到实体的list中,然后将这个li ...
- 查询拼接SQL语句,多条件模糊查询
多条件查询,使用StringBuilder拼接SQL语句,效果如下: 当点击按钮时代码如下: private void button1_Click(object sender, EventArgs e ...
- MyBatis中动态SQL语句完成多条件查询
一看这标题,我都感觉到是mybatis在动态SQL语句中的多条件查询是多么的强大,不仅让我们用SQL语句完成了对数据库的操作:还通过一些条件选择语句让我们SQL的多条件.动态查询更加容易.简洁.直观. ...
随机推荐
- [LeetCode] Masking Personal Information 给个人信息打码
We are given a personal information string S, which may represent either an email address or a phone ...
- 权限系统设计-day02
练习中的问题: 1,<s:url action="employee_input" />这个标签用来让struts自动生成请求的路径,struts生成的路径是一个全路径, ...
- 离线安装多版本node,使用nvm管理
windows环境下,使用nvm客户以方便地管理多个node版本,但有时候可能需要离线安装node版本. 结合网络搜搜索结果,多次尝试后我成功在离线安装了多个node版本,方法: 1.在其他联网环境下 ...
- Java作业六(2017-10-30)
/*游戏引擎包,播放音乐*/ import com.rupeng.game.GameCore; public class Mc implements Runnable{ public static v ...
- [复试机试]c++读取/写入文本文件
读取文件 #include <iostream> #include <cstdio> #include <string> #include <cstdlib& ...
- [Swift]LeetCode312. 戳气球 | Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- [Swift]LeetCode865. 具有所有最深结点的最小子树 | Smallest Subtree with all the Deepest Nodes
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...
- [Swift]LeetCode1018. 可被 5 整除的二进制前缀 | Binary Prefix Divisible By 5
Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a bi ...
- Redis 设计与实现 (八)--排序、慢查询日志、监视器
一.排序 SORT <key> 对一个数字值的key进行排序 1.alpha 对字符串类型的键进行排序 2.asc / desc redis 默认升序排序asc desc 与之相反 3. ...