C#中sqlbase的帮助类,使用此类可以在逻辑处理层,调用帮助类,帮助类访问数据库。也可以在dal层使用直接调用帮助类,帮助类型中主要是sql语法

1 先创建sql帮助类

   public class SQLBase
    {

        #region MD5加密
        public string MD5JM(string str)
        {
            byte[] result = Encoding.Default.GetBytes(str);    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            string md5_pwd = BitConverter.ToString(output).Replace("-", "");  //
            return md5_pwd;
        }

        #endregion



        #region 数据库底层查询方法
        #region DataTable转List
        private List<T> TableToEntity<T>(DataTable dt) where T : class,new()
        {
            // 定义集合
            List<T> ts = new List<T>();

            // 获得此模型的类型
            Type type = typeof(T);
            //定义一个临时变量
            string tempName = string.Empty;
            //遍历DataTable中所有的数据行
            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)
                            pi.SetValue(t, value, null);
                    }
                }
                //对象添加到泛型集合中
                ts.Add(t);
            }

            return ts;

        }
        #endregion
        #region 通用插入方法
        public int InsertModel<T>(T t,string  TableName) where T : class,new()
        {
            PropertyInfo[] propertys = t.GetType().GetProperties();
            //遍历该对象的所有属性
            string tempName = string.Empty;
           // string class_name = t.GetType().Name;
            string _sql = "insert into [" + TableName + "] ";
            string _sql_1 = "";
            string _sql_2 = "";
            int _numid = 0;

            //再用Type.GetProperties获得PropertyInfo[]
            foreach (PropertyInfo pi in propertys)
            {
                if (_numid == 0)
                {
                    _numid++;
                    continue;
                }
                object name = pi.Name;//用pi.GetValue获得值
                // 替换Sql注入符
                string value1 = Convert.ToString(pi.GetValue(t, null)).Replace("'", "''");
                //string dataType = pi.PropertyType.ToString().ToLower();
                string properName = name.ToString().ToLower();
                if (!string.IsNullOrEmpty(value1) && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
                {
                    // 判断是否为空
                    if (value1 == BaseSet.NULL)
                    {
                        value1 = "";
                    }
                    _sql_1 += Convert.ToString(name) + ",";
                    if (pi.ToString().Contains("Int32") || pi.ToString().Contains("Int64"))
                    {
                        _sql_2 += value1 + ",";
                    }
                    else if (pi.ToString().Contains("Boolean"))
                    {
                        if(value1== "True" || value1 == "true")
                        {
                            _sql_2 += "1,";
                        }
                        else 
                        {
                            _sql_2 += "0,";
                        }
                    }
                    else
                    {
                        _sql_2 += "'" + value1 + "',";
                    }

                }
            }


            if (_sql_1.Length > 0)
            {
                _sql_1 = _sql_1.Substring(0, _sql_1.Length - 1);
            } if (_sql_2.Length > 0)
            {
                _sql_2 = _sql_2.Substring(0, _sql_2.Length - 1);
            }
            string _sqlover = _sql + " (" + _sql_1 + ") values(" + _sql_2 + ")";

            return DBExecuteSql(_sqlover);
        }
        #endregion
        #region 通用插入方法 返回主键
        public object GetIDInsertModel<T>(T t,string TableName) where T : class,new()
        {
            PropertyInfo[] propertys = t.GetType().GetProperties();
            //遍历该对象的所有属性
            string tempName = string.Empty;
            //string class_name = t.GetType().Name;
            string _sql = "insert into [" + TableName + "] ";
            string _sql_1 = "";
            string _sql_2 = "";
            int _numid = 0;

            //再用Type.GetProperties获得PropertyInfo[]
            foreach (PropertyInfo pi in propertys)
            {
                if (_numid == 0)
                {
                    _numid++;
                    continue;
                }
                object name = pi.Name;//用pi.GetValue获得值
                // 替换Sql注入符
                string value1 = Convert.ToString(pi.GetValue(t, null)).Replace("'", "''");
                //string dataType = pi.PropertyType.ToString().ToLower();
                string properName = name.ToString().ToLower();
                if (!string.IsNullOrEmpty(value1) && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
                {
                    // 判断是否为空
                    if (value1 == BaseSet.NULL)
                    {
                        value1 = "";
                    }
                    _sql_1 += Convert.ToString(name) + ",";
                    if (pi.ToString().Contains("Int32") || pi.ToString().Contains("Int64"))
                    {
                        _sql_2 += value1 + ",";
                    }
                    else if (pi.ToString().Contains("Boolean"))
                    {
                        if (value1 == "True")
                        {
                            _sql_2 += "1,";
                        }
                        else
                        {
                            _sql_2 += "0,";
                        }
                    }
                    else
                    {
                        _sql_2 += "'" + value1 + "',";
                    }

                }
            }


            if (_sql_1.Length > 0)
            {
                _sql_1 = _sql_1.Substring(0, _sql_1.Length - 1);
            } if (_sql_2.Length > 0)
            {
                _sql_2 = _sql_2.Substring(0, _sql_2.Length - 1);
            }
            string _sqlover = _sql + " (" + _sql_1 + ") values(" + _sql_2 + ") SELECT @@identity";
            return DbHelperSQL.GetSingle(_sqlover);


        }
        #endregion
        #region 通用更新方法(指定字段更新)
        /// <summary>
        /// 批量更新(更新list)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="TableName"></param>
        /// <param name="listcol"></param>
        /// <returns></returns>
        public int UpdateModel<T>(T t,string  TableName, List<string> listcol) where T : class,new()
        {
            PropertyInfo[] propertys = t.GetType().GetProperties();
            //遍历该对象的所有属性
            string tempName = string.Empty;
            //string class_name = t.GetType().Name;
            string _sql = "update  " + TableName + " set ";
            string _sql_1 = "";//拼接更新字段信息
            string _sql_2 = "";//默认拼接查询条件 按第一个的ID
            int _numid = 0;

            //再用Type.GetProperties获得PropertyInfo[]
            foreach (PropertyInfo pi in propertys)
            {

                object name = pi.Name;//用pi.GetValue获得值
                // 替换Sql注入符
                string value1 = Convert.ToString(pi.GetValue(t, null)).Replace("'", "''");
                //string dataType = pi.PropertyType.ToString().ToLower();
                string properName = name.ToString().ToLower();
                if (_numid == 0)
                {
                    _numid++;
                    //拼接where 条件
                    _sql_2 = " where " + Convert.ToString(name) + "=" + value1;

                    continue;
                }
                if (!string.IsNullOrEmpty(value1) && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
                {

                    // 判断是否为空
                    if (value1 == BaseSet.NULL)
                    {
                        value1 = "";
                    }
                    if (listcol.Any(l => l == Convert.ToString(name)))
                    {
                        if (pi.ToString().Contains("Int32") || pi.ToString().Contains("Int64"))
                        {
                            _sql_1 += Convert.ToString(name) + "=" + value1 + ",";
                        }
                        else
                        {
                            _sql_1 += Convert.ToString(name) + "='" + value1 + "',";
                        }
                    }

                }
            }
            if (_sql_1.Length > 0)
            {
                _sql_1 = _sql_1.Substring(0, _sql_1.Length - 1);
            }
            string _sqlover = _sql + _sql_1 + _sql_2;

            return DBExecuteSql(_sqlover);

        }
        /// <summary>
        /// 批量更新2(更新单个实体-更新指定字段)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public int UpdateModel<T>(T t, string TableName) where T : class, new()
        {
            PropertyInfo[] propertys = t.GetType().GetProperties();
            //遍历该对象的所有属性
            string tempName = string.Empty;
            //string class_name = t.GetType().Name;
            string _sql = "update  " + TableName + " set ";
            string _sql_1 = "";//拼接更新字段信息
            string _sql_2 = "";//默认拼接查询条件 按第一个的ID
            int _numid = 0;

            //再用Type.GetProperties获得PropertyInfo[]
            foreach (PropertyInfo pi in propertys)
            {

                object name = pi.Name;//用pi.GetValue获得值
                //替换Sql注入符
                string value1 = Convert.ToString(pi.GetValue(t, null)).Replace("'", "''");
                string dataType = pi.PropertyType.ToString().ToLower();
                string properName = name.ToString().ToLower();
                if (_numid == 0)
                {
                    _numid++;
                    //拼接where 条件
                    _sql_2 = " where " + Convert.ToString(name) + "=" + value1;

                    continue;
                }
                if (!string.IsNullOrEmpty(value1) && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
                {

                    //判断是否为空
                    if (value1 == BaseSet.NULL)
                    {
                        value1 = "";
                    }
                    if (pi.ToString().Contains("Int32") || pi.ToString().Contains("Int64"))
                    {
                        _sql_1 += Convert.ToString(name) + "=" + value1 + ",";
                    }
                    else if (pi.ToString().Contains("Boolean"))
                    {
                        if (value1 == "True")
                        {
                            _sql_1 += Convert.ToString(name) + "=1,";
                        }
                        else
                        {
                            _sql_1 += Convert.ToString(name) + "=0,";
                        }
                    }
                    else
                    {
                        _sql_1 += Convert.ToString(name) + "='" + value1 + "',";
                    }
                }
            }
            if (_sql_1.Length > 0)
            {
                _sql_1 = _sql_1.Substring(0, _sql_1.Length - 1);
            }
            string _sqlover = _sql + _sql_1 + _sql_2;

            return DBExecuteSql(_sqlover);

        }
        /// <summary>
        /// 通用更新(单个对象更新-更新整个实体)
        /// </summary>
        /// <typeparam name="T">模型</typeparam>
        /// <param name="t">更新对象值</param>
        /// <returns></returns>
        public int Update_Model<T>(T t, string TableName) where T : class,new()
        {
            PropertyInfo[] propertys = t.GetType().GetProperties();
            //遍历该对象的所有属性
            string tempName = string.Empty;
            //string class_name = t.GetType().Name;
            string _sql = "update  [" + TableName + "] set ";
            string _sql_1 = "";//拼接更新字段信息
            string _sql_2 = "";//默认拼接查询条件 按第一个的ID
            int _numid = 0;

            //再用Type.GetProperties获得PropertyInfo[]
            foreach (PropertyInfo pi in propertys)
            {

                object name = pi.Name;//用pi.GetValue获得值
                // 替换Sql注入符
                string value1 = Convert.ToString(pi.GetValue(t, null)).Replace("'", "''");
                //string dataType = pi.PropertyType.ToString().ToLower();
                string properName = name.ToString().ToLower();
                if (_numid == 0)
                {
                    _numid++;
                    //拼接where 条件
                    _sql_2 = " where " + Convert.ToString(name) + "=" + value1;

                    continue;
                }
                if (!string.IsNullOrEmpty(value1) && properName != BaseSet.PrimaryKey.ToLower() && properName != BaseSet.TableName.ToLower() && value1 != BaseSet.DateTimeLongNull && value1 != BaseSet.DateTimeShortNull)
                {

                    // 判断是否为空
                    if (value1.ToString() == BaseSet.NULL)
                    {
                        value1 = "";
                    }
                    if (pi.ToString().Contains("Int32") || pi.ToString().Contains("Int64"))
                    {
                        if (value1.ToString() == "")
                        {
                            value1 = "null";
                        }
                        _sql_1 += Convert.ToString(name) + "=" + value1 + ",";
                    }
                    else if (pi.ToString().Contains("Boolean"))
                    {
                        if (value1 == "True")
                        {
                            _sql_1 += Convert.ToString(name) + "=1,";
                        }
                        else
                        {
                            _sql_1 += Convert.ToString(name) + "=0,";
                        }
                    }
                    else
                    {
                        _sql_1 += Convert.ToString(name) + "='" + value1 + "',";
                    }
                }
            }
            if (_sql_1.Length > 0)
            {
                _sql_1 = _sql_1.Substring(0, _sql_1.Length - 1);
            }
            string _sqlover = _sql + _sql_1 + _sql_2;

            return DBExecuteSql(_sqlover);

        }
        #endregion

        #region sql 执行
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql">sql</param>
        /// <returns></returns>
        public int DBExecuteSql(string sql)
        {
            return DbHelperSQL.ExecuteSql(sql);
        }

        public object DBExecuteSql2(string sql)
        {
            return DbHelperSQL.GetSingle(sql);
        }

        #endregion
        #region 集合查询
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tablename"></param>
        /// <param name="ziduan"></param>
        /// <param name="where"></param>

        /// <returns></returns>
        public List<T> GetListModel<T>(string tablename, string ziduan, string where) where T : class,new()
        {
            List<T> ListModel = new List<T>();

            StringBuilder strSql = new StringBuilder();
            if (ziduan == "")
            {
                ziduan = "*";
            }
            strSql.Append("select " + ziduan + " ");
            strSql.Append(" FROM " + tablename + " ");
            if (where.Trim() != "")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds = null;

            ds = DbHelperSQL.Query(strSql.ToString());

            if (ds != null)
            {
                ListModel = TableToEntity<T>(ds.Tables[0]);
            }
            return ListModel;
        }
        #endregion
        #region 获取单个对象
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tablename"></param>
        /// <param name="ziduan"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public T GetModel<T>(string tablename, string ziduan, string where) where T : class,new()
        {
            List<T> ListModel = new List<T>();
            StringBuilder strSql = new StringBuilder();
            if (ziduan == "")
            {
                ziduan = "*";
            }
            strSql.Append("select top(1)" + ziduan + " ");
            strSql.Append(" FROM " + tablename + " ");
            if (where.Trim() != "")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds = null;

            ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null)
            {
                ListModel = TableToEntity<T>(ds.Tables[0]);
            }

            if (ListModel != null && ListModel.Count > 0)
            {
                return ListModel[0];
            }
            else
            {
                return null;
            }
        }

        #endregion
        #region 分页查询
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="tablename">表名</param>
        /// <param name="ziduan">查询字段</param>
        /// <param name="strWhere"></param>
        /// <param name="orderby">排序</param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>

        /// <returns></returns>
        public List<T> GetListPageWhere<T>(string tablename, string ziduan, string strWhere, string orderby, int startIndex, int endIndex) where T : class,new()
        {
            List<T> ListModel = new List<T>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT " + ziduan + " FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()) && !orderby.Contains("CHARINDEX"))
            {
                strSql.Append("order by T." + orderby);
            }
            else if (!string.IsNullOrEmpty(orderby.Trim()) && orderby.Contains("CHARINDEX"))
            {
                strSql.Append("order by " + orderby);// CHARINDEX('河北废金属回收',T.gq_title) asc
            }
            else
            {
                // strSql.Append("order by T.gq_id desc");
            }
            strSql.Append(")AS Row, T.*  from " + tablename + " T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = null;

            ds = DbHelperSQL.Query(strSql.ToString());

            if (ds != null)
            {
                ListModel = TableToEntity<T>(ds.Tables[0]);
            }

            return ListModel;
        }

        public List<T> GetListPageWhere<T>(out int total, string tablename, string ziduan, string strWhere, string orderby, int startIndex, int endIndex) where T : class,new()
        {
            List<T> ListModel = new List<T>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT " + ziduan + " FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()) && !orderby.Contains("CHARINDEX"))
            {
                strSql.Append("order by T." + orderby);
            }
            else if (!string.IsNullOrEmpty(orderby.Trim()) && orderby.Contains("CHARINDEX"))
            {
                strSql.Append("order by " + orderby);
            }

            strSql.Append(")AS Row, T.*  from " + tablename + " T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE 1=1 " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = null;

            ds = DbHelperSQL.Query(strSql.ToString());

            if (ds != null)
            {
                ListModel = TableToEntity<T>(ds.Tables[0]);
                total = GetListRecordCount(tablename, strWhere);
            }
            else
            {
                total = 0;
            }

            return ListModel;
        }


        /// <summary>
        /// 获取条件分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="total"></param>
        /// <param name="tablename"></param>
        /// <param name="ziduan"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderby"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns></returns>
        public List<T> GetListPageWhereTow<T>(out int total, string tablename, string ziduan, string strWhere, string orderby, int startIndex, int endIndex) where T : class, new()
        {
            List<T> ListModel = new List<T>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()) && !orderby.Contains("CHARINDEX"))
            {
                strSql.Append("order by T." + orderby);
            }
            else if (!string.IsNullOrEmpty(orderby.Trim()) && orderby.Contains("CHARINDEX"))
            {
                strSql.Append("order by T." + orderby);
            }

            strSql.Append(")AS Row, *  from ( select " + ziduan  + "  from  "+tablename+"  ");


            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE  1=1 " + strWhere);
            }

            strSql.Append(")T  ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = null;

            ds = DbHelperSQL.Query(strSql.ToString());

            if (ds != null)
            {
                ListModel = TableToEntity<T>(ds.Tables[0]);
                total = GetListRecordCount(tablename, strWhere);
            }
            else
            {
                total = 0;
            }

            return ListModel;
        }
        #endregion
        #region 获取查询总条数
        /// <summary>
        /// 获取查询总条数
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public int GetListRecordCount(string tablename, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM " + tablename + " ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where  1=1 " + strWhere);
            }
            object obj = null;

            obj = DbHelperSQL.GetSingle(strSql.ToString());


            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }


        }
        #endregion
        #endregion

        #region 获取自定义页码分页的算法
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pcount">总页数</param>
        /// <param name="index">当前页</param>
        /// <param name="num">显示几个页</param>
        /// <returns></returns>
        public List<int> GetScodPageParam(int pcount, int index, int num)
        {
            List<int> listp = new List<int>();
            listp.Add(1);//首页
            //上一页
            int temp = index - 1; if (temp == 0) { temp = 1; }
            listp.Add(temp);
            int page_count = num;
            if (pcount < num)//当设定页数超出了总页数
            {
                num = pcount;
                page_count = pcount;
            }
            int p_index = pcount - page_count;
            int f = 0;//设定页奇偶数区分变量
            if (num % 2 == 1)//判断奇偶数
            {
                f = 1;
            }


            for (int i = 1; i <= page_count; i++)
            {
                if (index <= (num / 2 + f))//当前页小于等于设定页的中间页 按1-num 排列
                {
                    listp.Add(i);
                }
                else if ((index > (num / 2 + f)))//当前页大于设定页的中间页 则累加滚动+i
                {
                    //公式: i+当前页-((设定页/2)+f)
                    if ((index + (num / 2 + f)) > pcount)//当前页即将超出总页数时 按最后页排列
                    {
                        listp.Add(i + pcount - 2 * (num / 2 + f) + f);//取最后一组分页的中间数:(总页数-(设定页/2+f)+f)
                    }
                    else
                    {
                        listp.Add(i + index - (num / 2 + f));
                    }
                }
            }

            temp = index + 1; if (temp > pcount) { temp = pcount; }
            //下一页
            listp.Add(temp);
            //尾页
            listp.Add(pcount);//
            return listp;
        }

        #endregion

    

        #region 集合转json 字符串
        private string ListToJsonString<T>(List<T> list) where T : class,new()
        {
            char[] specialChars = new char[] { ',' };
            string JSONstring = "[";
            T t = new T();
            int index = 0;
            foreach (var item in list)
            {

                JSONstring += "{";
                PropertyInfo[] propertys = item.GetType().GetProperties();
                foreach (var prop in propertys)
                {
                    JSONstring += "\"" + prop.Name + "\":\"" + prop.GetValue(item, null) + "\",";
                }
                JSONstring = JSONstring.TrimEnd(specialChars);
                JSONstring += "},";

                index++;
            }
            JSONstring = JSONstring.TrimEnd(specialChars);
            JSONstring += "]";

            return JSONstring;



        }
        #endregion

        #region 分页(过时)
        分页方法
        //public int FunPage2<T>(int? Page, string tablename, string ziduan, string strWhere, string orderBy) where T : class,new()
        //{
        //    int pageIndex = 1;//当前
        //    int _star = 0;
        //    List<int> listp = null;

        //    if (Page != null)
        //    {
        //        pageIndex = Page.Value;

        //    }
        //    int pageSize = 5;//每页数
        //    int count = GetListRecordCount(tablename, strWhere);
        //    int pcount = count / pageSize;//总页数
        //    if (count % pageSize > 0)
        //    {
        //        pcount++;
        //    }
        //    _star = (pageIndex - 1) * pageSize + 1;
        //    var links = GetListPageWhere<T>(tablename, ziduan, strWhere, orderBy, _star, _star + pageSize - 1);
        //    listp = GetScodPageParam(pcount, pageIndex, 5);//分页参数
        //    if (links.Count == 0 && Page > 1)
        //    {
        //        Page = Page - 1;
        //        Response.Redirect("?Page=" + Page);
        //    }
        //    ViewBag.count = count;//总数
        //    ViewBag.links = links;//集合
        //    ViewBag.listp = listp;
        //    ViewBag.Page = Page;//当前页
        //    return count;
        //}
        //public int gq_cf//获取刷新次数
        //{
        //    get { return Session["gq_cf"] == null ? 0 : Convert.ToInt32(Session["gq_cf"]); }
        //    set { Session["gq_cf"] = value; }
        //}
        #endregion

        #region 创建时间范围条件
        /// <summary>
        /// 创建时间范围条件
        /// </summary>
        public string CreateTimeWhereStr(string StartEndDate, string FieldNames, string _where, string prefix = null)
        {
            if (!string.IsNullOrEmpty(StartEndDate) && StartEndDate != " ~ ")
            {
                if (StartEndDate.Contains("~"))
                {
                    if (StartEndDate.Contains("+"))
                    {
                        StartEndDate = StartEndDate.Replace("+", "");
                    }
                    var dts = StartEndDate.Split('~');
                    var start = dts[0].Trim();
                    var end = dts[1].Trim();
                    if (!string.IsNullOrEmpty(start))
                    {
                        if (!string.IsNullOrEmpty(prefix))
                        {
                            _where += string.Format(" and {1}{2}>='{0} 00:00:00'", start, prefix, FieldNames);
                        }
                        else
                        {
                            _where += string.Format(" and {1}>='{0} 00:00:00'", start, FieldNames);
                        }
                    }
                    if (!string.IsNullOrEmpty(end))
                    {
                        if (!string.IsNullOrEmpty(prefix))
                        {
                            _where += string.Format(" and {1}{2}<='{0} 23:59:59'", end, prefix, FieldNames);
                        }
                        else
                        {
                            _where += string.Format(" and {1}<='{0} 23:59:59'", end, FieldNames);
                        }
                    }
                }
            }
            return _where;
        }
        #endregion

        #region 随机数
         <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="length">指定验证码的长度</param>
        /// <returns></returns>
        public string CreateValidateCode(int length)
        {
            int[] randMembers = new int[length];
            int[] validateNums = new int[length];
            string validateNumberStr = "";
            //生成起始序列值
            int seekSeek = unchecked((int)DateTime.Now.Ticks);
            Random seekRand = new Random(seekSeek);
            int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
            int[] seeks = new int[length];
            for (int i = 0; i < length; i++)
            {
                beginSeek += 10000;
                seeks[i] = beginSeek;
            }
            //生成随机数字
            for (int i = 0; i < length; i++)
            {
                Random rand = new Random(seeks[i]);
                int pownum = 1 * (int)Math.Pow(10, length);
                randMembers[i] = rand.Next(pownum, Int32.MaxValue);
            }
            //抽取随机数字
            for (int i = 0; i < length; i++)
            {
                string numStr = randMembers[i].ToString();
                int numLength = numStr.Length;
                Random rand = new Random();
                int numPosition = rand.Next(0, numLength - 1);
                validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
            }
            //生成验证码
            for (int i = 0; i < length; i++)
            {
                validateNumberStr += validateNums[i].ToString();
            }
            return validateNumberStr;
        }
        #endregion

        #region sql查询获取DataTable
        /// <summary>
        /// sql查询
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public DataTable GetDataTable(string sql)
        {
            return DbHelperSQL.Query(sql).Tables[0];
        }
        #endregion


        #region sql查询获取list<T>
        /// <summary>
        /// sql查询
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public List<T> GetListModel<T>(string sql) where T : class, new()
        {
            List<T> ListModel = new List<T>();
            DataSet ds = DbHelperSQL.Query(sql);
            if (ds != null)
            {
                ListModel = TableToEntity<T>(ds.Tables[0]);
            }
            return ListModel;
        }
        #endregion

        #region 开一个事务添加图集
        public int ExecuteImgTran(QX_Member_archives_APImodel model, ref int newid)
        {
            return DbHelperSQL.ExecuteImgTran(model, ref newid);
        }
        #endregion

        public int editImg(QX_Member_archives_APImodel model)
        {
            return DbHelperSQL.editImg(model);
        }

    }
    public class BaseSet
    {
        public static string NULL
        {
            get { return "@null"; }
        }


        public static string DateTimeShortNull
        {
            get { return "1970-01-01 0:00:00"; }
        }


        public static string DateTimeLongNull
        {
            get { return "1970-01-01 00:00:00"; }
        }


        public static string PrimaryKey
        {
            get { return "PrimaryKey"; }
        }


        public static string TableName
        {
            get { return "TableName"; }
        }


       
    }

 2 在逻辑处理层和dal层可以直接访问帮助类

猜你喜欢

转载自blog.csdn.net/zhang123csdn/article/details/129611954