多条件Sql语句

public static string doQuery()
        {
            bool hasWhere = false;
            StringBuilder sql = new StringBuilder("select * from table");
            if (true)
            {
                hasWhere = appendWhereIfNeed(sql, hasWhere);
                sql.AppendLine("FNumber between ' 工号文本框1内容 ' and '工号文本框2内容 '");
            }
            if (true)
            {
                hasWhere = appendWhereIfNeed(sql, hasWhere);
                sql.AppendLine("FNumber2 between ' 工号文本框1内容 ' and '工号文本框2内容 '");
            }
            if (true)
            {
                hasWhere = appendWhereIfNeed(sql, hasWhere);
                sql.AppendLine("FNumber3 between ' 工号文本框1内容 ' and '工号文本框2内容 '");
            }
            return sql.ToString();
             
        }

        private static bool appendWhereIfNeed(StringBuilder sql, bool hasWhere)
        {
            if (hasWhere == false)
            {
                sql.AppendLine(" where ");
                hasWhere = true;
            }
            else
            {
                sql.AppendLine(" and ");

            }
            return hasWhere;

        }

posted @ 2012-09-05 18:23  读者4074  阅读(264)  评论(0编辑  收藏  举报