问题描述
Datalist1显示say这个表的数据然后想在Datalist1中嵌套一个Datalist2用于显示对应的评论表2的sayID对应表1的id,若表2中找不到对应sayId则在对应的Datalist1中不显示Datalist2.难点在于如何达到“林夏”的说说下面只显示“黄磊”、“叶伟信”的评论,“韩寒”的下面只显示“程程”的评论。(林夏的id是3,所以查询对应的表2中sayId为3的行)。<body><formid="form1"runat="server"><div><asp:DataListID="DataList1"runat="server"DataKeyField="id"DataSourceID="SqlDataSource1"><ItemTemplate>id:<asp:LabelID="idLabel"runat="server"Text='<%#Eval("id")%>'/><br/>username:<asp:LabelID="usernameLabel"runat="server"Text='<%#Eval("username")%>'/><br/>text:<asp:LabelID="textLabel"runat="server"Text='<%#Eval("text")%>'/><br/>date:<asp:LabelID="dateLabel"runat="server"Text='<%#Eval("date")%>'/><br/>place:<asp:LabelID="placeLabel"runat="server"Text='<%#Eval("place")%>'/><br/><br/><asp:DataListID="DataList2"runat="server"></asp:DataList><br/></ItemTemplate></asp:DataList><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="DataSource=.;InitialCatalog=D:DESKTOP15321712401APP_DATADING.MDF;IntegratedSecurity=True"ProviderName="System.Data.SqlClient"SelectCommand="SELECT*FROM[say]"></asp:SqlDataSource></div></form></body>
解决方案
本帖最后由 u014090746 于 2016-06-19 12:05:15 编辑
解决方案二:
解决方案三:
楼上大神异常专业的回答
解决方案四:
引用1楼guwei4037的回复:web.config文件中数据连接为<configuration><connectionStrings><addname="conStr"connectionString="DataSource=.;AttachDbFilename=|DataDirectory|ding.mdf;UserID=sa;Password=1"providerName="System.Data.SqlClient"/><addname="ding_sql"connectionString="DataSource=.;AttachDbFilename=|DataDirectory|ding.mdf;IntegratedSecurity=true"/></connectionStrings><system.web><compilationdebug="true"targetFramework="4.0"><assemblies><addassembly="System.Configuration.Install,Version=4.0.0.0,Culture=neutral,PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation><pagescontrolRenderingCompatibilityVersion="3.5"clientIDMode="AutoID"/></system.web></configuration>
表1的表名是say表2的表名是say_back请大神指点如何把这一段换成ado.net连接查询数据库十分感谢您的耐心解答publicDataSetData(){DataSetds=newDataSet();DataTabledtParent=newDataTable();dtParent.Columns.Add("id",typeof(int));dtParent.Columns.Add("username",typeof(string));dtParent.Columns.Add("text",typeof(string));dtParent.Columns.Add("date",typeof(string));dtParent.Columns.Add("place",typeof(string));dtParent.Rows.Add(1,"摩根","这个...","2014.6","伦敦");dtParent.Rows.Add(2,"韩寒","潮流...","2015.7","湖南");dtParent.Rows.Add(3,"林夏","岁月...","2016.2","小城");dtParent.Rows.Add(4,"王菲","我会...","2015.9","香港");dtParent.Rows.Add(5,"郭敬明","我们...","2015.1","上海");DataTabledtChild=newDataTable();dtChild.Columns.Add("id",typeof(int));dtChild.Columns.Add("sayId",typeof(int));dtChild.Columns.Add("username",typeof(string));dtChild.Columns.Add("text",typeof(string));dtChild.Columns.Add("date",typeof(string));dtChild.Rows.Add(1,3,"黄磊","我是第一个评说","2016.6.1713:06:12");dtChild.Rows.Add(3,3,"叶伟信","评论林夏的说","2016.6.1712:26:43");dtChild.Rows.Add(4,2,"程程","我评论韩寒","2015.3.115:20:10");ds.Tables.AddRange(newDataTable[]{dtParent,dtChild});DataRelationdr=newDataRelation("relation",dtParent.Columns["id"],dtChild.Columns["sayId"]);ds.Relations.Add(dr);dtParent=ds.Relations["relation"].ParentTable;dtChild=ds.Relations["relation"].ChildTable;returnds;}
解决方案五:
publicDataSetData(){using(SqlConnectionconn=newSqlConnection("server=.;uid=sa;pwd=123456;database=test20151026;")){conn.Open();SqlCommandcmd=newSqlCommand("select*fromtopic;select*fromcomment;",conn);SqlDataAdaptersda=newSqlDataAdapter(cmd);DataSetds=newDataSet();sda.Fill(ds,"tables");DataTabledtParent=ds.Tables[0];DataTabledtChild=ds.Tables[1];DataRelationdr=newDataRelation("relation",dtParent.Columns["id"],dtChild.Columns["sayId"]);ds.Relations.Add(dr);returnds;}}