• 通过拼接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 > 0)
                {
                    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 = 0;
                toolStripProgressBar1.Maximum = dt.Rows.Count;
                for (int i = 0; i <= dt.Rows.Count; i++)
                {
                    toolStripProgressBar1.Value = i;
                }
    
            }
  • 相关阅读:
    在Oracle怎样查询表中的top10条记录
    Ant
    oracle 时间函数(sysdate)
    oracle 时间函数 (to_date)
    Maven仓库管理器
    数据库中select into from 和 insert into select的区别
    oracle 定时器简单用法
    oraclea 定时器
    XP Home Edition SP2 也可以装 Rational Rose 2003
    Unity3D动态天空之UniSky
  • 原文地址:https://www.cnblogs.com/flywong/p/8341315.html
Copyright © 2020-2023  润新知