1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066 |
- using System;
- using System.Data;
- using System.Text;
- using System.Data.SqlClient;
- using System.Collections.Generic;
- using LYFZ.Helper;
- namespace LYFZ.WeixinServiceDate.DAL
- {
- /// <summary>
- /// 数据操作抽象基类
- /// </summary>
- public abstract class BaseDataOperate : LYFZ.WeixinServiceDate.StandardInterface.IBaseDataLaye
- {
- #region 属性
- /// <summary>
- /// 数据表模型对象
- /// </summary>
- public abstract object ObjModel { get; }
- /// <summary>
- /// 数据表名
- /// </summary>
- public abstract string TableName { get; set; }
- /// <summary>
- /// 过期属性请不要使用 数据表字段名字符串,字段名以“,”号分隔
- /// </summary>
- public abstract string TableFieldNameString { get; set; }
- string _TableFieldNameStr = "";
- /// <summary>
- /// 数据表字段名字符串,字段名以“,”号分隔
- /// </summary>
- public string TableFieldNameStr
- {
- get
- {
- if (this._TableFieldNameStr.Trim() == "")
- {
- for (int i = 0; i < this.TableFieldNames.Length; i++)
- {
- if (this._TableFieldNameStr.Trim() == "")
- {
- this._TableFieldNameStr += "[" + this.TableFieldNames[i] + "]";
- }
- else
- {
- this._TableFieldNameStr += ",[" + this.TableFieldNames[i] + "]";
- }
- }
- }
- if (this._TableFieldNameStr.Trim() == "")
- {
- this._TableFieldNameStr = "*";
- }
- return this._TableFieldNameStr;
- }
- set { this._TableFieldNameStr = value; }
- }
- /// <summary>
- /// 数据表字段名数组
- /// </summary>
- public string[] TableFieldNames
- {
- get
- {
- System.Reflection.PropertyInfo[] propertyInfos = this.PropertyInfos;
- string[] fieldNames = new string[propertyInfos.Length];
- for (int i = 0; i < propertyInfos.Length; i++)
- {
- if (!propertyInfos[i].IsSpecialName)
- {
- System.Reflection.PropertyInfo propertyInfo = propertyInfos[i];
- fieldNames[i] = propertyInfo.Name;
- }
- }
- // System.Reflection.PropertyInfo propertyInfo = ObjModel.GetType().GetProperty(propertyInfos[0].Name);
- return fieldNames;//this.TableFieldNameString.Replace("[", "").Replace("]", "").Replace(" ", "").Split(',');
- }
- }
- /// <summary>
- /// 获取要查询的当前表的字段集合字符串 用","号分隔
- /// </summary>
- /// <param name="filters"></param>
- /// <param name="isContain">如果为true 表示只返回 filters 集合中的字段 如果为false 表示返回排除 filters 集合中的字段以外的所有字段</param>
- /// <returns></returns>
- public string GetQueryTableFieldNames(string[] filters = null, bool isContain = false)
- {
- List<string> tempFilters = new List<string>();
- if (filters != null)
- {
- tempFilters.AddRange(filters);
- }
- System.Reflection.PropertyInfo[] propertyInfos = this.PropertyInfos;
- string fieldNames = "";
- for (int i = 0; i < propertyInfos.Length; i++)
- {
- if (!propertyInfos[i].IsSpecialName)
- {
- System.Reflection.PropertyInfo propertyInfo = propertyInfos[i];
- if ((!tempFilters.Contains(propertyInfo.Name) && !isContain) || (tempFilters.Contains(propertyInfo.Name) && isContain))
- {
- if (fieldNames.Trim().Length <= 0)
- {
- fieldNames = "[" + propertyInfo.Name + "]";
- }
- else
- {
- fieldNames += ",[" + propertyInfo.Name + "]";
- }
- }
- }
- }
- return fieldNames;
- }
- /// <summary>
- /// 获取属性集合
- /// </summary>
- public System.Reflection.PropertyInfo[] PropertyInfos
- {
- get
- {
- List<System.Reflection.PropertyInfo> list = new List<System.Reflection.PropertyInfo>();
- System.Reflection.PropertyInfo[] infos = ObjModel.GetType().GetProperties();
- foreach (System.Reflection.PropertyInfo info in infos)
- {
- if (info.CanWrite)
- {
- list.Add(info);
- }
- }
- return list.ToArray();
- }
- }
- #endregion
- #region 检查记录
- /// <summary>
- /// 是否存在该记录
- /// </summary>
- public bool Exists(int id)
- {
- return this.Exists("ID", id);
- }
- /// <summary>
- /// 根据筛选条件判断是否存在该记录
- /// </summary>
- /// <param name="filterFieldName">筛选字段名</param>
- /// <param name="filterValue">筛选值</param>
- /// <returns></returns>
- public bool Exists(string filterFieldName, object filterValue)
- {
- if (filterFieldName != "" && filterValue != null)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("select count(1) from " + this.TableName + " ");
- strSql.Append(" where 1=1 and " + filterFieldName + "=@" + filterFieldName);
- SqlParameter[] parameters = {
- new SqlParameter("@"+filterFieldName, filterValue)
- };
- return SQLHelper.Exists(strSql.ToString(), parameters);
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 根据筛选条件判断是否存在该记录
- /// </summary>
- /// <param name="filterFieldName">筛选字段名</param>
- /// <param name="filterValue">筛选值</param>
- /// <returns></returns>
- public bool Exists(string whereString)
- {
- if (whereString.Trim().Length > 0)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("select count(1) from " + this.TableName + " ");
- strSql.Append(" where " + whereString);
- return SQLHelper.Exists(strSql.ToString());
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 判断是否存某个字段
- /// </summary>
- /// <param name="columnName">列名称</param>
- /// <returns>是否存在</returns>
- public bool ColumnExists(string columnName)
- {
- return SQLHelper.ColumnExists(this.TableName, columnName);
- }
- #endregion
- #region 公共方法
- /// <summary>
- /// 获取最大ID()
- /// </summary>
- /// <returns></returns>
- public int GetMaxID()
- {
- return this.GetMaxID("ID");
- }
- /// <summary>
- /// 获取某字段最大值(获取字段必须为数字类型)
- /// </summary>
- /// <param name="FieldName">字段名</param>
- /// <returns></returns>
- public int GetMaxID(string FieldName)
- {
- return SQLHelper.GetMaxID(FieldName, this.TableName);
- }
- /// <summary>
- /// 检查CLR功能是否开启
- /// </summary>
- /// <returns></returns>
- public static bool CheckingCLR()
- {
- try
- {
- DataSet cds = LYFZ.Helper.SQLHelper.Query("SELECT TOP 1 [Cus_Name] ,[Cus_Name_py],[Cus_Sex_cs] FROM [dbo].[Vw_AggregationCustomer]");
- return true;
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #region 判断方法
- /// <summary>
- /// 获取是否为忽略字段 返回 true 表示忽略 false 不忽略
- /// </summary>
- /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
- /// <param name="fieldName">要判断的字段名</param>
- /// <returns></returns>
- public bool IsOverlookFiel(string overlookFieldList, string fieldName)
- {
- bool bl = false;
- string[] tempOverlookFieldList = overlookFieldList.Split(',');
- for (int i = 0; i < tempOverlookFieldList.Length; i++)
- {
- if (tempOverlookFieldList[i].Trim().ToLower() == fieldName.Trim().ToLower())
- {
- bl = true;
- break;
- }
- }
- return bl;
- }
- #endregion
- #region 增加数据
- /// <summary>
- /// 增加一条数据
- /// </summary>
- /// <param name="model">Model对象</param>
- /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
- /// <returns></returns>
- public bool Add(object model, string overlookFieldList = "ID")
- {
-
- LYFZ.Helper.CommandInfo comdInfo = this.GetAddCommandInfo(model, overlookFieldList);
- int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
- if (rows > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 获取插入数CommandInfo对象
- /// </summary>
- /// <param name="model"></param>
- /// <param name="overlookFieldList">要忽略的字段集合 多个字段名有“,”号分隔</param>
- /// <returns></returns>
- public object GetAddCommandInfoObject(object model, string overlookFieldList = "ID")
- {
- return this.GetAddCommandInfo(model, overlookFieldList);
- }
- /// <summary>
- /// 获取插入数CommandInfo对象
- /// </summary>
- /// <param name="model"></param>
- /// <param name="overlookFieldList">要忽略的字段集合 多个字段名有“,”号分隔</param>
- /// <param name="type"></param>
- /// <returns></returns>
- public LYFZ.Helper.CommandInfo GetAddCommandInfo(object model, string overlookFieldList = "ID", EffentNextType type = EffentNextType.None)
- {
- StringBuilder strSql = new StringBuilder();
- StringBuilder tempFieldNameString = new StringBuilder();
- StringBuilder tempValueVariable = new StringBuilder();
- List<SqlParameter> parameterlist = new List<SqlParameter>();
- for (int i = 0; i < PropertyInfos.Length; i++)
- {
- try
- {
- System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
- string tempFieldName = propertyInfo.Name;
- object tempValue = propertyInfo.GetValue(model, null);
- Type tempType = propertyInfo.PropertyType;
- if (!IsOverlookFiel(overlookFieldList, tempFieldName) || (tempFieldName.ToLower() == "id" && tempType == typeof(long)))
- {
- try
- {
- if (tempType.Name == "DateTime")
- {
- if (tempValue.ToString().Contains("0001"))
- {
- tempValue = null;
- }
- try
- {
- DateTime tempTime;
- if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
- {
- tempValue = null;
- }
- else if (tempTime <= LYFZ.WinAPI.CustomPublicMethod.GetNullDateTime())
- {
- tempValue = null;
- }
- }
- catch { tempValue = null; }
- }
- else if (tempFieldName.ToLower() == "id" && tempType == typeof(long) && (tempValue == null || Convert.ToInt64(tempValue) == 0))
- {
- tempValue = LYFZ.WinAPI.CustomPublicMethod.GenerateId();
- }
- if (tempValue != null)
- {
- tempFieldNameString.Append("" + tempFieldName + ",");
- tempValueVariable.Append("@" + tempFieldName + ",");
- SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
- parameterlist.Add(parameter);
- }
- }
- catch { }
- }
- }
- catch { }
- }
- //去除结尾多余的“,”
- if (tempFieldNameString.ToString().LastIndexOf(',') == tempFieldNameString.Length - 1)
- {
- tempFieldNameString.Remove(tempFieldNameString.Length - 1, 1);
- }
- //去除结尾多余的“,”
- if (tempValueVariable.ToString().LastIndexOf(',') == tempValueVariable.Length - 1)
- {
- tempValueVariable.Remove(tempValueVariable.Length - 1, 1);
- }
- strSql.Append("insert into " + this.TableName + "(");
- strSql.Append("" + tempFieldNameString.ToString() + ")");
- strSql.Append(" values (");
- strSql.Append("" + tempValueVariable.ToString() + ")");
- strSql.Append(";select @@IDENTITY");
- SqlParameter[] parameters = parameterlist.ToArray();
- return new CommandInfo(strSql.ToString(), parameters, type);
- }
- #endregion
- #region 更新数据
- /// <summary>
- /// 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
- /// </summary>
- /// <param name="model">Model对象</param>
- /// <param name="filterFieldName">筛选字段名称</param>
- /// <param name="operators">SQL筛选运算符号</param>
- /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
- /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
- /// <returns></returns>
- protected bool InsideUpdate(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
- {
-
- LYFZ.Helper.CommandInfo comdInfo = GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
- int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
- if (rows > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
- /// </summary>
- /// <param name="model">Model对象</param>
- /// <param name="filterFieldName">筛选字段名称</param>
- /// <param name="operators">SQL筛选运算符号</param>
- /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
- /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
- public object GetUpdateCommandInfoObject(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
- {
- return this.GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
- }
- /// <summary>
- /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
- /// </summary>
- /// <param name="model">Model对象</param>
- /// <param name="filterFieldName">筛选字段名称</param>
- /// <param name="operators">SQL筛选运算符号</param>
- /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
- /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
- /// <param name="type"></param>
- public LYFZ.Helper.CommandInfo GetUpdateCommandInfo(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null, EffentNextType type = EffentNextType.None)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("update " + this.TableName + " set ");
- List<SqlParameter> parameterlist = new List<SqlParameter>();
- overlookFieldList += "," + filterFieldName;
- for (int i = 0; i < PropertyInfos.Length; i++)
- {
- try
- {
- System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
- string tempFieldName = propertyInfo.Name;//字段名
- object tempValue = propertyInfo.GetValue(model, null);//对应字段值
- //如果字段名不为忽略字段则进行处理
- if (!IsOverlookFiel(overlookFieldList, tempFieldName))
- {
- try
- {
- if (tempValue.GetType() == typeof(DateTime))
- {
- DateTime tempTime;
- try
- {
- if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
- {
- tempValue = null;
- }
- else if (tempTime <= LYFZ.WinAPI.CustomPublicMethod.GetNullDateTime())
- {
- tempValue = null;
- }
- else
- if (tempTime.Year == Convert.ToDateTime("0001-01-01").Year || tempTime.Year == Convert.ToDateTime("1753-01-01").Year)
- {
- tempValue = null;
- }
- }
- catch { tempValue = null; }
- }
- }
- catch { }
- strSql.Append("" + tempFieldName + "=@" + tempFieldName + ",");
- SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
- parameterlist.Add(parameter);
- }
- }
- catch { }
- }
- //去除结尾多余的“,”
- if (strSql.ToString().LastIndexOf(',') == strSql.Length - 1)
- {
- strSql.Remove(strSql.Length - 1, 1);
- }
- if (whereStr == null)
- {
- if (filterFieldName != "" && operators != "")
- {
- try
- {
- object FilterFieldValue = LYFZ.WinAPI.CustomPublicMethod.GetPropertyValue(model, filterFieldName);// model.GetType().GetProperty(filterFieldName).GetValue(model, null);
- strSql.Append(" where " + filterFieldName + operators + "@" + filterFieldName);
- parameterlist.Add(new SqlParameter("@" + filterFieldName, FilterFieldValue));
- }
- catch
- {
- }
- }
- }
- else
- {
- strSql.Append(" where 1=1 and (" + whereStr + ")");
- }
- SqlParameter[] parameters = parameterlist.ToArray();
- return new CommandInfo(strSql.ToString(), parameters, type);
- }
- /// <summary>
- /// 根据筛选字段和SQL筛选运算符号更新数据
- /// </summary>
- /// <param name="model">Model对象</param>
- /// <param name="filterFieldName">筛选字段名称</param>
- /// <param name="operators">SQL筛选运算符号</param>
- /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
- /// <returns></returns>
- public bool Update(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID")
- {
- return InsideUpdate(model, filterFieldName, operators, overlookFieldList);
- }
- /// <summary>
- /// 根据Wher条件更新数据 不建义使用此方法
- /// </summary>
- /// <param name="model">Model对象</param>
- /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
- /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
- /// <returns></returns>
- public bool Update(object model, string whereStr, string overlookFieldList = "ID")
- {
- return InsideUpdate(model, overlookFieldList: overlookFieldList, whereStr: whereStr);
- }
- #endregion
- #region 删除数据
- /// <summary>
- /// 根据筛选字段或where条件删除数据
- /// </summary>
- /// <param name="filterFieldName">筛选字段名</param>
- /// <param name="operators">筛选SQL运算符</param>
- /// <param name="filterValue">筛选值</param>
- /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
- /// <returns></returns>
- protected bool InsideDelete(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
- {
-
- LYFZ.Helper.CommandInfo comdInfo = GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
- int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
- if (rows > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 根据筛选字段或where条件删除数据
- /// </summary>
- /// <param name="filterFieldName">筛选字段名</param>
- /// <param name="operators">筛选SQL运算符</param>
- /// <param name="filterValue">筛选值</param>
- /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
- /// <returns></returns>
- public object GetDeleteCommandInfoObject(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
- {
- return this.GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
- }
- /// <summary>
- /// 根据筛选字段或where条件删除数据
- /// </summary>
- /// <param name="filterFieldName">筛选字段名</param>
- /// <param name="operators">筛选SQL运算符</param>
- /// <param name="filterValue">筛选值</param>
- /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
- /// <param name="type"></param>
- /// <returns></returns>
- public LYFZ.Helper.CommandInfo GetDeleteCommandInfo(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null, EffentNextType type = EffentNextType.None)
- {
- if (filterValue == null) { filterValue = ""; }
- List<SqlParameter> parameterlist = new List<SqlParameter>();
- StringBuilder strSql = new StringBuilder();
- strSql.Append("delete from " + this.TableName + " ");
- if (whereStr == null)
- {
- strSql.Append(" where " + filterFieldName + "" + operators + "@" + filterFieldName + "");
- parameterlist.Add(new SqlParameter("@" + filterFieldName, filterValue));
- }
- else
- {
- strSql.Append(" where ID>@ID and (" + whereStr + ")");
- parameterlist.Add(new SqlParameter("@ID", Convert.ToInt32(0)));
- }
- SqlParameter[] parameters = parameterlist.ToArray();
- return new CommandInfo(strSql.ToString(), parameters, type);
- }
- /// <summary>
- /// 删除一条数据
- /// </summary>
- /// <param name="ID">id</param>
- public bool Delete(long ID)
- {
- return this.InsideDelete(filterValue: ID);
- }
- /// <summary>
- /// 删除一条数据
- /// </summary>
- /// <param name="ID">id</param>
- public bool Delete(int ID)
- {
- return this.InsideDelete(filterValue: ID);
- }
- /// <summary>
- /// 根据筛选字段删除数据
- /// </summary>
- /// <param name="filterValue">筛选值</param>
- /// <param name="filterFieldName">筛选字段名</param>
- /// <param name="operators">筛选SQL运算符</param>
- /// <returns></returns>
- public bool Delete(object filterValue, string filterFieldName = "ID", string operators = "=")
- {
- return this.InsideDelete(filterFieldName, operators, filterValue);
- }
- /// <summary>
- /// 根据where条件删除数据 不建义使用此方法
- /// </summary>
- /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
- /// <returns></returns>
- public bool Delete(string whereStr)
- {
- return this.InsideDelete(whereStr: whereStr);
- }
- /// <summary>
- /// 批量删除数据
- /// </summary>
- /// <param name="filterFieldName">筛选字段名</param>
- /// <param name="valueList">值列表,多个值用“,”分隔,字符串值用“'”号包含</param>
- public bool DeleteList(string filterFieldName, string valueList)
- {
- if (filterFieldName != "" && valueList != "")
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("delete from " + this.TableName + " ");
- strSql.Append(" where " + filterFieldName + " in (" + valueList + ") ");
- int rows = SQLHelper.ExecuteSql(strSql.ToString());
- if (rows > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 按ID列表批量删除数据
- /// </summary>
- /// <param name="idList">ID列表,多个用“,”分隔</param>
- public bool DeleteList(string idList)
- {
- return this.DeleteList("ID", idList);
- }
- #endregion
- #region 查询数据
- /// <summary>
- /// 得到一个Object对象实体
- /// </summary>
- /// <param name="ID"></param>
- /// <returns></returns>
- public object GetModelObject(long ID)
- {
- return DataRowToModelObject(GetDataRow(ID));
- }
- /// <summary>
- /// 得到一个Object对象实体
- /// </summary>
- /// <param name="ID"></param>
- /// <returns></returns>
- public object GetModelObject(int ID)
- {
- return DataRowToModelObject(GetDataRow(ID));
- }
- /// <summary>
- /// 根据筛选条件获取一条数据Model对象
- /// </summary>
- /// <param name="filterFieldName">筛选条件字段名</param>
- /// <param name="filterValue">值</param>
- /// <returns></returns>
- public object GetModelObject(string filterFieldName, object filterValue)
- {
- return DataRowToModelObject(GetDataRow(filterFieldName, filterValue));
- }
- /// <summary>
- /// 根据筛选条件获取一条数据Model对象
- /// </summary>
- /// <param name="filterFieldName">筛选条件字段名</param>
- /// <param name="filterValue">值</param>
- /// <param name="operators">SQL筛选运算符号</param>
- /// <returns></returns>
- public object GetModelObject(string filterFieldName, object filterValue, string operators)
- {
- return DataRowToModelObject(GetDataRow(filterFieldName, filterValue, operators));
- }
- /// <summary>
- /// 根据筛选条件获取一条数据Model对象
- /// </summary>
- /// <param name="whereString">筛选条件</param>
- /// <returns></returns>
- public object GetModelObject(string whereString)
- {
- return DataRowToModelObject(GetDataRow(whereString));
- }
- /// <summary>
- /// 得到一个object对象实体
- /// </summary>
- /// <param name="row"></param>
- /// <returns></returns>
- public object DataRowToModelObject(DataRow row)
- {
- return DataRowToModel(row, this.ObjModel);
- }
- /// <summary>
- /// 获取一条数据DataRow对象
- /// </summary>
- /// <param name="ID">id</param>
- /// <returns></returns>
- public DataRow GetDataRow(long id)
- {
- return GetDataRow("ID", id);
- }
- /// <summary>
- /// 获取一条数据DataRow对象
- /// </summary>
- /// <param name="ID">id</param>
- /// <returns></returns>
- public DataRow GetDataRow(int id)
- {
- return GetDataRow("ID", id);
- }
- /// <summary>
- /// 根据筛选条件获取一条数据DataRow对象
- /// </summary>
- /// <param name="whereString">筛选条件</param>
- /// <returns></returns>
- public DataRow GetDataRow(string whereString)
- {
- if (whereString.Trim().Length > 0)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
- strSql.Append(" where " + whereString);
- DataSet ds = SQLHelper.Query(strSql.ToString());
- if (ds.Tables[0].Rows.Count > 0)
- {
- return ds.Tables[0].Rows[0];
- }
- else
- {
- return null;
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 根据筛选条件获取一条数据DataRow对象
- /// </summary>
- /// <param name="filterFieldName">筛选条件字段名</param>
- /// <param name="filterValue">值</param>
- /// <param name="operators">SQL筛选运算符号</param>
- /// <returns></returns>
- public DataRow GetDataRow(string filterFieldName, object filterValue, string operators = "=")
- {
- if (filterFieldName != "" && filterValue != null)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
- strSql.Append(" where 1=1 and " + filterFieldName + "" + operators + "@" + filterFieldName + " ");
- SqlParameter[] parameters = {
- new SqlParameter("@"+filterFieldName, filterValue)
- };
- DataSet ds = SQLHelper.Query(strSql.ToString(), parameters);
- if (ds.Tables[0].Rows.Count > 0)
- {
- return ds.Tables[0].Rows[0];
- }
- else
- {
- return null;
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 根据筛选条件获取一条数据DataRow对象(运算符是“=”)
- /// </summary>
- /// <param name="filterFieldName">筛选条件字段名</param>
- /// <param name="filterValue">值</param>
- /// <returns></returns>
- public DataRow GetDataRow(string filterFieldName, object filterValue)
- {
- return GetDataRow(filterFieldName, filterValue, "=");
- }
- /// <summary>
- /// 得到一个对象实体
- /// </summary>
- public object DataRowToModel(DataRow row, object model)
- {
- if (row != null)
- {
- for (int i = 0; i < PropertyInfos.Length; i++)
- {
- try
- {
- System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
- string tempFieldName = propertyInfo.Name;//字段名
- if (row[tempFieldName] != null)
- {
- object tempValue = row[tempFieldName];//对应字段值
- LYFZ.WinAPI.CustomPublicMethod.SetPropertyValue(model, propertyInfo, tempValue);
- }
- }
- catch
- {
- }
- }
- //for (int i = 0; i < this.TableFieldNames.Length; i++)
- //{
- // try
- // {
- // string tempFieldName = this.TableFieldNames[i].Trim();
- // if (row[tempFieldName] != null)
- // {
- // object tempValue = row[tempFieldName];
- // LYFZ.WinAPI.CustomPublicMethod.SetPropertyValue(model, tempFieldName, tempValue);
- // }
- // }
- // catch
- // {
- // }
- //}
- }
- return model;
- }
- /// <summary>
- /// 获得数据列表
- /// </summary>
- /// <param name="strWhere">条件语句 不包含 where 关键字</param>
- /// <param name="filedOrder">SQL排序 如:id desc</param>
- public DataSet GetList(string strWhere, string filedOrder = "ID asc")
- {
- return this.GetList(0, strWhere, filedOrder);
- }
- /// <summary>
- /// 获得全部数据列表
- /// </summary>
- /// <param name="filedOrder">SQL排序 如:id desc</param>
- public DataSet GetAllList(string filedOrder = "ID asc")
- {
- return this.GetList("", filedOrder);
- }
- /// <summary>
- /// 获得前几行数据
- /// </summary>
- /// <param name="Top">行数</param>
- /// <param name="strWhere">条件 不包含 where 关键字</param>
- /// <param name="filedOrder">SQL排序 如:id asc</param>
- /// <returns></returns>
- public DataSet GetList(int Top, string strWhere, string filedOrder = "ID asc")
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("select ");
- if (Top > 0)
- {
- strSql.Append(" top " + Top.ToString());
- }
- strSql.Append(" " + this.TableFieldNameStr + " from " + this.TableName + " ");
- if (strWhere.Trim() != "")
- {
- strSql.Append(" where 1=1 and (" + strWhere + ")");
- }
- strSql.Append(" order by " + filedOrder);
- DataSet ds = SQLHelper.Query(strSql.ToString());
- ds.Tables[0].TableName = this.TableName;
- return ds;
- }
- #endregion
- #region 数据分页
- /// <summary>
- /// 获取记录总数
- /// </summary>
- public int GetRecordCount(string strWhere)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append("select count(1) FROM " + this.TableName + " ");
- if (strWhere.Trim() != "")
- {
- strSql.Append(" where 1=1 and (" + strWhere + ")");
- }
- object obj = SQLHelper.GetSingle(strSql.ToString());
- if (obj == null)
- {
- return 0;
- }
- else
- {
- return Convert.ToInt32(obj);
- }
- }
- /// <summary>
- /// 获取分页后总页数
- /// </summary>
- /// <param name="strWhere">筛选条件</param>
- /// <param name="pageSize">页面大小</param>
- /// <returns></returns>
- public int GetByPageCount(string strWhere, int pageSize)
- {
- int pageCount = 0;
- if (pageSize > 0)
- {
- int allCount = GetRecordCount(strWhere);
- pageCount = allCount / pageSize;
- if (pageCount * pageSize < allCount)
- {
- pageCount++;
- }
- }
- return pageCount;
- }
- /// <summary>
- /// 分页获取数据列表
- /// </summary>
- /// <param name="strWhere">筛选条件</param>
- /// <param name="pageIndex">当前页 不能小于0的整数</param>
- /// <param name="pageSize">页面大小,每页显示条数 不能小于0的整数</param>
- /// <param name="orderby">排序</param>
- /// <returns></returns>
- public DataSet GetListByPage(string strWhere, int pageIndex, int pageSize, string orderby = "id desc")
- {
- return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
- }
- /// <summary>
- /// 分页获取数据列表
- /// </summary>
- /// <param name="strWhere">筛选条件</param>
- /// <param name="orderby">排序</param>
- /// <param name="pageIndex">当前页</param>
- /// <param name="pageSize">页面大小,每页显示条数</param>
- /// <param name="pageCount">返回总页数</param>
- /// <returns></returns>
- public DataSet GetListByPage(string strWhere, string orderby, int pageIndex, int pageSize, ref int pageCount)
- {
- pageCount = GetByPageCount(strWhere, pageSize);
- return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
- }
- /// <summary>
- /// 分页获取数据列表
- /// </summary>
- /// <param name="strWhere">条件</param>
- /// <param name="orderby">排序</param>
- /// <param name="startIndex">开始index</param>
- /// <param name="endIndex">结束index</param>
- /// <returns></returns>
- public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
- {
- if (!string.IsNullOrEmpty(strWhere.Trim()))
- {
- strWhere = " Where " + strWhere;
- }
- if (!string.IsNullOrEmpty(orderby.Trim()))
- {
- orderby = " Order By " + orderby;
- }
- StringBuilder strSql = new StringBuilder();
- strSql.Append("SELECT * FROM " + this.TableName + " Where ID Not IN ");
- strSql.Append("(Select Top " + startIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
- strSql.Append(" And ID In ");
- strSql.Append("(Select Top " + endIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
- strSql.Append(orderby);
- DataSet ds = SQLHelper.Query(strSql.ToString());
- ds.Tables[0].TableName = this.TableName;
- return ds;
- }
- #endregion
- #region 2014-8-25 滕工
- /// <summary>
- /// Type转Dbtype
- /// </summary>
- /// <param name="type">变量的Type类型</param>
- /// <returns>对应的DbType类型</returns>
- public static SqlDbType GetDbType(Type type)
- {
- SqlDbType result = SqlDbType.NVarChar;
- if (type.Equals(typeof(Int32)) || type.IsEnum)
- result = SqlDbType.Int;
- else if (type.Equals(typeof(Int64)))
- result = SqlDbType.Int;
- else if (type.Equals(typeof(Double)) || type.Equals(typeof(Double)))
- result = SqlDbType.Decimal;
- else if (type.Equals(typeof(DateTime)))
- result = SqlDbType.DateTime;
- else if (type.Equals(typeof(Boolean)))
- result = SqlDbType.Bit;
- else if (type.Equals(typeof(String)))
- result = SqlDbType.NVarChar;
- else if (type.Equals(typeof(Decimal)))
- result = SqlDbType.Decimal;
- else if (type.Equals(typeof(Byte[])))
- result = SqlDbType.Image;
- else if (type.Equals(typeof(Guid)))
- result = SqlDbType.UniqueIdentifier;
- return result;
- }
- #endregion
- }
- }
|