我有几个数据表其中表 thread,结构如下threadid threadtitle threadcontent zzid classid表 zzzzid zzname表 classclassid classtitle表commentcommentid  threadid我现在想读出数据表thread,zz,class,comment中所有的数据,形式如,一个BLOG的帖子中有作者,帖子相关的内容,该帖的分类,并显示comment中对应该帖子对应得到回复的条数。.aspx.cs代码如下 
public string blog() 
    {         string  strSql = "SQL语句"; 
        string strConnection = ConfigurationSettings.AppSettings["dns"]; 
        SqlConnection objConnection = new SqlConnection(strConnection); 
        objConnection.Open(); 
        SqlCommand cmd = new SqlCommand(strSql, objConnection); 
        SqlDataReader dr = cmd.ExecuteReader(); 
        string strBody = null; 
        while (dr.Read()) 
        {                 strBody += "相关的内容"; 
        } 
        dr.Close(); 
        objConnection.Close(); 
        return strBody; 
    } 在.aspx中通过 <%=blog()%>获得所有的满足条件的数据,请问该怎么加代码呢?谢谢

解决方案 »

  1.   

    strSql="select a.*,zzname,classtitle,(select count(*) from comment where a.threadid =threadid )
    from thread a left join zz b on a.zzid =b.zzid  left join class c on a.classid=c.classid"
     strBody += dr[""];//用table或控件实现数据显示
      

  2.   

    这个SQL语句不行,还有其他办法吗
      

  3.   

    select a.threadid,a.threadtitle,a.threadcontent,
           b.zzid,c.classid,b.zzname,c.classtitle,
           d.commentid
    from zz as b join thread as a
    on a.zzid=b.zzid
    join class as c
    on a.classid =c.classid
    join thread as a 
    on a.classid=c.classid
    join comment as d
    on d.threadid=a.threadid试试,如果不行的话,你就直接在企业管理器中建视图算了,视图很方便的。两下就搞定了。