示例#1
0
        /// <summary>
        /// 创建时间查询的SQL片段
        /// </summary>
        /// <param name="FieldName">查询字段名称</param>
        /// <param name="Op">查询操作符</param>
        /// <param name="dt">日期时间</param>
        /// <param name="IgnoreTime">是否忽略时间</param>
        /// <returns></returns>
        public virtual string CreateDateTimeSQLSegment(string FieldName, QueryOperatorEnum Op, DateTime dt, bool IgnoreTime)
        {
            if (String.IsNullOrEmpty(FieldName))
            {
                throw new ArgumentNullException("FieldName is empty");
            }
            if (Op == QueryOperatorEnum.In || Op == QueryOperatorEnum.Inline || Op == QueryOperatorEnum.Like)
            {
                throw new ArgumentException("日期查询不支持当前操作符:" + QueryOperator.ConvertToOperator(Op));
            }
            if (!FieldName.EndsWith(" "))//添加一个空格
            {
                FieldName += " ";
            }

            //按照时间精确查询
            if (!IgnoreTime)
            {
                return(FieldName + QueryOperator.ConvertToOperator(Op) + ConvertDateTimeToSQL(dt));
            }

            //忽略时间,按照日期查询
            string   Date    = dt.ToString("yyyy-MM-dd");
            DateTime DtBegin = Convert.ToDateTime(Date + " 00:00:00");
            DateTime DtEnd   = Convert.ToDateTime(Date + " 23:59:59");

            if (Op == QueryOperatorEnum.Equal)  //在这一天之内
            {
                return("(" + FieldName + ">=" + ConvertDateTimeToSQL(DtBegin) + " AND " +
                       FieldName + "<=" + ConvertDateTimeToSQL(DtEnd) + ")");
            }
            if (Op == QueryOperatorEnum.Great)  //比这一天大
            {
                return(FieldName + ">" + ConvertDateTimeToSQL(DtEnd));
            }
            if (Op == QueryOperatorEnum.Less)     //比这一天小
            {
                return(FieldName + "<" + ConvertDateTimeToSQL(DtBegin));
            }
            if (Op == QueryOperatorEnum.GreatAndEqual)//比这一天大,包括这一天
            {
                return(FieldName + ">=" + ConvertDateTimeToSQL(DtBegin));
            }
            if (Op == QueryOperatorEnum.LessAndEqual)//比这一天小,包括这一天
            {
                return(FieldName + "<=" + ConvertDateTimeToSQL(DtEnd));
            }
            if (Op == QueryOperatorEnum.NotEqual)//比这一天小或者比这一天大
            {
                return("(" + FieldName + "<" + ConvertDateTimeToSQL(DtBegin) + " OR " +
                       FieldName + ">" + ConvertDateTimeToSQL(DtEnd) + ")");
            }
            else
            {
                throw new ArgumentException("日期查询不支持当前操作符:" + QueryOperator.ConvertToOperator(Op));
            }
        }