zoukankan      html  css  js  c++  java
  • 多表DATASET的显示方法,自己乱搞的.

    效果:
    {030A7B52-98FE-4378-A41D-6578B5532DF3}.JPG
    表现层:
    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
    <%@ Control Language="VB"  %>
     
    <%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "error"%>
     
    &nbsp;<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>
        
    ///返回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多个表的方法.
    有没有人能告诉我方便快捷的方法呢?
  • 相关阅读:
    git中Please enter a commit message to explain why this merge is necessary.
    用$(this)选择其下带有class的子元素
    将某页面中ajax中获取到的信息放置到sessionStorage中保存,并在其他页面调用这些数据。
    返回顶部黑科技
    对于div里面内容过大根据长度或者宽度进行适配,然后可以滚轮缩放的功能
    vue runtime报错问题
    webpack简单配置
    input type=color 设置颜色
    vue统一注册组件
    vue模板字符串写法
  • 原文地址:https://www.cnblogs.com/aowind/p/532355.html
Copyright © 2011-2022 走看看