前言:

在做重构组合查询的时候难道我了,很多巨人都是用的Windows窗体,想站在巨人的肩膀上还不让踩,自己用WPF想要做个继承还挺难,于是就换了一种查询方式——模糊查询

经过这次练习学习到了模糊查询如何用了。

通配符

% 表示任意个或多个字符。可匹配任意类型的长度的字符 _ 表示任意单个字符。匹配单个任意字符,常用来限制表达式的字符长度语句(可以代表一个中文字符)
//查询name字段中含有数字的。
select * from table1 where name like '%[0-9]%'
//查询name字段中含有小写字母的。
select * from table1 where name like '%[a-z]%'
//查询name字段中不含有数字的。
select * from table1 where name like '%[!0-9]%'

充值查询窗体使用LIKE

窗体展示:

重构之模糊查询LIKE的使用

查询功能主要是访问数据库,所以我们的模糊查询就应写在D层,当我们要查询一个条件时,如果像卡号输入在去Like‘%[0-9]%’的话作用不大,因为我不管输入任何数字我所有的卡号都会被查询出来,怎么办呢?来看我D层的代码:

//模糊查询 充值表
        public class RechargeRecordDAL:IDAL.RechargeRecordIDAL
        {
            
            public List<ReChargeInfo> SltStudent(ReChargeInfo ReChargeInfo)
            {
                SQLHelper sqlHelper = new SQLHelper();
                SqlParameter[] sqlParams =
                {
                    new SqlParameter("@CardNo",ReChargeInfo.CardNo),
                    new SqlParameter("@RechCash",ReChargeInfo.RechCash),
                    new SqlParameter("@RechDate",ReChargeInfo.RechDate),
                    new SqlParameter("@UserID",ReChargeInfo.UserID)
                };
                string sql = @"SELECT * FROM[T_ReCharge]WHERE (CardNo like '%'[email protected]+'%') and (RechCash like '%'[email protected]+'%') and (RechDate >[email protected]) and (UserID like '%'[email protected]+'%')";
                DataTable dt = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
                ConvertSQLHelper convert = new ConvertSQLHelper();
                List<ReChargeInfo> list = convert.ConvertToModel<ReChargeInfo>(dt);
                return list;
            }
        }

还是像其他的D层一样,把将会输入进去的给个参数再传进去, 只不过在SQL语言中where的符号写的不同了。

相关文章: