public News getNews(string webtype, string classinfo, string type)
        {
            try
            {
                string sql = "select top 1 * from " + table + " where 1=1";
                if (webtype != null && webtype.Length > 0)
                {
                    sql = sql + " and n_webtype = '" + webtype + "' ";
                }
                if (classinfo != null && classinfo.Length > 0)
                {
                    sql = sql + " and n_class = '" + classinfo + "' ";
                }
                if (type != null && type.Length > 0)
                {
                    sql = sql + " and n_type = '" + type + "' ";
                }
                sql = sql + " and n_state= " + DictState.OPENING + " order by n_news_id desc ";
 
这是项目中一些代码,新人,所以贴出来请教一下。
 string sql = "select top 1 * from " + table + " where 1=1";
1.这个里面select top 1 * 这是怎么理解?另外这里有where 1=1在这里起作用。

解决方案 »

  1.   

    还有,sql = sql + " and n_state= " + DictState.OPENING + " order by n_news_id desc ,这句话又怎么理解呢~新人求教
      

  2.   

    1.这个里面select top 1 * 这是怎么理解?另外这里有where 1=1在这里起作用。 TOP 1 * 是取第一列的所有值,
    WHERE 1=1 没什么只是恒成立,为了后面连接AND 条件
      

  3.   

    select top 1 * 查询第一条数据
    where 1=1 我也不知道什么意思
    但是加与不加这句查询出来的东西没什么改变
    关注
      

  4.   

    select top 1 *    --显示第一行
    where 1=1         --可能是为了防止SQL注入为什么不用parameter呢?
      

  5.   

    string sql = "select top 1 * from " + table + " where 1=1"+" and n_state= " + DictState.OPENING + " order by n_news_id desc "
    这就是一个完整的SQL语句!!字符相加
      

  6.   

    这句是假如你的if中的等式成立的话在原来的sql语句上加上 “and n_state= " + DictState.OPENING + " order by n_news_id desc 
      

  7.   

    where 1=1主要就是动态加条件的时候就是“and **"了,
    top 1应该是找的结果的第一行吧
      

  8.   

    top 1就是取搜索出來的第一筆
    where 1=1 在這里 其實更多的是代碼上的一個處理而已
    可以看他的代碼里面的邏輯
    因為這個Sql是拼接字符串而來的 所以 在select ..... from table 后 不知道要不要加where 如果if 條件里的都不成立 那就不需要加where 如果if  條件里有成立的就需要加where 所以這個判斷就有點繁
      因此就不管三七二十一 先加上where 用select .... from table where 1=1 然后后面滿足一個if條件就拼上去一個and ....
      

  9.   


      public News getNews(string webtype, string classinfo, string type) 
            { 
                try 
                { 
                    string sql = "select top 1 * from " + table + "   //返回结果集中的第一行
    where 1=1";                                                       //方便连接后面的sql条件
                    if (webtype != null && webtype.Length > 0) 
                    { 
                        sql = sql + " and n_webtype = '" + webtype + "' "; 
                    } 
                    if (classinfo != null && classinfo.Length > 0) 
                    { 
                        sql = sql + " and n_class = '" + classinfo + "' "; 
                    } 
                    if (type != null && type.Length > 0) 
                    { 
                        sql = sql + " and n_type = '" + type + "' "; 
                    } 
                    sql = sql + " and n_state= " + DictState.OPENING + " order by n_news_id desc "; 
      

  10.   

    谢谢,那这么说·后面的if判断语句,判断的几种情况,假如出现了webtype,就把这行取出来,假如classinfo 这列就把他们取出来,放在SQL语句里 类似 select   .... from table where 1=1
    就是里面的if判断夹在SQL中间,是不是不符合SQL语法呀~
      

  11.   

    这个就是连接sql语句的SQL语句在这段程序中就是字符串
      

  12.   

    1=1 是恒定条件。
    偏于后面接 And 字符。 不然的话还要写一堆逻辑来确定是否要加 And还是不加。 
    而且1=1 这个条件不影响执行效率的,所以偷懒有理。反正我Pro c/c++ for oracle & .net + sqlserver & php+mysql 都是这么干的。Top 1 / Top n 是取顺序结果的前1(N)条记录。
      

  13.   

    那请问一下~是不是用这个方法,用SQL获取news里面的三个参数呀
      

  14.   

    忘记说了,Top 1 只在SQL_SERVER中有效。 MSSQL是 limit(0,1) Oracle是 rownum<=1
      

  15.   

    写这段语句的人水平有限,写得好傻。在不改变原意的情况下,我给你优化下,你可以参考:            string sql = String.Format("select top 1 * from {0} where n_state= {1} {2} {3} {4} order by n_news_id desc"
                , table
                , DictState.OPENING
                , String.IsNullOrEmpty(webtype) ? "and n_webtype = '" + webtype + "'" : ""
                , String.IsNullOrEmpty(classinfo) ? "and n_class = '" + classinfo + "'" : ""
                , String.IsNullOrEmpty(type) ? "and n_type = '" + type + "'" : "");
      

  16.   

    如你所说,Top N可以取回N条记录,还是N个字段的值呢~我对于这个N没有理解
      

  17.   

    晕,写反了,重新纠正下:            string sql = String.Format("select top 1 * from {0} where n_state= {1} {2} {3} {4} order by n_news_id desc"
                , table
                , DictState.OPENING
                , !String.IsNullOrEmpty(webtype) ? "and n_webtype = '" + webtype + "'" : ""
                , !String.IsNullOrEmpty(classinfo) ? "and n_class = '" + classinfo + "'" : ""
                , !String.IsNullOrEmpty(type) ? "and n_type = '" + type + "'" : "");
      

  18.   

    这是连接数据库的语句,其中字符串代表sql语句,如果你使用过sql2005的数据库工具 
    里面就有sql编辑语句,只不过出c#用到了这一功能,他们是等效的。
      

  19.   

    有SQL2005~就是在表里面查呀~还是利用自己写的方法在程序中查呢~
      

  20.   

    设断点跟踪获取sql,然后在数据路运行sql语句
      

  21.   

    前面回答很正确了,来玩了,这里的1=1 没什么用,只是为了后面 加 and
      

  22.   

    where 1=1 是为了后面的 And 的条件, 不用判断 前面是否有加 where 了 没有
      

  23.   

    答案都已经被说完了  呵呵  这只是SQL语句根据 不同条件来组合 SQL语句的 where 条件。。