C# SQL多条件查询拼接技巧

本文转载:http://blog.csdn.net/limlimlim/article/details/8638080

   #region 多条件搜索时,使用List集合来拼接条件(拼接Sql)

            StringBuilder sql = new StringBuilder("select * from PhoneNum");
            List<string> wheres = new List<string>();
            if (cboGroup.SelectedIndex != 0)
            {
                wheres.Add(" ptypeid=" + cboGroup.Text.Split(‘|‘)[0]);
            }

            if (txtSearchName.Text.Trim().Length > 0)
            {
                 wheres.Add(" pname like ‘%" + txtSearchName.Text.Trim() + "%‘");
            }

            if (txtSearchCellPhone.Text.Trim().Length > 0)
            {
                 wheres.Add(" pcellphone like ‘%" + txtSearchCellPhone.Text.Trim() + "%‘");
            }

            //判断用户是否选择了条件
            if (wheres.Count > 0)
            {
                string wh = string.Join(" and ", wheres.ToArray());
                sql.Append(" where " + wh);
            }
            #endregion

            #region 多条件搜索使用带参数的sql语句

            StringBuilder sql = new StringBuilder("select * from PhoneNum");
            List<string> wheres = new List<string>();
            List<SqlParameter> listParameter = new List<SqlParameter>();

            if (cboGroup.SelectedIndex != 0)
            {
                wheres.Add(" [email protected] ");
                listParameter.Add(new SqlParameter("@typeid", cboGroup.Text.Split(‘|‘)[0]));
            }

            if (txtSearchName.Text.Trim().Length > 0)
            {
                wheres.Add(" pname like @pname ");
                //pname like ‘%乔%‘
                //pname liek ‘%‘[email protected]+‘%‘
                listParameter.Add(new SqlParameter("@pname", "%" + txtSearchName.Text.Trim() + "%"));
            }

            if (txtSearchCellPhone.Text.Trim().Length > 0)
            {
                wheres.Add(" pcellphone like @cellphone ");
                listParameter.Add(new SqlParameter("@cellphone", "%" + txtSearchCellPhone.Text.Trim() + "%"));
            }

            //判断用户是否选择了条件
            if (wheres.Count > 0)
            {
                string wh = string.Join(" and ", wheres.ToArray());
                sql.Append(" where " + wh);
            }

            SqlHelper.ExecuteDataTable(sql.ToString(), listParameter.ToArray());
            #endregion

  

C# SQL多条件查询拼接技巧

时间: 2024-07-29 18:05:59

C# SQL多条件查询拼接技巧的相关文章

sql 多条件查询 拼接字符串 改成 普通查询格式

set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROC [dbo].[usp_SRV_CheckServiceDemandOrder] @AInsNO NVARCHAR(50) =null,--必填 @ACompanyName NVARCHAR(50) = null,--必填 @ADepartmentName NVARCHAR(50) = null, @AName NVARCHAR(50) = null, --必填 @ApplicantI

Linq to Sql 多条件查询

Linq To Sql 多条件查询 string proName = this.txtName.Text.Trim();string lowPrice = this.txtLowPrice.Text.Trim();string highPrice = this.txtHighPrice.Text.Trim(); decimal? lowPrice1 = null, highPrice1 = null;if (!string.IsNullOrEmpty(lowPrice)){        low

C# SQL 多条件查询技巧

#region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneNum");            List<string> wheres = new List<string>();            if (cboGroup.SelectedIndex != 0)            {                wheres.

SQL多条件查询安全高效比较

ALTER PROCEDURE _tmp @ID VARCHAR(50), @PN VARCHAR(50), @Type INT AS BEGIN /********************************** -- 功能:多条件查询性能 _tmp 'K3G8KG6NN94SBBS0','K7F7FF',0 **********************************/ PRINT '测试数据条数500W' set nocount ON DECLARE @time DATETIM

Webform中linq to sql多条件查询(小练习)

多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: 1 <body> 2 <form id="form1" runat="server"> 3 4 <br /> 5 <asp:Label ID="Label1" runat="server" Text="关键字:"></asp:Label&g

qt sql多重条件查询简便方法

转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情况,解决方案: QSqlQuery query(m_db); QString province = QString::fromLocal8Bit("广东"); QString city = ""; QString null = ""; QString

SQL 多条件查询

网上有不少人提出过类似的问题:“看到有人写了WHERE 1=1这样的SQL,到底是什么意思?”.其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查询界面来根据各种复杂的条件来查询员工信息,界面如下图: 界面中列出了四个查询条件,包括按工号查询.按姓名查询.按年龄查询以及按工资查询,每个查询条件前都有一个复选框,如果复选框被选中,则表示将其做为一个过滤条件.比如上图就表示“检索工号介于DEV001和DEV008之间.姓名中含有J并且工资介于30

SQL 时间条件查询

to_date()使用 select * from table t where t.time >= to_date(aaaa,'yyyy-mm-dd hh24:mm:ss') and t.time<to_date(bbbb,'yyyy-mm-dd hh24:mm:ss') aaaa,bbbb是字符串类型 比如:aaaa = '2018-04-19 00:00:00' bbbb = '2018-04-20 00:00:00' to_date()中yyyy-mm-dd hh24:mm:ss 意思把

SQL多条件查询

SELECT a.tel,a.business_code,b.name AS business_name,a.register_time FROM T_RED_USER a LEFT JOIN T_Product_Spec b ON a.business_code=b.code where a.tel LIKE CONCAT(?,'%') AND (IF(?='', 1=1, IFNULL(a.business_code,'')=?) OR a.business_code IS NULL ) O