多表DATASET的显示方法,自己乱搞的.
效果:
表现层:
Classlist.ascx
逻辑层:
有没有人能告诉我方便快捷的方法呢?
表现层:
Classlist.ascx
<%@ Control Language="C#" ClassName="classlist" Debug="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
dataclass datacls = (dataclass)Application["data"];
System.Data.DataSet ds = new System.Data.DataSet();
ds=datacls.getclasslist();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i <ds.Tables.Count; i++)
{
PlaceHolder tt = new PlaceHolder();
Label title = new Label();
title.Text = ds.Tables[i].TableName;
title.Font.Bold = true;
tt.Controls.Add(title);
DataList dl=new DataList();
dl.ItemTemplate = Page.LoadTemplate("/conterl/classitem.ascx");
dl.RepeatDirection=(RepeatDirection)0;
dl.RepeatColumns = 4;
dl.DataSource = ds.Tables[i];
dl.DataBind();
tt.Controls.Add(dl);
PlaceHolder1.Controls.Add(tt);
}
}
}
</script>
<div id="le_biaoti02" class="left_biaoti">
<img src="../image/bioti_02.gif" width="60" height="26" />
</div>
<div id="left_fenlei02" class="left_fenlei" runat="server" >
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
</div>
/conterl/classitem.ascx<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
dataclass datacls = (dataclass)Application["data"];
System.Data.DataSet ds = new System.Data.DataSet();
ds=datacls.getclasslist();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i <ds.Tables.Count; i++)
{
PlaceHolder tt = new PlaceHolder();
Label title = new Label();
title.Text = ds.Tables[i].TableName;
title.Font.Bold = true;
tt.Controls.Add(title);
DataList dl=new DataList();
dl.ItemTemplate = Page.LoadTemplate("/conterl/classitem.ascx");
dl.RepeatDirection=(RepeatDirection)0;
dl.RepeatColumns = 4;
dl.DataSource = ds.Tables[i];
dl.DataBind();
tt.Controls.Add(dl);
PlaceHolder1.Controls.Add(tt);
}
}
}
</script>
<div id="le_biaoti02" class="left_biaoti">
<img src="../image/bioti_02.gif" width="60" height="26" />
</div>
<div id="left_fenlei02" class="left_fenlei" runat="server" >
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
</div>
<%@ Control Language="VB" %>
<%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "error") %>
<a href="class.aspx?id=<%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "ID") %>"> <%#DataBinder.Eval(CType(Container, DataListItem).DataItem, "classname")%></a><br />
<%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "error") %>
<a href="class.aspx?id=<%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "ID") %>"> <%#DataBinder.Eval(CType(Container, DataListItem).DataItem, "classname")%></a><br />
逻辑层:
///<summary>
/// 取得分类列表
/// </summary>
public DataSet getclasslist()
{
try
{
string sqlstr = "";
sqlstr = "select id,classname from classname where upid=0";
DataSet dc=new DataSet();
dc=getds(sqlstr);
if (dc.Tables[0].Rows.Count > 0)
{
DataSet dl=new DataSet();
for(int j=0;j<dc.Tables[0].Rows.Count;j++)
{
sqlstr = "select top 3 id,classname from classname where upid=" + (int)dc.Tables[0].Rows[j]["id"];
dl= getdsname(sqlstr, (string)dc.Tables[0].Rows[j]["classname"],dl);
dl.Tables[(string)dc.Tables[0].Rows[j]["classname"]].Columns.Add("error");
DataRow drow;
drow = dl.Tables[(string)dc.Tables[0].Rows[j]["classname"]].NewRow();
drow["id"] = (int)dc.Tables[0].Rows[j]["id"];
drow["classname"] = "";
dl.Tables[(string)dc.Tables[0].Rows[j]["classname"]].Rows.Add(drow);
}
return dl;
}
else
{
throw new Exception("no");
}
}
catch ( Exception ex)
{
DataSet temp = new DataSet();
DataTable dtServer = new DataTable();
DataColumn dterror = new DataColumn("error");
dterror.DataType = typeof(string);
dtServer.Columns.Add(dterror);
dtServer.Columns.Add("id", typeof(int));
dtServer.Columns.Add("classname");
DataRow drowServer;
drowServer = dtServer.NewRow();
drowServer["error"] = ex.Message;
dtServer.Rows.Add(drowServer);
temp.Tables.Add(dtServer);
return temp;
}
}
数据层:/// 取得分类列表
/// </summary>
public DataSet getclasslist()
{
try
{
string sqlstr = "";
sqlstr = "select id,classname from classname where upid=0";
DataSet dc=new DataSet();
dc=getds(sqlstr);
if (dc.Tables[0].Rows.Count > 0)
{
DataSet dl=new DataSet();
for(int j=0;j<dc.Tables[0].Rows.Count;j++)
{
sqlstr = "select top 3 id,classname from classname where upid=" + (int)dc.Tables[0].Rows[j]["id"];
dl= getdsname(sqlstr, (string)dc.Tables[0].Rows[j]["classname"],dl);
dl.Tables[(string)dc.Tables[0].Rows[j]["classname"]].Columns.Add("error");
DataRow drow;
drow = dl.Tables[(string)dc.Tables[0].Rows[j]["classname"]].NewRow();
drow["id"] = (int)dc.Tables[0].Rows[j]["id"];
drow["classname"] = "";
dl.Tables[(string)dc.Tables[0].Rows[j]["classname"]].Rows.Add(drow);
}
return dl;
}
else
{
throw new Exception("no");
}
}
catch ( Exception ex)
{
DataSet temp = new DataSet();
DataTable dtServer = new DataTable();
DataColumn dterror = new DataColumn("error");
dterror.DataType = typeof(string);
dtServer.Columns.Add(dterror);
dtServer.Columns.Add("id", typeof(int));
dtServer.Columns.Add("classname");
DataRow drowServer;
drowServer = dtServer.NewRow();
drowServer["error"] = ex.Message;
dtServer.Rows.Add(drowServer);
temp.Tables.Add(dtServer);
return temp;
}
}
///<summary>
///返回Dataset类型的方法
///</summary>
public DataSet getds(string sql)
{
SqlCommand comm = new SqlCommand(sql, sqlconn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet temp = new DataSet();
int i=da.Fill(temp);
return temp;
}
///<summary>
///返回Dataset类型的方法,带表名,添加新表
///</summary>
public DataSet getdsname(string sql,string name,DataSet ds)
{
SqlCommand comm = new SqlCommand(sql, sqlconn);
SqlDataAdapter da = new SqlDataAdapter(comm);
//DataSet temp = new DataSet();
int i = da.Fill(ds,name);
return ds;
}
以前没有做过这方面的东东,C#也是刚刚才开始用.今天用了一上午,终于做出来了,但这并不是真正的显示DATASET多个表的方法.///返回Dataset类型的方法
///</summary>
public DataSet getds(string sql)
{
SqlCommand comm = new SqlCommand(sql, sqlconn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet temp = new DataSet();
int i=da.Fill(temp);
return temp;
}
///<summary>
///返回Dataset类型的方法,带表名,添加新表
///</summary>
public DataSet getdsname(string sql,string name,DataSet ds)
{
SqlCommand comm = new SqlCommand(sql, sqlconn);
SqlDataAdapter da = new SqlDataAdapter(comm);
//DataSet temp = new DataSet();
int i = da.Fill(ds,name);
return ds;
}
有没有人能告诉我方便快捷的方法呢?