求教关于多个Label如何用少量代码和数据库绑定?首先我做了一个表格,里面大约有10个Label,功能是调出每个部门的数量第一个Label我现在是这样写的测试通过
protected void lb1_Init(object sender, EventArgs e)
    {
        string sqlsum = "select count(number) as total from equipment where locus='检修西部'";
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        SqlCommand thisCommand = new SqlCommand(sqlsum,conn);
       
        thisCommand.CommandType = CommandType.Text;
        try
        {
            thisCommand.Connection.Open();
            SqlDataReader dr = thisCommand.ExecuteReader();
            while (dr.Read())
            {
                this.lb1.Text = dr["total"].ToString();
            }
}还有9个Label,但每个都这样写一堆一定很难看,而且又麻烦想知道有什么更快的方法可以一次搞定9个Label?谢谢啦

解决方案 »

  1.   

    你可以只用一条SQL将10个搜查出来的资料放在一个table中,
    然后
    for(int i=0;i<10:i++)
    {
    }
    循环一下就能将资料分别赋给10个label了这样只要读一次数据库,而不需要读10次了。
      

  2.   

    闲难看就在aspx代码中绑定吧<asp:Label runat="server" Text="<%Eval("字段")%>"></asp:Label>
      

  3.   

    谢谢 但我不是要绑定字段那么简单,而是要计算出一个数量,应为我没有做表
    一楼的for(int i=0;i <10:i++) 怎么写啊能给写具体代码吗?
      

  4.   


    顺便提醒下楼主, 用数据控件岂不是更简单?一个label放在模板列,哪怕有100,1000个,同样给你显示出来你这样做太麻烦呵呵
      

  5.   

    protected void lb1_Init(object sender, EventArgs e) 
        { 
            //按部门id排序统计各个部门数量
            string sqlsum = "select count(number)as total,deptid from equipment order by deptid; 
            SqlConnection conn = new SqlConnection(); 
            conn.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"]; 
            SqlCommand thisCommand = new SqlCommand(sqlsum,conn); 
          
            thisCommand.CommandType = CommandType.Text; 
            try 
            { 
                thisCommand.Connection.Open(); 
                SqlDataReader dr = thisCommand.ExecuteReader(); 
                int i=1;
                //按顺序绑定
                while (dr.Read()) 
                { 
                    Lable lb=(Label)Page.FindControl("lb"+i.ToString());
                    lb.Text = dr["total"].ToString(); 
                } 
      

  6.   

    我按照我理解的说啊。
    我不清楚你的10个字段分别是什么啊。
    我自己打个比方,第一个lable放数量,第二个lable放甲部门,第三个lable放乙部门老大名字
    ……
    你在SQL中用union把所有的东西放到一个table中去,表的结构如下:txt
    ----------
    35
    IT部門
    孔乙己
    ……然后在C#中你将它们一条条的串成一个string,如“35,IT部门,孔乙己……”//代码只是一个思路
            DataTable txtRead = DBHelp.ExecuteReader(strSQL);
            string txt= "";
            if (txt!= null && txtRead.Rows.Count > 0)
            {
                foreach (DataRow dr in txtRead.Rows)
                {
                    txt+= dr[0].ToString().Trim();
                    txt+= ",";
                }
            }
            if (!string.IsNullOrEmpty(txt))
            {
                txt= txt.TrimEnd(',');
            }
            return txt;最后你用C#的split切割字符串,再将数组中的赋给lable.string[] Field = txt.Split(',');
                lbSubject.Text = Field[0];
                lbClass.Text = Field[1];
                lbContent.Text = Field[2];
                 ……