ORMBase对象/关系型数据库映射在MVC中的应用(二)

时间:2014-05-14 12:14:27   收藏:0   阅读:396

  3、DataBase基类,查询方法返回值是List<T>,并且是分页的,ThePart.dll版本2.0中封装了一个PageInfo类,作为分页的类型。这种方法很机械,也很狗血..建议大家不这么用,下面这个例子是项目中同事写的。为了DataBase这个基类的操作数据库返回值各种类型,让人们知道各种情况的数据都可以处理。的这么一个目的,我还是把例子贴出来。我自己都难得去费神看。

        public static List<SportsBetRecords> SportsGetListPager(string SaleID, string UserName, string status, string classtype, string StarTime, string EndTime, int? pSize, int? pIndex, out int count)
        {
            pSize = pSize ?? 10;  //每页最大记录数
            pIndex = pIndex ?? 1;  //当前第几页
            PagerInfo pager = new PagerInfo();  //ThePart.dll中封装的这么一个分页类PagerInfo
            pager.ProcName = "pr_Pager2005";  //需要的数据库的存储过程
            pager.TableNames = "SportsBetRecords";  //需要操作的数据库表名
            pager.PageSize = (int)pSize;    //最大记录数
            pager.CurrentPage = (int)pIndex;    //当前第几页
            StringBuilder sb = new StringBuilder("1=1 and SaleID=‘" + SaleID + "‘");  //可变字符串的条件
            if (!string.IsNullOrEmpty(UserName))
                sb.Append(" and UserName=‘" + UserName + "‘");  
            if (!string.IsNullOrEmpty(StarTime) && !string.IsNullOrEmpty(EndTime))
                sb.Append(" and [T] between ‘" + Convert.ToDateTime(StarTime) + "‘ and ‘" + Convert.ToDateTime(EndTime).AddDays(1) + "‘");
            pager.Condition = sb.ToString();    //sql里面where后的条件
            pager.Count = 1;
            pager.Fields = "*";
            pager.IndexName = "Id";
            pager.OrderNames = "[T] desc";
            List<SportsBetRecords> recordList = ORMBase.GetList<SportsBetRecords>(ref pager);
            count = pager.Count;
            return recordList;
        }

 

以下是相应该存储过程:

USE [Casino]
GO
/****** Object:  StoredProcedure [dbo].[pr_Pager2005]    Script Date: 05/13/2014 15:05:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[pr_Pager2005]
(
    @TableNames VARCHAR(4000), -- 表名
    @Fields VARCHAR(1000) = ‘*‘, -- 需要返回的列(不能有相同的字段出现,如有相同字段,可把第一个外的其它字段转为别名,如:b.ID As bID)
    @Condition VARCHAR(1500) = ‘‘, -- 查询条件
    @IndexName VARCHAR(20) = ‘ID‘, -- 主索引字段(此处作废,只为兼容)
    @PageSize INT = 20, -- 每页记录数
    @CurrentPage INT = 1, -- 当前页码
    @OrderNames VARCHAR(50) = ‘‘, -- 排序字段,要包括排序类型
    @OrderType BIT = 1, -- 排序类型,0-降序  1-升序(默认)(此处作废,只为兼容)
    @Count INT=null OUTPUT ,
    @Distinct VARCHAR(9)=‘‘, --去重复记录distinct
    @GroupBy VARCHAR(100)=‘‘,
    @TSQL nVARCHAR(4000)=‘‘ OUTPUT --本次执行的TSQL语句或在分页前执行的TSQL
)
AS
    DECLARE @SQL nVARCHAR(max) -- 主语句
    DECLARE @OrderBy VARCHAR(300) -- 排序
    DECLARE @StartRow INT,@ENDRow INT

    SET @SQL = ‘‘
    
    BEGIN
        SET NOCOUNT ON
        IF(@CurrentPage<1)
            SET @CurrentPage = 1

        SET @StartRow = (@CurrentPage-1)*@PageSize+1
        SET @ENDRow = @CurrentPage*@PageSize

        IF(@Condition != ‘‘)
            SET @Condition = ‘ Where ‘ + @Condition

        IF(@GroupBy!=‘‘)
            SET @GroupBy = ‘ Group By ‘ + @GroupBy

        IF(@Distinct!=‘‘)
            SET @Distinct = @Distinct + ‘ ‘

        IF(@OrderNames = ‘‘)
            SET @OrderBy = ‘‘
        ELSE
            SET @OrderBy = ‘ Order By ‘ + @OrderNames

        IF ((@Count is not null) And @Count > 0 )
        BEGIN

            IF(@GroupBy=‘‘)
                SET @SQL = @TSQL + ‘ Select ‘ +@Distinct+ ‘ @c=Count(0) From ‘ + @TableNames + @Condition
            ELSE
                SET @SQL = @TSQL + ‘ Select ‘ +@Distinct+ ‘ @c=Count(0) From (Select 0 As ID From ‘ + @TableNames + @Condition + @GroupBy +‘) As T‘

            EXEC sp_executesql @SQL,N‘@c INT OUTPUT‘,@Count OUTPUT
        END

        --以下实现了带Group By和不带Group By的情况
        IF(@GroupBy=‘‘)
        Begin
            SET @SQL = @TSQL + ‘ With T1 As ( Select ‘ + @Distinct + @Fields + ‘,Row_Number() Over (‘ + @OrderBy +‘) As RowNo From ‘ + @TableNames + @Condition + ‘ ) ‘
            SET @SQL = @SQL + ‘Select * From T1 Where RowNo Between ‘ + Cast(@StartRow As VARCHAR) + ‘ And ‘ + Cast(@ENDRow AS VARCHAR)
        END
        ELSE
        Begin
            SET @SQL = @TSQL + ‘ With T1 As ( Select ‘ + @Distinct + @Fields +‘ From ‘ + @TableNames + @Condition + @GroupBy + ‘ ) ‘
            SET @SQL = @SQL + ‘Select * From (Select *,Row_Number() Over (‘ + @OrderBy +‘) As RowNo From T1) As T2 Where RowNo Between ‘ + Cast(@StartRow As VARCHAR) + ‘ And ‘ + Cast(@ENDRow As VARCHAR)
        END
    END
EXEC (@SQL)

 

  4、直接返回所有List表记录:不需要查询条件和写sql语句。  实体类.GetList<实体类>(0)

  public static List<BetResultInfo> BetRInfo()
        {
            return BetResultInfo.GetList<BetResultInfo>(0);
        }

 

  5、更新数据表字段的值: 实体类.Update(实体的实例对象,"需要修改的字段值")

  public static int Update(W88BetRecords item)
        {
            return W88BetRecords.Update(item, "Id=" + item.Id.Value + "and TerraceId=2");
        }

  6、查询出单个的值,值是个Object类型:实体类.ExecSQLObj("数据库名","完整的Sql语句",new Object[]{});

              /// <summary>
        /// 根据等级ID查询限注值范围
        /// </summary>
        /// <param name="gradeID"></param>
        /// <returns></returns>
        public static string GetRegardByID(int gradeID)
        {
            string sql = "select LimitValue from Regard where GradeID=" + gradeID;
            return Convert.ToString(Regard.ExecSQLObj("Casino", sql, new object[] { }));
        }

 

 

 

 

ORMBase对象/关系型数据库映射在MVC中的应用(二),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!