public DataTable GetList(int top)
  {
  StringBuilder strSql = new StringBuilder();
  string addsql = "top "+top;
  if (top <= 0)
  {
  addsql = "";
  }  strSql.Append("select " + addsql + " a.ID,a.Author,a.TypeId,a.[Subject],a.[Content],a.[Count],a.date,b.Type ");
  strSql.Append(" FROM Article a join [type] b on a.TypeId = b.ID order by a.ID desc");
  return SQLHelper.ExecuteTable(strSql.ToString());
  }
addSql为什么是一个字段,表示什么意思?a.和b.又是什么?from大小写没关系?数据库中的content和subject都没中括号的,而在代码中加了中括号?count在数据库中加了中括号我理解~

解决方案 »

  1.   

    addSql为什么是一个字段,表示什么意思?  此处为拼接起来的SQL,意思为select top 10 , 10为你此函数传进来的参数。a.和b.又是什么? 是你为查询表起的别名from大小写没关系? 没有关系数据库中的content和subject都没中括号的,而在代码中加了中括号? 加了中括号,表示此处不做系统关键字处理(个人理解,老是这么写,请高人指点)
      

  2.   

    这是拼SQL语句,假如参数Int top=3,则SQL语句为:
    select top 3 a.ID,a.Author,a.TypeId,a.[Subject],a.[Content],a.[Count],a.date,b.Type FROM Article a join [type] b on a.TypeId = b.ID order by a.ID desc
    top 3就是取数据的前3条数据;
    a就是Article表(相当于简写),
    select a.Subject from FROM Article a等同于select Article.Subject from FROM Article,b就是type表了;
    SQl语句是不区人大小写的;
    加中括号是为了避免与数据库关键字冲突
      

  3.   

    addsql是显示几行·如top 5 前5行
    a是Article  别名
    [type]type是关键词
      

  4.   

      addSql  是拼接进去作为了一个查询字段;‘a.’在你的上下文中就表示了‘Article.’,如a.[ID]就表示Article.[ID],即表Article的ID列;‘b.’代表‘type.’ ,b.Type就代表即表type的Type列;中括号:
    有些字段可能是SQL里面的保留字,但是你又用了它做字段名 比如 count,那么用[count]可以避免这个问题