FineUI 官方论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

本论坛已关闭(禁止注册、发帖和回复)
请移步 三石和他的朋友们

FineUI首页 WebForms - MVC & Core - JavaScript 常见问题 - QQ群 - 十周年征文活动

FineUI(开源版) 下载源代码 - 下载空项目 - 获取ExtJS - 文档 在线示例 - 版本更新 - 捐赠作者 - 教程

升级到 ASP.NET Core 3.1,快、快、快! 全新ASP.NET Core,比WebForms还简单! 欢迎加入【三石和他的朋友们】(基础版下载)

搜索
查看: 2502|回复: 3
打印 上一主题 下一主题

数据库分页的问题

[复制链接]
跳转到指定楼层
楼主
发表于 2017-5-22 22:42:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
grid数据库分页时,  我怎么找不到 没法调用 DateSourceUtil?提示上下文中不存在 DateSourceUtil

private int GetTotalCount()
        {
            return DateSourceUtil.GetDataTable2().Rows.Count;
        }

沙发
发表于 2017-6-1 17:09:42 | 只看该作者
public ActionResult Index()
        {
            
            var pagingInfo = new PagingInfoViewModel
            {
                SortField = "ID",
                SortDirection = "DESC",
                PageIndex = 0,
                PageSize = ConfigHelper.PageSize
            };
            ViewBag.PagingInfo = pagingInfo;

            var list = AA.GetDataList(pagingInfo, string.Empty);

            return View(list);
        }
  var dt = DBHelper.GetPagedDataTable(pagingInfo, sql);

            return ConvertUtil<AA>.ConvertToModel(dt);

自己写了一个方法,估计高版本有现成的分页 方法吧?
/// <summary>
        /// 数据库分页
        /// </summary>
        /// <returns></returns>
        public static DataTable GetPagedDataTable(PagingInfoViewModel pagingInfo, string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                int pageIndex = pagingInfo.PageIndex;
                int pageSize = pagingInfo.PageSize;

                string sqlCount = string.Format("select count(0) from ({0}) a ", sql);
                pagingInfo.RecordCount = Convert.ToInt32(DBHelper.ExecuteScalar(sqlCount));

                Int16 totalRecord = Convert.ToInt16(pagingInfo.RecordCount);
                int rowStart = (pageIndex * pageSize) + 1;//当前页的起始序号
                int rowEnd = (pageIndex + 1) * pageSize;//当前页的结束序号
                rowEnd = rowEnd > totalRecord ? totalRecord : rowEnd;

                //排序
                if (!string.IsNullOrEmpty(pagingInfo.SortField))
                {
                    if (string.IsNullOrEmpty(pagingInfo.SortDirection)) pagingInfo.SortDirection = "ASC";
                    sql += " ORDER BY " + pagingInfo.SortField + " " + pagingInfo.SortDirection;
                }

                //查询语句
                sql = @"SELECT * FROM (
                            SELECT A.*, ROWNUM RN
                            FROM(" + sql + @") A
                            WHERE ROWNUM <= " + rowEnd.ToString() + @"
                            )  WHERE RN >= " + rowStart.ToString();

                dt = DBHelper.GetDataFromDB(sql);
                //DataTable source = DBHelper.ExecuteDataTable(sql);
            }
            catch(Exception ex)
            {
                LogClass.WriteLog(ex, sql);
            }
            return dt;
        }
板凳
发表于 2017-6-1 17:10:20 | 只看该作者
/// <summary>
        /// 将DataTable转换为List实体类(模型)列表
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <returns></returns>
        public static List<T> ConvertToModel(DataTable dt)
        {

            // 定义集合   
            List<T> ts = new List<T>();

            // 获得此模型的类型   
            Type type = typeof(T);
            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                // 获得此模型的公共属性      
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列   

                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;

                        object value = dr[tempName];
                        if (value != DBNull.Value)
                        {
                            try
                            {
                                pi.SetValue(t, value, null);
                            }
                            catch (Exception ex)
                            {
                                LogClass.WriteLog(ex, "数据库字段类型异常,请检查:" + tempName);
                            }
                        }
                    }
                }
                ts.Add(t);
            }

            return ts;
        }
地板
发表于 2017-6-1 21:59:20 | 只看该作者
在VS中搜索官网示例源代码,你肯定可以找到: DateSourceUtil
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|FineUI 官方论坛 ( 皖ICP备2021006167号-1 )

GMT+8, 2024-4-20 19:57 , Processed in 0.047898 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表