当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net 通用数据操作类 (附通用数据基类)第1/2页

Asp.Net 通用数据操作类 (附通用数据基类)第1/2页

2018年04月21日  | 移动技术网IT编程  | 我要评论

双福语音浏览器,股票交易费,郭德纲最新相声


        /// <summary>
        /// 执行存储过程(返回记录集)
        /// </summary>
        /// <param name="procname">过程名</param>
        /// <param name="hashtable">传入的参数表</param>
        /// <returns>返回记录集</returns>
        public dataset execprocedure(string procname, system.collections.hashtable hashtable)
        {
            system.data.dataset ds = new dataset();
            com.commandtext = procname;
            com.commandtype = commandtype.storedprocedure;

            system.collections.idictionaryenumerator ide = hashtable.getenumerator();

            while (ide.movenext())
            {
                system.data.sqlclient.sqlparameter p = new system.data.sqlclient.sqlparameter(ide.key.tostring(), ide.value);
                com.parameters.add(p);
            }
            try
            {
                system.data.sqlclient.sqldataadapter da = new system.data.sqlclient.sqldataadapter(com);
                da.fill(ds);
                da.dispose();
            }
            catch (exception ee)
            {
                throw new exception(ee.message.tostring());
            }
            finally
            {
                com.cancel();
            }
            return ds;
        }
        #endregion

        #region 数据操作
        /// <summary>
        /// 统计某表记录总数
        /// </summary>
        /// <param name="keyfield">主键/索引键</param>
        /// <param name="tablename">数据库.用户名.表名</param>
        /// <param name="condition">查询条件</param>
        /// <returns>返回记录总数</returns>
        public int getrecordcount(string keyfield, string tablename, string condition)
        {
            int recordcount = 0;
            string sql = "select count(" + keyfield + ") as count from " + tablename + " " + condition;
            system.data.dataset ds = getdataset(sql);
            if (ds.tables[0].rows.count > 0)
            {
                recordcount =convert.toint32(ds.tables[0].rows[0][0]);
            }
            ds.clear();
            ds.dispose();
            return recordcount;
        }
        /// <summary>
        /// 统计某表记录总数
        /// </summary>
        /// <param name="field">可重复的字段</param>
        /// <param name="tablename">数据库.用户名.表名</param>
        /// <param name="condition">查询条件</param>
        /// <param name="flag">字段是否主键</param>
        /// <returns>返回记录总数</returns>
        public int getrecordcount(string field, string tablename, string condition, bool flag)
        {
            int recordcount = 0;
            if (flag)
            {
                recordcount = getrecordcount(field, tablename, condition);
            }
            else
            {
                string sql = "select count(distinct(" + field + ")) as count from " + tablename + " " + condition;
                system.data.dataset ds = getdataset(sql);
                if (ds.tables[0].rows.count > 0)
                {
                    recordcount = convert.toint32(ds.tables[0].rows[0][0]);
                }
                ds.clear();
                ds.dispose();
            }
            return recordcount;
        }
        /// <summary>
        /// 统计某表分页总数
        /// </summary>
        /// <param name="keyfield">主键/索引键</param>
        /// <param name="tablename">表名</param>
        /// <param name="condition">查询条件</param>
        /// <param name="pagesize">页宽</param>
        /// <param name="recordcount">记录总数</param>
        /// <returns>返回分页总数</returns>
        public int getpagecount(string keyfield, string tablename, string condition, int pagesize, int recordcount)
        {
            int pagecount = 0;
            pagecount = (recordcount % pagesize) > 0 ? (recordcount / pagesize) + 1 : recordcount / pagesize;
            if (pagecount < 1) pagecount = 1;
            return pagecount;
        }
        /// <summary>
        /// 统计某表分页总数
        /// </summary>
        /// <param name="keyfield">主键/索引键</param>
        /// <param name="tablename">表名</param>
        /// <param name="condition">查询条件</param>
        /// <param name="pagesize">页宽</param>
        /// <returns>返回页面总数</returns>
        public int getpagecount(string keyfield, string tablename, string condition, int pagesize, ref int recordcount)
        {
            recordcount = getrecordcount(keyfield, tablename, condition);
            return getpagecount(keyfield, tablename, condition, pagesize, recordcount);
        }
        /// <summary>
        /// 统计某表分页总数
        /// </summary>
        /// <param name="field">可重复的字段</param>
        /// <param name="tablename">表名</param>
        /// <param name="condition">查询条件</param>
        /// <param name="pagesize">页宽</param>
        /// <param name="flag">是否主键</param>
        /// <returns>返回页页总数</returns>
        public int getpagecount(string field, string tablename, string condition, ref int recordcount, int pagesize, bool flag)
        {
            recordcount = getrecordcount(field, tablename, condition, flag);
            return getpagecount(field, tablename, condition, pagesize, ref recordcount);
        }
        #endregion

        #region 分页函数
         /// <summary>
        /// 构造分页查询sql语句
        /// </summary>
        /// <param name="keyfield">主键</param>
        /// <param name="fieldstr">所有需要查询的字段(field1,field2...)</param>
        /// <param name="tablename">库名.拥有者.表名</param>
        /// <param name="condition">查询条件1(where ...)</param>
        /// <param name="condition2">查询条件2(order by ...)</param>
        /// <param name="currentpage">当前页号</param>
        /// <param name="pagesize">页宽</param>
        /// <returns>sql语句</returns>
        public static string joinpagesql(string keyfield, string fieldstr, string tablename, string condition, string condition2, int currentpage, int pagesize)
        {
            string sql = null;
            if (currentpage == 1)
            {
                sql = "select top " + currentpage * pagesize + " " + fieldstr + " from " + tablename + " " + condition + " " + condition2 + " ";
            }
            else
            {
                sql = "select * from (";
                sql += "select top " + currentpage * pagesize + " " + fieldstr + " from " + tablename + " " + condition + " " + condition2 + ") a ";
                sql += "where " + keyfield + " not in (";
                sql += "select top " + (currentpage - 1) * pagesize + " " + keyfield + " from " + tablename + " " + condition + " " + condition2 + ")";
            }
            return sql;
        }
        /// <summary>
        /// 构造分页查询sql语句
        /// </summary>
        /// <param name="field">字段名(非主键)</param>
        /// <param name="tablename">库名.拥有者.表名</param>
        /// <param name="condition">查询条件1(where ...)</param>
        /// <param name="condition2">查询条件2(order by ...)</param>
        /// <param name="currentpage">当前页号</param>
        /// <param name="pagesize">页宽</param>
        /// <returns>sql语句</returns>
        public static string joinpagesql(string field, string tablename, string condition, string condition2, int currentpage, int pagesize)
        {
            string sql = null;
            if (currentpage == 1)
            {
                sql = "select top " + currentpage * pagesize + " " + field + " from " + tablename + " " + condition + " " + condition2 + " group by " + field;
            }
            else
            {
                sql = "select * from (";
                sql += "select top " + currentpage * pagesize + " " + field + " from " + tablename + " " + condition + " " + condition2 + " group by " + field + " ) a ";
                sql += "where " + field + " not in (";
                sql += "select top " + (currentpage - 1) * pagesize + " " + field + " from " + tablename + " " + condition + " " + condition2 + " group by " + field + ")";
            }
            return sql;
        }
        /// <summary>
        /// 页面分页显示功能
        /// </summary>
        /// <param name="parameters">参数串(a=1&b=2...)</param>
        /// <param name="recordcount">记录总数</param>
        /// <param name="pagesize">页宽</param>
        /// <param name="currentpage">当前页号</param>
        /// <param name="showjump">是否显示跳转输入框及按钮</param>
        /// <param name="style">样式(1:上页下页...,2:1234...)</param>
        /// <returns></returns>
        public static string paging(string parameters, int recordcount, int pagecount, int pagesize, int currentpage, bool showjump, int style)
        {
            string str;
            if (recordcount <= pagesize) return "";
            if (parameters != "") parameters += "&";
            if (currentpage < 1) currentpage = 1;
            if (currentpage > pagecount) currentpage = pagecount;

            str = "<table align='center'  width=\"100%\"><tr><td align=\"center\">";

            str += "共 " + recordcount + " 条记录 页次:" + currentpage + "/" + pagecount + "页 ";
            str += pagesize + "条/页 ";

            if (style == 1)
            {
                if (currentpage == 1)
                    str += "<font color=\"#999999\">首页 上页</font> ";
                else
                {
                    str += "<a href='?" + parameters + "page=1'  class=\"link\">首页</a> ";
                    str += "<a href='?" + parameters + "page=" + (currentpage - 1) + "'  class=\"link\">上页</a> "; ;
                }
                if (currentpage == pagecount )
                {
                    str += "<font color=\"#999999\">下页 尾页</font> ";
                }
                else
                {
                    str += "<a href='?" + parameters + "page=" + (currentpage + 1) + "'  class=\"link\">下页</a> ";
                    str += "<a href='?" + parameters + "page=" + pagecount + "'  class=\"link\">尾页</a>  ";
                }
            }
            else if (style == 2)
            {
                int numbersize = 10;
                int pagenumber = (currentpage - 1) / numbersize;

                if (pagenumber * numbersize > 0)
                    str += "<a href='?" + parameters + "page=" + pagenumber * numbersize + "' title=上十页 >[<<]</a>   ";
                int i;
                for (i = pagenumber * numbersize + 1; i <= (pagenumber + 1) * numbersize; i++)
                {
                    if (i == currentpage)
                        str += "<strong><font color=#ff0000>[" + i + "]</font></strong> ";
                    else
                        str += "<a href='?" + parameters + "page=" + i + "'>[" + i + "]</a> ";
                    if (i == pagecount) break;
                }
                if (i < recordcount) str += "<a href='?" + parameters + "page=" + i + "' title=下十页>[>>]</a>  ";
            }
            if (showjump)
            {
                str += "";
            }
            str += "</td></tr></table>";
            return str;
        }

        #endregion
    }
}
2

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网