group by的简单使用例子

2015-04-06 12:14:00  访问(1340) 赞(0) 踩(0)

生成和输出的SQL语句

select 
(count(t.ID)) as '学生个数', 
tc.TheName as TheName
from UTB_EXAM_STUDENT t , 
UTB_EXAM_CLASS tc
where t.ClassId = tc.ID group by 
tc.ID, tc.TheName
order by 
tc.ID asc


关键代码

 igroup.AddIGroupByItemWithEntityFieldInfo(entityClass._ID);
            igroup.AddIGroupByItemWithEntityFieldInfo(entityClass._TheName);


代码写法

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SlowX.DAL.Helpers;
using SlowX.Core.ICoreClasses;
using System.Data;
using SlowX.Functions.Functions;
using SlowX.Core.CoreClasses;

public partial class ExamSQL : System.Web.UI.Page
{
    protected string strResult = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        DataBindTheContorls(null);
    }
    #region 操作

    /// <summary>
    /// 
    /// </summary>
    /// <param name="xdbHelper"></param>
    protected void DataBindTheContorls(DBHelper xdbHelper)
    {
        string theResult = null;
        bool bIsCreate = true;

        if (xdbHelper == null)
        {
            xdbHelper = SlowX.DAL.Helpers.DBHelper.CreateDBHelper();
        }
        else
        {
            // 没有打开链接 //
            bIsCreate = xdbHelper.IsNotOpen();
        }

        try
        {
            if (bIsCreate)
                xdbHelper.OpenDBHelper();

            SlowX.ExamLib.Business.UTB_EXAM_STUDENT 
                bll
                =
                SlowX.ExamLib.Business.UTB_EXAM_STUDENT.instance;

            SlowX.ExamLib.Entity.UTB_EXAM_STUDENT 
                entity
                =
                new SlowX.ExamLib.Entity.UTB_EXAM_STUDENT();

            SlowX.ExamLib.Entity.UTB_EXAM_CLASS
               entityClass
               =
               new SlowX.ExamLib.Entity.UTB_EXAM_CLASS();

            IFromTableDriver ifrom = entity;
            ISelectDriver iselect = entity;
            IQueryDriver iq = entity;
            IGroupByDriver igroup = entity;

            ifrom.AddIFromTableItemWithBaseEntity("tc", entityClass);

            iselect.AddISelectItemWithEntityFieldInfo
                ( 
                    SlowX.Core.Enums.SqlCommonFn.EmSqlCommonFn._Count,
                    "'学生个数'",
                    entity._ID
                );

            iselect.AddISelectItemWithEntityFieldInfo
                (
                    entityClass._TheName
                );

            IOrderByDriver iorder = entity;
            iorder.AddIOrderByItemWithEntityFieldInfo
                (
                    entityClass._ID, 
                    SlowX.Core.Enums.OrderBy.EmOrderBy.ASC
                );

            iq.AddIQueryItemWithEntityFieldInfo(entity._ClassId, entityClass._ID);


            igroup.AddIGroupByItemWithEntityFieldInfo(entityClass._ID);
            igroup.AddIGroupByItemWithEntityFieldInfo(entityClass._TheName);

            DataSet ds = bll.List(entity, xdbHelper);

            theResult =
                @"<textarea name=""TextBox1"" rows=""2"" cols=""20"" id=""TextBox1"" style=""height:400px;width:99%;"">"
                + bll.BuildSqlList(entity, xdbHelper)
                + "</textarea>";

            theResult += "<br /><br />";

            theResult += DataSetSlowXFunctions.DataSetToHtmlTable(ds, true);



            if (bIsCreate)
                xdbHelper.EndDBHelper();

        }
        catch (Exception err)
        {
            if (bIsCreate)
                xdbHelper.TranDBHelper();

            throw err;
        }
        finally
        {
            if (bIsCreate)
                xdbHelper.FinallyDBHelper();
        }

        strResult = theResult;
    }

    #endregion 操作


}




标签:group by的简单使用例子 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)