BaseDAL.cs 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. using MOKA_Factory_Tools.Database;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Converters;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Dynamic;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace MOKA_Factory_Tools.DAL
  17. {
  18. public abstract class BaseDAL
  19. {
  20. #region 属性
  21. /// <summary>
  22. /// 数据表模型对象
  23. /// </summary>
  24. public abstract object ObjModel { get; }
  25. /// <summary>
  26. /// 数据表名
  27. /// </summary>
  28. public abstract string TableName { get; set; }
  29. /// <summary>
  30. /// 过期属性请不要使用 数据表字段名字符串,字段名以“,”号分隔
  31. /// </summary>
  32. public abstract string TableFieldNameString { get; set; }
  33. string _TableFieldNameStr = "";
  34. /// <summary>
  35. /// 数据表字段名字符串,字段名以“,”号分隔
  36. /// </summary>
  37. public string TableFieldNameStr
  38. {
  39. get
  40. {
  41. if (this._TableFieldNameStr.Trim() == "")
  42. {
  43. for (int i = 0; i < this.TableFieldNames.Length; i++)
  44. {
  45. if (this._TableFieldNameStr.Trim() == "")
  46. {
  47. this._TableFieldNameStr += "[" + this.TableFieldNames[i] + "]";
  48. }
  49. else
  50. {
  51. this._TableFieldNameStr += ",[" + this.TableFieldNames[i] + "]";
  52. }
  53. }
  54. }
  55. if (this._TableFieldNameStr.Trim() == "")
  56. {
  57. this._TableFieldNameStr = "*";
  58. }
  59. return this._TableFieldNameStr;
  60. }
  61. set { this._TableFieldNameStr = value; }
  62. }
  63. /// <summary>
  64. /// 数据表字段名数组
  65. /// </summary>
  66. public string[] TableFieldNames
  67. {
  68. get
  69. {
  70. System.Reflection.PropertyInfo[] propertyInfos = this.PropertyInfos;
  71. string[] fieldNames = new string[propertyInfos.Length];
  72. for (int i = 0; i < propertyInfos.Length; i++)
  73. {
  74. if (!propertyInfos[i].IsSpecialName)
  75. {
  76. System.Reflection.PropertyInfo propertyInfo = propertyInfos[i];
  77. fieldNames[i] = propertyInfo.Name;
  78. }
  79. }
  80. // System.Reflection.PropertyInfo propertyInfo = ObjModel.GetType().GetProperty(propertyInfos[0].Name);
  81. return fieldNames;//this.TableFieldNameString.Replace("[", "").Replace("]", "").Replace(" ", "").Split(',');
  82. }
  83. }
  84. public ConnectionParameters cps = new ConnectionParameters();
  85. /// <summary>
  86. /// 获取要查询的当前表的字段集合字符串 用","号分隔
  87. /// </summary>
  88. /// <param name="filters"></param>
  89. /// <param name="isContain">如果为true 表示只返回 filters 集合中的字段 如果为false 表示返回排除 filters 集合中的字段以外的所有字段</param>
  90. /// <returns></returns>
  91. public string GetQueryTableFieldNames(string[] filters = null, bool isContain = false)
  92. {
  93. List<string> tempFilters = new List<string>();
  94. if (filters != null)
  95. {
  96. tempFilters.AddRange(filters);
  97. }
  98. System.Reflection.PropertyInfo[] propertyInfos = this.PropertyInfos;
  99. string fieldNames = "";
  100. for (int i = 0; i < propertyInfos.Length; i++)
  101. {
  102. if (!propertyInfos[i].IsSpecialName)
  103. {
  104. System.Reflection.PropertyInfo propertyInfo = propertyInfos[i];
  105. if ((!tempFilters.Contains(propertyInfo.Name) && !isContain) || (tempFilters.Contains(propertyInfo.Name) && isContain))
  106. {
  107. if (fieldNames.Trim().Length <= 0)
  108. {
  109. fieldNames = "[" + propertyInfo.Name + "]";
  110. }
  111. else
  112. {
  113. fieldNames += ",[" + propertyInfo.Name + "]";
  114. }
  115. }
  116. }
  117. }
  118. return fieldNames;
  119. }
  120. /// <summary>
  121. /// 获取属性集合
  122. /// </summary>
  123. public System.Reflection.PropertyInfo[] PropertyInfos
  124. {
  125. get
  126. {
  127. List<System.Reflection.PropertyInfo> list = new List<System.Reflection.PropertyInfo>();
  128. System.Reflection.PropertyInfo[] infos = ObjModel.GetType().GetProperties();
  129. foreach (System.Reflection.PropertyInfo info in infos)
  130. {
  131. if (info.CanWrite)
  132. {
  133. list.Add(info);
  134. }
  135. }
  136. return list.ToArray();
  137. }
  138. }
  139. #endregion
  140. #region 检查记录
  141. /// <summary>
  142. /// 是否存在该记录
  143. /// </summary>
  144. public bool Exists(long id)
  145. {
  146. return this.Exists("ID", id);
  147. }
  148. /// <summary>
  149. /// 是否存在该记录
  150. /// </summary>
  151. public bool Exists(int id)
  152. {
  153. return this.Exists("ID", id);
  154. }
  155. /// <summary>
  156. /// 根据筛选条件判断是否存在该记录
  157. /// </summary>
  158. /// <param name="filterFieldName">筛选字段名</param>
  159. /// <param name="filterValue">筛选值</param>
  160. /// <returns></returns>
  161. public bool Exists(string filterFieldName, object filterValue)
  162. {
  163. if (filterFieldName != "" && filterValue != null)
  164. {
  165. StringBuilder strSql = new StringBuilder();
  166. strSql.Append("select count(1) from " + this.TableName + " ");
  167. strSql.Append(" where 1=1 and " + filterFieldName + "=@" + filterFieldName);
  168. SqlParameter[] parameters = { new SqlParameter("@" + filterFieldName, filterValue) };
  169. return DbHelper.Exists(cps.ConnectionString, strSql.ToString(), parameters);
  170. }
  171. else
  172. {
  173. return false;
  174. }
  175. }
  176. /// <summary>
  177. /// 根据筛选条件判断是否存在该记录
  178. /// </summary>
  179. /// <param name="filterFieldName">筛选字段名</param>
  180. /// <param name="filterValue">筛选值</param>
  181. /// <returns></returns>
  182. public bool Exists(string whereString)
  183. {
  184. if (whereString.Trim().Length > 0)
  185. {
  186. StringBuilder strSql = new StringBuilder();
  187. strSql.Append("select count(1) from " + this.TableName + " ");
  188. strSql.Append(" where " + whereString);
  189. return DbHelper.Exists(cps.ConnectionString, strSql.ToString());
  190. }
  191. else
  192. {
  193. return false;
  194. }
  195. }
  196. /// <summary>
  197. /// 判断是否存某个字段
  198. /// </summary>
  199. /// <param name="columnName">列名称</param>
  200. /// <returns>是否存在</returns>
  201. public bool ColumnExists(string columnName)
  202. {
  203. return DbHelper.IsColumnExists(cps.ConnectionString, this.TableName, columnName);
  204. }
  205. #endregion
  206. #region 公共方法
  207. /// <summary>
  208. /// 获取最大ID()
  209. /// </summary>
  210. /// <returns></returns>
  211. public int GetMaxID()
  212. {
  213. return this.GetMaxID("ID");
  214. }
  215. /// <summary>
  216. /// 获取某字段最大值(获取字段必须为数字类型)
  217. /// </summary>
  218. /// <param name="FieldName"></param>
  219. /// <param name="strWhere"></param>
  220. /// <returns></returns>
  221. public int GetMaxID(string FieldName, string strWhere = "")
  222. {
  223. return DbHelper.GetMaxID(cps.ConnectionString, FieldName, this.TableName, strWhere);
  224. }
  225. /// <summary>
  226. /// 检查CLR功能是否开启
  227. /// </summary>
  228. /// <returns></returns>
  229. public bool CheckingCLR()
  230. {
  231. try
  232. {
  233. DataSet cds = DbHelper.Query(cps.ConnectionString, "select top 1 CONVERT(nvarchar(200), dbo.FunTrim(dbo.AggregateString(GM_CustomerID + '{$$}/{where}' +[GM_CustomerID] + '{$$}={$$}' + GM_RelatedPersonID + '{$$}s') + '/' + dbo.AggregateString(GM_CustomerID + '{$$}/{where}' +[GM_CustomerID] + '{$$}<>{$$}' + GM_RelatedPersonID + '{$$}s'), '/')) as CustomerID from[dbo].[tb_ErpCustomerGroupMembers] group by GM_CustomerGroupID");
  234. return true;
  235. }
  236. catch
  237. {
  238. return false;
  239. }
  240. }
  241. public void ExecuteSql(string sql)
  242. {
  243. DbHelper.ExecuteSQL(cps.ConnectionString, sql);
  244. }
  245. #endregion
  246. #region 判断方法
  247. /// <summary>
  248. /// 获取是否为忽略字段 返回 true 表示忽略 false 不忽略
  249. /// </summary>
  250. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  251. /// <param name="fieldName">要判断的字段名</param>
  252. /// <returns></returns>
  253. public bool IsOverlookFiel(string overlookFieldList, string fieldName)
  254. {
  255. bool bl = false;
  256. string[] tempOverlookFieldList = overlookFieldList.Split(',');
  257. for (int i = 0; i < tempOverlookFieldList.Length; i++)
  258. {
  259. if (tempOverlookFieldList[i].Trim().ToLower() == fieldName.Trim().ToLower())
  260. {
  261. bl = true;
  262. break;
  263. }
  264. }
  265. return bl;
  266. }
  267. #endregion
  268. #region 增加数据
  269. /// <summary>
  270. /// 增加一条数据
  271. /// </summary>
  272. /// <param name="model">Model对象</param>
  273. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  274. /// <returns></returns>
  275. public bool Add(object model, string overlookFieldList = "ID")
  276. {
  277. CommandInfo comdInfo = this.GetAddCommandInfo(model, overlookFieldList);
  278. int rows = DbHelper.ExecuteSQL(cps.ConnectionString, comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  279. if (rows > 0)
  280. {
  281. return true;
  282. }
  283. else
  284. {
  285. return false;
  286. }
  287. }
  288. /// <summary>
  289. /// 获取插入数CommandInfo对象
  290. /// </summary>
  291. /// <param name="model"></param>
  292. /// <param name="overlookFieldList">要忽略的字段集合 多个字段名有“,”号分隔</param>
  293. /// <returns></returns>
  294. public object GetAddCommandInfoObject(object model, string overlookFieldList = "ID")
  295. {
  296. return this.GetAddCommandInfo(model, overlookFieldList);
  297. }
  298. /// <summary>
  299. /// 获取插入数CommandInfo对象
  300. /// </summary>
  301. /// <param name="model"></param>
  302. /// <param name="overlookFieldList">要忽略的字段集合 多个字段名有“,”号分隔</param>
  303. /// <param name="type"></param>
  304. /// <returns></returns>
  305. public CommandInfo GetAddCommandInfo(object model, string overlookFieldList = "ID", EffentNextType type = EffentNextType.None)
  306. {
  307. StringBuilder strSql = new StringBuilder();
  308. StringBuilder tempFieldNameString = new StringBuilder();
  309. StringBuilder tempValueVariable = new StringBuilder();
  310. List<SqlParameter> parameterlist = new List<SqlParameter>();
  311. for (int i = 0; i < PropertyInfos.Length; i++)
  312. {
  313. try
  314. {
  315. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  316. string tempFieldName = propertyInfo.Name;
  317. object tempValue = propertyInfo.GetValue(model, null);
  318. Type tempType = propertyInfo.PropertyType;
  319. if (!IsOverlookFiel(overlookFieldList, tempFieldName) || (tempFieldName.ToLower() == "id" && tempType == typeof(long)))
  320. {
  321. try
  322. {
  323. if (tempType.Name == "DateTime")
  324. {
  325. if (tempValue.ToString().Contains("0001"))
  326. {
  327. tempValue = null;
  328. }
  329. try
  330. {
  331. DateTime tempTime;
  332. if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
  333. {
  334. tempValue = null;
  335. }
  336. else if (tempTime <= Convert.ToDateTime("1900-01-01"))
  337. {
  338. tempValue = null;
  339. }
  340. }
  341. catch
  342. {
  343. tempValue = null;
  344. }
  345. }
  346. else if (tempFieldName.ToLower() == "id" && tempType == typeof(long) && (tempValue == null || Convert.ToInt64(tempValue) == 0))
  347. {
  348. byte[] buffer = Guid.NewGuid().ToByteArray();
  349. tempValue = BitConverter.ToInt64(buffer, 0);
  350. }
  351. if (tempValue != null)
  352. {
  353. tempFieldNameString.Append("" + tempFieldName + ",");
  354. tempValueVariable.Append("@" + tempFieldName + ",");
  355. SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
  356. string classFullName = tempType.ToString();
  357. if (classFullName.ToLower().IndexOf("system.") != 0)
  358. {
  359. parameter.DbType = DbType.String;
  360. parameter = new SqlParameter("@" + tempFieldName, tempValue.ToString());
  361. }
  362. parameterlist.Add(parameter);
  363. }
  364. }
  365. catch { }
  366. }
  367. }
  368. catch { }
  369. }
  370. //去除结尾多余的“,”
  371. if (tempFieldNameString.ToString().LastIndexOf(',') == tempFieldNameString.Length - 1)
  372. {
  373. tempFieldNameString.Remove(tempFieldNameString.Length - 1, 1);
  374. }
  375. //去除结尾多余的“,”
  376. if (tempValueVariable.ToString().LastIndexOf(',') == tempValueVariable.Length - 1)
  377. {
  378. tempValueVariable.Remove(tempValueVariable.Length - 1, 1);
  379. }
  380. strSql.Append("insert into " + this.TableName + "(");
  381. strSql.Append("" + tempFieldNameString.ToString() + ")");
  382. strSql.Append(" values (");
  383. strSql.Append("" + tempValueVariable.ToString() + ")");
  384. strSql.Append(";select @@IDENTITY");
  385. SqlParameter[] parameters = parameterlist.ToArray();
  386. return new CommandInfo(strSql.ToString(), parameters, type);
  387. }
  388. #endregion
  389. #region 更新数据
  390. /// <summary>
  391. /// 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  392. /// </summary>
  393. /// <param name="model">Model对象</param>
  394. /// <param name="filterFieldName">筛选字段名称</param>
  395. /// <param name="operators">SQL筛选运算符号</param>
  396. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  397. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  398. /// <returns></returns>
  399. protected bool InsideUpdate(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
  400. {
  401. CommandInfo comdInfo = GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
  402. int rows = DbHelper.ExecuteSQL(cps.ConnectionString, comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  403. if (rows > 0)
  404. {
  405. return true;
  406. }
  407. else
  408. {
  409. return false;
  410. }
  411. }
  412. /// <summary>
  413. /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  414. /// </summary>
  415. /// <param name="model">Model对象</param>
  416. /// <param name="filterFieldName">筛选字段名称</param>
  417. /// <param name="operators">SQL筛选运算符号</param>
  418. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  419. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  420. public object GetUpdateCommandInfoObject(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
  421. {
  422. return this.GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
  423. }
  424. /// <summary>
  425. /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  426. /// </summary>
  427. /// <param name="model">Model对象</param>
  428. /// <param name="filterFieldName">筛选字段名称</param>
  429. /// <param name="operators">SQL筛选运算符号</param>
  430. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  431. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  432. /// <param name="type"></param>
  433. public CommandInfo GetUpdateCommandInfo(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null, EffentNextType type = EffentNextType.None)
  434. {
  435. StringBuilder strSql = new StringBuilder();
  436. strSql.Append("update " + this.TableName + " set ");
  437. List<SqlParameter> parameterlist = new List<SqlParameter>();
  438. overlookFieldList += "," + filterFieldName;
  439. object VerifyTimestamp = null;
  440. for (int i = 0; i < PropertyInfos.Length; i++)
  441. {
  442. try
  443. {
  444. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  445. string tempFieldName = propertyInfo.Name;//字段名
  446. object tempValue = propertyInfo.GetValue(model, null);//对应字段值
  447. //如果字段名不为忽略字段则进行处理
  448. if (!IsOverlookFiel(overlookFieldList, tempFieldName))
  449. {
  450. try
  451. {
  452. if (tempValue.GetType() == typeof(DateTime))
  453. {
  454. DateTime tempTime;
  455. try
  456. {
  457. if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
  458. {
  459. tempValue = null;
  460. }
  461. else if (tempTime <= Convert.ToDateTime("1900-01-01"))
  462. {
  463. tempValue = null;
  464. }
  465. else if (tempTime.Year == Convert.ToDateTime("0001-01-01").Year || tempTime.Year == Convert.ToDateTime("1753-01-01").Year)
  466. {
  467. tempValue = null;
  468. }
  469. }
  470. catch { tempValue = null; }
  471. }
  472. else if (tempValue.GetType() == typeof(byte[]))
  473. {
  474. if (tempFieldName.ToLower() == "VerifyTimestamp".ToLower())
  475. {
  476. VerifyTimestamp = tempValue;
  477. continue;
  478. }
  479. }
  480. }
  481. catch { }
  482. strSql.Append("" + tempFieldName + "=@" + tempFieldName + ",");
  483. SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
  484. string classFullName = propertyInfo.PropertyType.ToString();
  485. if (classFullName.ToLower().IndexOf("system.") != 0)
  486. {
  487. parameter.DbType = DbType.String;
  488. parameter = new SqlParameter("@" + tempFieldName, tempValue.ToString());
  489. }
  490. parameterlist.Add(parameter);
  491. }
  492. }
  493. catch { }
  494. }
  495. //去除结尾多余的“,”
  496. if (strSql.ToString().LastIndexOf(',') == strSql.Length - 1)
  497. {
  498. strSql.Remove(strSql.Length - 1, 1);
  499. }
  500. if (whereStr == null)
  501. {
  502. if (filterFieldName != "" && operators != "")
  503. {
  504. try
  505. {
  506. object FilterFieldValue = DbHelper.GetPropertyValue(model, filterFieldName);// model.GetType().GetProperty(filterFieldName).GetValue(model, null);
  507. if (VerifyTimestamp != null)
  508. {
  509. strSql.Append(" where " + filterFieldName + operators + "@" + filterFieldName + " and VerifyTimestamp=@VerifyTimestamp");
  510. parameterlist.Add(new SqlParameter("@VerifyTimestamp", VerifyTimestamp));
  511. }
  512. else
  513. {
  514. strSql.Append(" where " + filterFieldName + operators + "@" + filterFieldName);
  515. }
  516. parameterlist.Add(new SqlParameter("@" + filterFieldName, FilterFieldValue));
  517. }
  518. catch
  519. {
  520. }
  521. }
  522. }
  523. else
  524. {
  525. if (VerifyTimestamp != null)
  526. {
  527. strSql.Append(" where " + filterFieldName + operators + "@" + filterFieldName + " and VerifyTimestamp=@VerifyTimestamp");
  528. parameterlist.Add(new SqlParameter("@VerifyTimestamp", VerifyTimestamp));
  529. }
  530. else
  531. {
  532. strSql.Append(" where 1=1 and (" + whereStr + ")");
  533. }
  534. }
  535. SqlParameter[] parameters = parameterlist.ToArray();
  536. return new CommandInfo(strSql.ToString(), parameters, type);
  537. }
  538. /// <summary>
  539. /// 根据筛选字段和SQL筛选运算符号更新数据
  540. /// </summary>
  541. /// <param name="model">Model对象</param>
  542. /// <param name="filterFieldName">筛选字段名称</param>
  543. /// <param name="operators">SQL筛选运算符号</param>
  544. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  545. /// <returns></returns>
  546. public bool Update(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID")
  547. {
  548. return InsideUpdate(model, filterFieldName, operators, overlookFieldList);
  549. }
  550. /// <summary>
  551. /// 根据Wher条件更新数据 不建义使用此方法
  552. /// </summary>
  553. /// <param name="model">Model对象</param>
  554. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  555. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  556. /// <returns></returns>
  557. public bool Update(object model, string whereStr, string overlookFieldList = "ID")
  558. {
  559. return InsideUpdate(model, overlookFieldList: overlookFieldList, whereStr: whereStr);
  560. }
  561. /// <summary>
  562. ///
  563. /// </summary>
  564. /// <param name="model"></param>
  565. /// <param name="strWhere"></param>
  566. /// <returns></returns>
  567. /// 因为AMResult表没有代理主键ID,所以需要独立方法更新;
  568. public bool UpdateAMResult(object model, string strWhere, string IgnoreUpdateFields = "Station,SN,ODF,DSN,Line,Model,Dimension")
  569. {
  570. CommandInfo comdInfo = GetUpdateAMResultCommandInfo(model, IgnoreUpdateFields, strWhere);
  571. int rows = DbHelper.ExecuteSQL(cps.ConnectionString, comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  572. if (rows > 0)
  573. {
  574. return true;
  575. }
  576. else
  577. {
  578. return false;
  579. }
  580. }
  581. /// <summary>
  582. ///
  583. /// 问题:Where条件后面跟的值是旧值,model的新值;
  584. /// </summary>
  585. /// <param name="model"></param>
  586. /// <param name="IgnoreUpdateFields"></param>
  587. /// <param name="whereStr"></param>
  588. /// <param name="type"></param>
  589. /// <returns></returns>
  590. public CommandInfo GetUpdateAMResultCommandInfo(object model, string IgnoreUpdateFields = "Station,SN,ODF,DSN,Line,Model,Dimension", string whereStr = null, EffentNextType type = EffentNextType.None)
  591. {
  592. StringBuilder strSql = new StringBuilder();
  593. strSql.Append("update " + this.TableName + " set ");
  594. List<SqlParameter> parameterlist = new List<SqlParameter>();
  595. IgnoreUpdateFields += ",";
  596. object VerifyTimestamp = null;
  597. for (int i = 0; i < PropertyInfos.Length; i++)
  598. {
  599. try
  600. {
  601. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  602. string tempFieldName = propertyInfo.Name;//字段名
  603. object tempValue = propertyInfo.GetValue(model, null);//对应字段值
  604. //如果字段名不为忽略字段则进行处理
  605. if (!IsOverlookFiel(IgnoreUpdateFields, tempFieldName))
  606. {
  607. try
  608. {
  609. if (tempValue.GetType() == typeof(DateTime))
  610. {
  611. DateTime tempTime;
  612. try
  613. {
  614. if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
  615. {
  616. tempValue = null;
  617. }
  618. else if (tempTime <= Convert.ToDateTime("1900-01-01"))
  619. {
  620. tempValue = null;
  621. }
  622. else if (tempTime.Year == Convert.ToDateTime("0001-01-01").Year || tempTime.Year == Convert.ToDateTime("1753-01-01").Year)
  623. {
  624. tempValue = null;
  625. }
  626. }
  627. catch { tempValue = null; }
  628. }
  629. else if (tempValue.GetType() == typeof(byte[]))
  630. {
  631. if (tempFieldName.ToLower() == "VerifyTimestamp".ToLower())
  632. {
  633. VerifyTimestamp = tempValue;
  634. continue;
  635. }
  636. }
  637. }
  638. catch { }
  639. strSql.Append("" + tempFieldName + "=@" + tempFieldName + ",");
  640. SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
  641. string classFullName = propertyInfo.PropertyType.ToString();
  642. if (classFullName.ToLower().IndexOf("system.") != 0)
  643. {
  644. parameter.DbType = DbType.String;
  645. parameter = new SqlParameter("@" + tempFieldName, tempValue.ToString());
  646. }
  647. parameterlist.Add(parameter);
  648. }
  649. }
  650. catch { }
  651. }
  652. //去除结尾多余的“,”
  653. if (strSql.ToString().LastIndexOf(',') == strSql.Length - 1)
  654. {
  655. strSql.Remove(strSql.Length - 1, 1);
  656. }
  657. if (whereStr != null)
  658. {
  659. strSql.Append(" where 1=1 and (" + whereStr + ")");
  660. }
  661. SqlParameter[] parameters = parameterlist.ToArray();
  662. return new CommandInfo(strSql.ToString(), parameters, type);
  663. }
  664. #endregion
  665. #region 删除数据
  666. /// <summary>
  667. /// 根据筛选字段或where条件删除数据
  668. /// </summary>
  669. /// <param name="filterFieldName">筛选字段名</param>
  670. /// <param name="operators">筛选SQL运算符</param>
  671. /// <param name="filterValue">筛选值</param>
  672. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  673. /// <returns></returns>
  674. protected bool InsideDelete(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
  675. {
  676. CommandInfo comdInfo = GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
  677. int rows = DbHelper.ExecuteSQL(cps.ConnectionString, comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  678. if (rows > 0)
  679. {
  680. return true;
  681. }
  682. else
  683. {
  684. return false;
  685. }
  686. }
  687. /// <summary>
  688. /// 根据筛选字段或where条件删除数据
  689. /// </summary>
  690. /// <param name="filterFieldName">筛选字段名</param>
  691. /// <param name="operators">筛选SQL运算符</param>
  692. /// <param name="filterValue">筛选值</param>
  693. /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
  694. /// <returns></returns>
  695. public object GetDeleteCommandInfoObject(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
  696. {
  697. return this.GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
  698. }
  699. /// <summary>
  700. /// 根据筛选字段或where条件删除数据
  701. /// </summary>
  702. /// <param name="filterFieldName">筛选字段名</param>
  703. /// <param name="operators">筛选SQL运算符</param>
  704. /// <param name="filterValue">筛选值</param>
  705. /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
  706. /// <param name="type"></param>
  707. /// <returns></returns>
  708. public CommandInfo GetDeleteCommandInfo(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null, EffentNextType type = EffentNextType.None)
  709. {
  710. if (filterValue == null) { filterValue = ""; }
  711. List<SqlParameter> parameterlist = new List<SqlParameter>();
  712. StringBuilder strSql = new StringBuilder();
  713. strSql.Append("delete from " + this.TableName + " ");
  714. if (whereStr == null)
  715. {
  716. strSql.Append(" where " + filterFieldName + "" + operators + "@" + filterFieldName + "");
  717. parameterlist.Add(new SqlParameter("@" + filterFieldName, filterValue));
  718. }
  719. else
  720. {
  721. strSql.Append(" where ID>@ID and (" + whereStr + ")");
  722. parameterlist.Add(new SqlParameter("@ID", Convert.ToInt32(0)));
  723. }
  724. SqlParameter[] parameters = parameterlist.ToArray();
  725. return new CommandInfo(strSql.ToString(), parameters, type);
  726. }
  727. /// <summary>
  728. /// 删除一条数据
  729. /// </summary>
  730. /// <param name="ID">id</param>
  731. public bool Delete(long ID)
  732. {
  733. return this.InsideDelete(filterValue: ID);
  734. }
  735. /// <summary>
  736. /// 删除一条数据
  737. /// </summary>
  738. /// <param name="ID">id</param>
  739. public bool Delete(int ID)
  740. {
  741. return this.InsideDelete(filterValue: ID);
  742. }
  743. /// <summary>
  744. /// 根据筛选字段删除数据
  745. /// </summary>
  746. /// <param name="filterValue">筛选值</param>
  747. /// <param name="filterFieldName">筛选字段名</param>
  748. /// <param name="operators">筛选SQL运算符</param>
  749. /// <returns></returns>
  750. public bool Delete(object filterValue, string filterFieldName = "ID", string operators = "=")
  751. {
  752. return this.InsideDelete(filterFieldName, operators, filterValue);
  753. }
  754. /// <summary>
  755. /// 根据where条件删除数据 不建义使用此方法
  756. /// </summary>
  757. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  758. /// <returns></returns>
  759. public bool Delete(string whereStr)
  760. {
  761. return this.InsideDelete(whereStr: whereStr);
  762. }
  763. /// <summary>
  764. /// 批量删除数据
  765. /// </summary>
  766. /// <param name="filterFieldName">筛选字段名</param>
  767. /// <param name="valueList">值列表,多个值用“,”分隔,字符串值用“'”号包含</param>
  768. public bool DeleteList(string filterFieldName, string valueList)
  769. {
  770. if (filterFieldName != "" && valueList != "")
  771. {
  772. StringBuilder strSql = new StringBuilder();
  773. strSql.Append("delete from " + this.TableName + " ");
  774. strSql.Append(" where " + filterFieldName + " in (" + valueList + ") ");
  775. int rows = DbHelper.ExecuteSQL(cps.ConnectionString, strSql.ToString());
  776. if (rows > 0)
  777. {
  778. return true;
  779. }
  780. else
  781. {
  782. return false;
  783. }
  784. }
  785. else
  786. {
  787. return false;
  788. }
  789. }
  790. /// <summary>
  791. /// 按ID列表批量删除数据
  792. /// </summary>
  793. /// <param name="idList">ID列表,多个用“,”分隔</param>
  794. public bool DeleteList(string idList)
  795. {
  796. return this.DeleteList("ID", idList);
  797. }
  798. #endregion
  799. #region 查询数据
  800. public T GetDataRowToModelByEntity<T>(DataRow row)
  801. {
  802. if (row == null)
  803. {
  804. return Activator.CreateInstance<T>();
  805. }
  806. Dictionary<string, string> dataInfo = new Dictionary<string, string>();
  807. dynamic objmatch = new ExpandoObject();
  808. for (int i = 0; i < row.Table.Columns.Count; i++)
  809. {
  810. string value = "";
  811. if (row[row.Table.Columns[i].ColumnName] != null)
  812. {
  813. value = row[row.Table.Columns[i].ColumnName].ToString();
  814. }
  815. if (!string.IsNullOrEmpty(value))
  816. {
  817. if (row.Table.Columns[i].DataType.Name.ToLower().IndexOf("DateTime".ToLower()) != -1)
  818. {
  819. dataInfo.Add(row.Table.Columns[i].ColumnName, Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss"));
  820. }
  821. else
  822. {
  823. dataInfo.Add(row.Table.Columns[i].ColumnName, value);
  824. }
  825. }
  826. else
  827. {
  828. if (row.Table.Columns[i].DataType.Name.ToLower().IndexOf("String".ToLower()) != -1)
  829. {
  830. dataInfo.Add(row.Table.Columns[i].ColumnName, value);
  831. }
  832. }
  833. }
  834. objmatch = dataInfo;
  835. string json = JsonConvert.SerializeObject((object)objmatch);
  836. return JsonToModelByEntity<T>(json);
  837. }
  838. T JsonToModelByEntity<T>(string json)
  839. {
  840. T t = Activator.CreateInstance<T>();
  841. return (T)JsonConvert.DeserializeObject(json, t.GetType());
  842. }
  843. public DataSet GetViewDataSetByCustomer(string strSQL)
  844. {
  845. return DbHelper.Query(cps.ConnectionString, strSQL);
  846. }
  847. public object DataRowToModel<T>(DataRow dr)
  848. {
  849. if (dr == null)
  850. {
  851. return Activator.CreateInstance<T>();
  852. }
  853. Dictionary<string, string> dataInfo = new Dictionary<string, string>();
  854. dynamic objmatch = new ExpandoObject();
  855. for (int i = 0; i < dr.Table.Columns.Count; i++)
  856. {
  857. string value = dr[dr.Table.Columns[i].ColumnName].ToString();
  858. if (!string.IsNullOrEmpty(value))
  859. {
  860. if (dr.Table.Columns[i].DataType.Name.IndexOf("DateTime") != -1)
  861. {
  862. dataInfo.Add(dr.Table.Columns[i].ColumnName, Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss"));
  863. }
  864. else
  865. {
  866. dataInfo.Add(dr.Table.Columns[i].ColumnName, value);
  867. }
  868. }
  869. else
  870. {
  871. if (dr.Table.Columns[i].DataType.Name.ToLower().IndexOf("String".ToLower()) != -1)
  872. {
  873. dataInfo.Add(dr.Table.Columns[i].ColumnName, "");
  874. }
  875. }
  876. }
  877. objmatch = dataInfo;
  878. string json = JsonConvert.SerializeObject((object)objmatch);
  879. return DataRowToModelByEntity<T>(json);
  880. }
  881. public object GetModelObjectByEntity<T>(string filterFieldName, object filterValue)
  882. {
  883. DataRow dr = GetDataRow(filterFieldName, filterValue);
  884. Dictionary<string, string> dataInfo = new Dictionary<string, string>();
  885. if (dr == null)
  886. {
  887. return Activator.CreateInstance<T>();
  888. }
  889. dynamic objmatch = new ExpandoObject();
  890. for (int i = 0; i < dr.Table.Columns.Count; i++)
  891. {
  892. string value = dr[dr.Table.Columns[i].ColumnName].ToString();
  893. if (!string.IsNullOrEmpty(value))
  894. {
  895. if (dr.Table.Columns[i].DataType.Name.IndexOf("DateTime") != -1)
  896. {
  897. dataInfo.Add(dr.Table.Columns[i].ColumnName, Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss"));
  898. }
  899. else
  900. {
  901. dataInfo.Add(dr.Table.Columns[i].ColumnName, value);
  902. }
  903. }
  904. else
  905. {
  906. if (dr.Table.Columns[i].DataType.Name.ToLower().IndexOf("String".ToLower()) != -1)
  907. {
  908. dataInfo.Add(dr.Table.Columns[i].ColumnName, value);
  909. }
  910. }
  911. }
  912. objmatch = dataInfo;
  913. string json = JsonConvert.SerializeObject((object)objmatch);
  914. return DataRowToModelByEntity<T>(json);
  915. }
  916. public object DataRowToModelByEntity<T>(int ID)
  917. {
  918. DataRow dr = GetDataRow(ID);
  919. if (dr == null)
  920. {
  921. return Activator.CreateInstance<T>();
  922. }
  923. Dictionary<string, string> dataInfo = new Dictionary<string, string>();
  924. dynamic objmatch = new ExpandoObject();
  925. for (int i = 0; i < dr.Table.Columns.Count; i++)
  926. {
  927. string value = dr[dr.Table.Columns[i].ColumnName].ToString();
  928. if (!string.IsNullOrEmpty(value))
  929. {
  930. if (dr.Table.Columns[i].DataType.Name.IndexOf("DateTime") != -1)
  931. {
  932. dataInfo.Add(dr.Table.Columns[i].ColumnName, Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss"));
  933. }
  934. else
  935. {
  936. dataInfo.Add(dr.Table.Columns[i].ColumnName, value);
  937. }
  938. }
  939. else
  940. {
  941. if (dr.Table.Columns[i].DataType.Name.ToLower().IndexOf("String".ToLower()) != -1)
  942. {
  943. dataInfo.Add(dr.Table.Columns[i].ColumnName, value);
  944. }
  945. }
  946. }
  947. objmatch = dataInfo;
  948. IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
  949. timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
  950. string json = JsonConvert.SerializeObject((object)objmatch, Formatting.Indented, timeFormat);
  951. return DataRowToModelByEntity<T>(json);
  952. }
  953. object DataRowToModelByEntity<T>(string json)
  954. {
  955. T t = Activator.CreateInstance<T>();
  956. object obj = JsonConvert.DeserializeObject(json, t.GetType());//CustomPublicMethod.JsonToObject(json, t);
  957. return obj;
  958. }
  959. /// <summary>
  960. /// 得到一个Object对象实体
  961. /// </summary>
  962. /// <param name="ID"></param>
  963. /// <returns></returns>
  964. public object GetModelObject(long ID)
  965. {
  966. return DataRowToModelObject(GetDataRow(ID));
  967. }
  968. /// <summary>
  969. /// 得到一个Object对象实体
  970. /// </summary>
  971. /// <param name="ID"></param>
  972. /// <returns></returns>
  973. public object GetModelObject(int ID)
  974. {
  975. return DataRowToModelObject(GetDataRow(ID));
  976. }
  977. /// <summary>
  978. /// 根据筛选条件获取一条数据Model对象
  979. /// </summary>
  980. /// <param name="filterFieldName">筛选条件字段名</param>
  981. /// <param name="filterValue">值</param>
  982. /// <returns></returns>
  983. public object GetModelObject(string filterFieldName, object filterValue)
  984. {
  985. return DataRowToModelObject(GetDataRow(filterFieldName, filterValue));
  986. }
  987. /// <summary>
  988. /// 根据筛选条件获取一条数据Model对象
  989. /// </summary>
  990. /// <param name="filterFieldName">筛选条件字段名</param>
  991. /// <param name="filterValue">值</param>
  992. /// <param name="operators">SQL筛选运算符号</param>
  993. /// <returns></returns>
  994. public object GetModelObject(string filterFieldName, object filterValue, string operators)
  995. {
  996. return DataRowToModelObject(GetDataRow(filterFieldName, filterValue, operators));
  997. }
  998. /// <summary>
  999. /// 根据筛选条件获取一条数据Model对象
  1000. /// </summary>
  1001. /// <param name="whereString">筛选条件</param>
  1002. /// <returns></returns>
  1003. public object GetModelObject(string whereString)
  1004. {
  1005. return DataRowToModelObject(GetDataRow(whereString));
  1006. }
  1007. /// <summary>
  1008. /// 得到一个object对象实体
  1009. /// </summary>
  1010. /// <param name="row"></param>
  1011. /// <returns></returns>
  1012. public object DataRowToModelObject(DataRow row)
  1013. {
  1014. return DataRowToModel(row, this.ObjModel);
  1015. }
  1016. /// <summary>
  1017. /// 获取一条数据DataRow对象
  1018. /// </summary>
  1019. /// <param name="ID">id</param>
  1020. /// <returns></returns>
  1021. public DataRow GetDataRow(long id)
  1022. {
  1023. return GetDataRow("ID", id);
  1024. }
  1025. /// <summary>
  1026. /// 获取一条数据DataRow对象
  1027. /// </summary>
  1028. /// <param name="ID">id</param>
  1029. /// <returns></returns>
  1030. public DataRow GetDataRow(int id)
  1031. {
  1032. return GetDataRow("ID", id);
  1033. }
  1034. /// <summary>
  1035. /// 根据筛选条件获取一条数据DataRow对象
  1036. /// </summary>
  1037. /// <param name="whereString">筛选条件</param>
  1038. /// <returns></returns>
  1039. public DataRow GetDataRow(string whereString)
  1040. {
  1041. if (whereString.Trim().Length > 0)
  1042. {
  1043. StringBuilder strSql = new StringBuilder();
  1044. strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
  1045. strSql.Append(" where " + whereString);
  1046. DataSet ds = DbHelper.Query(cps.ConnectionString, strSql.ToString());
  1047. if (ds.Tables[0].Rows.Count > 0)
  1048. {
  1049. return ds.Tables[0].Rows[0];
  1050. }
  1051. else
  1052. {
  1053. return null;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. return null;
  1059. }
  1060. }
  1061. /// <summary>
  1062. /// 根据筛选条件获取一条数据DataRow对象
  1063. /// </summary>
  1064. /// <param name="filterFieldName">筛选条件字段名</param>
  1065. /// <param name="filterValue">值</param>
  1066. /// <param name="operators">SQL筛选运算符号</param>
  1067. /// <returns></returns>
  1068. public DataRow GetDataRow(string filterFieldName, object filterValue, string operators = "=")
  1069. {
  1070. if (filterFieldName != "" && filterValue != null)
  1071. {
  1072. StringBuilder strSql = new StringBuilder();
  1073. strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
  1074. strSql.Append(" where 1=1 and " + filterFieldName + "" + operators + "@" + filterFieldName + " ");
  1075. SqlParameter[] parameters = { new SqlParameter("@" + filterFieldName, filterValue) };
  1076. DataSet ds = DbHelper.Query(cps.ConnectionString, strSql.ToString(), parameters);
  1077. if (ds.Tables[0].Rows.Count > 0)
  1078. {
  1079. return ds.Tables[0].Rows[0];
  1080. }
  1081. else
  1082. {
  1083. return null;
  1084. }
  1085. }
  1086. else
  1087. {
  1088. return null;
  1089. }
  1090. }
  1091. /// <summary>
  1092. /// 根据筛选条件获取一条数据DataRow对象(运算符是“=”)
  1093. /// </summary>
  1094. /// <param name="filterFieldName">筛选条件字段名</param>
  1095. /// <param name="filterValue">值</param>
  1096. /// <returns></returns>
  1097. public DataRow GetDataRow(string filterFieldName, object filterValue)
  1098. {
  1099. return GetDataRow(filterFieldName, filterValue, "=");
  1100. }
  1101. /// <summary>
  1102. /// 得到一个对象实体
  1103. /// </summary>
  1104. public object DataRowToModel(DataRow row, object model)
  1105. {
  1106. if (row != null)
  1107. {
  1108. for (int i = 0; i < PropertyInfos.Length; i++)
  1109. {
  1110. try
  1111. {
  1112. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  1113. string tempFieldName = propertyInfo.Name;//字段名
  1114. if (row[tempFieldName] != null)
  1115. {
  1116. object tempValue = row[tempFieldName];//对应字段值
  1117. DbHelper.SetPropertyValue(model, propertyInfo, tempValue);
  1118. }
  1119. }
  1120. catch
  1121. {
  1122. }
  1123. }
  1124. }
  1125. return model;
  1126. }
  1127. /// <summary>
  1128. /// 获得数据列表
  1129. /// </summary>
  1130. /// <param name="strWhere">条件语句 不包含 where 关键字</param>
  1131. /// <param name="filedOrder">SQL排序 如:id desc</param>
  1132. public DataSet GetList(string strWhere, string filedOrder = "SN desc")
  1133. {
  1134. return this.GetList(0, strWhere, filedOrder);
  1135. }
  1136. /// <summary>
  1137. /// 获得全部数据列表
  1138. /// </summary>
  1139. /// <param name="filedOrder">SQL排序 如:id desc</param>
  1140. public DataSet GetAllList(string filedOrder = "SN desc")
  1141. {
  1142. return this.GetList("", filedOrder);
  1143. }
  1144. /// <summary>
  1145. /// 获得前几行数据
  1146. /// </summary>
  1147. /// <param name="Top">行数</param>
  1148. /// <param name="strWhere">条件 不包含 where 关键字</param>
  1149. /// <param name="filedOrder">SQL排序 如:id asc</param>
  1150. /// <returns></returns>
  1151. public DataSet GetList(int Top, string strWhere, string filedOrder = "SN desc")
  1152. {
  1153. StringBuilder strSql = new StringBuilder();
  1154. strSql.Append("select ");
  1155. if (Top > 0)
  1156. {
  1157. strSql.Append(" top " + Top.ToString());
  1158. }
  1159. strSql.Append(" " + this.TableFieldNameStr + " from " + this.TableName + " ");
  1160. if (strWhere.Trim() != "")
  1161. {
  1162. strSql.Append(" where 1=1 and (" + strWhere + ")");
  1163. }
  1164. strSql.Append(" order by " + filedOrder);
  1165. DataSet ds = DbHelper.Query(cps.ConnectionString, strSql.ToString());
  1166. ds.Tables[0].TableName = this.TableName;
  1167. return ds;
  1168. }
  1169. #endregion
  1170. #region 数据分页
  1171. /// <summary>
  1172. /// 获取记录总数
  1173. /// </summary>
  1174. public int GetRecordCount(string strWhere)
  1175. {
  1176. StringBuilder strSql = new StringBuilder();
  1177. strSql.Append("select count(1) FROM " + this.TableName + " ");
  1178. if (strWhere.Trim() != "")
  1179. {
  1180. strSql.Append(" where 1=1 and (" + strWhere + ")");
  1181. }
  1182. object obj = DbHelper.GetSingle(cps.ConnectionString, strSql.ToString());
  1183. if (obj == null)
  1184. {
  1185. return 0;
  1186. }
  1187. else
  1188. {
  1189. return Convert.ToInt32(obj);
  1190. }
  1191. }
  1192. /// <summary>
  1193. /// 获取分页后总页数
  1194. /// </summary>
  1195. /// <param name="strWhere">筛选条件</param>
  1196. /// <param name="pageSize">页面大小</param>
  1197. /// <returns></returns>
  1198. public int GetByPageCount(string strWhere, int pageSize)
  1199. {
  1200. int pageCount = 0;
  1201. if (pageSize > 0)
  1202. {
  1203. int allCount = GetRecordCount(strWhere);
  1204. pageCount = allCount / pageSize;
  1205. if (pageCount * pageSize < allCount)
  1206. {
  1207. pageCount++;
  1208. }
  1209. }
  1210. return pageCount;
  1211. }
  1212. public int GetByPageCount(string strWhere, int pageSize, ref int totalCount)
  1213. {
  1214. int pageCount = 0;
  1215. if (pageSize > 0)
  1216. {
  1217. int allCount = GetRecordCount(strWhere);
  1218. totalCount = allCount;
  1219. pageCount = allCount / pageSize;
  1220. if (pageCount * pageSize < allCount)
  1221. {
  1222. pageCount++;
  1223. }
  1224. }
  1225. return pageCount;
  1226. }
  1227. /// <summary>
  1228. /// 分页获取数据列表
  1229. /// </summary>
  1230. /// <param name="strWhere">筛选条件</param>
  1231. /// <param name="pageIndex">当前页 不能小于0的整数</param>
  1232. /// <param name="pageSize">页面大小,每页显示条数 不能小于0的整数</param>
  1233. /// <param name="orderby">排序</param>
  1234. /// <returns></returns>
  1235. public DataSet GetListByPage(string strWhere, int pageIndex, int pageSize, string orderby = "SN desc")
  1236. {
  1237. return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
  1238. }
  1239. /// <summary>
  1240. /// 分页获取数据列表
  1241. /// </summary>
  1242. /// <param name="strWhere">筛选条件</param>
  1243. /// <param name="orderby">排序</param>
  1244. /// <param name="pageIndex">当前页</param>
  1245. /// <param name="pageSize">页面大小,每页显示条数</param>
  1246. /// <param name="pageCount">返回总页数</param>
  1247. /// <returns></returns>
  1248. public DataSet GetListByPage(string strWhere, string orderby, int pageIndex, int pageSize, ref int pageCount)
  1249. {
  1250. pageCount = GetByPageCount(strWhere, pageSize);
  1251. return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
  1252. }
  1253. /// <summary>
  1254. /// 分页获取数据列表
  1255. /// </summary>
  1256. /// <param name="strWhere">条件</param>
  1257. /// <param name="orderby">排序</param>
  1258. /// <param name="startIndex">开始index</param>
  1259. /// <param name="endIndex">结束index</param>
  1260. /// <returns></returns>
  1261. public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
  1262. {
  1263. if (!string.IsNullOrEmpty(strWhere.Trim()))
  1264. {
  1265. strWhere = " Where " + strWhere;
  1266. }
  1267. if (!string.IsNullOrEmpty(orderby.Trim()))
  1268. {
  1269. orderby = " Order By " + orderby;
  1270. }
  1271. StringBuilder strSql = new StringBuilder();
  1272. strSql.Append("SELECT * FROM " + this.TableName + " Where ID Not IN ");
  1273. strSql.Append("(Select Top " + startIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
  1274. strSql.Append(" And ID In ");
  1275. strSql.Append("(Select Top " + endIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
  1276. strSql.Append(orderby);
  1277. DataSet ds = DbHelper.Query(cps.ConnectionString, strSql.ToString());
  1278. ds.Tables[0].TableName = this.TableName;
  1279. return ds;
  1280. }
  1281. #endregion
  1282. #region
  1283. /// <summary>
  1284. /// Type转Dbtype
  1285. /// </summary>
  1286. /// <param name="type">变量的Type类型</param>
  1287. /// <returns>对应的DbType类型</returns>
  1288. public static SqlDbType GetDbType(Type type)
  1289. {
  1290. SqlDbType result = SqlDbType.NVarChar;
  1291. if (type.Equals(typeof(Int32)) || type.IsEnum)
  1292. result = SqlDbType.Int;
  1293. else if (type.Equals(typeof(Int64)))
  1294. result = SqlDbType.Int;
  1295. else if (type.Equals(typeof(Double)) || type.Equals(typeof(Double)))
  1296. result = SqlDbType.Decimal;
  1297. else if (type.Equals(typeof(DateTime)))
  1298. result = SqlDbType.DateTime;
  1299. else if (type.Equals(typeof(Boolean)))
  1300. result = SqlDbType.Bit;
  1301. else if (type.Equals(typeof(String)))
  1302. result = SqlDbType.NVarChar;
  1303. else if (type.Equals(typeof(Decimal)))
  1304. result = SqlDbType.Decimal;
  1305. else if (type.Equals(typeof(Byte[])))
  1306. result = SqlDbType.Image;
  1307. else if (type.Equals(typeof(Guid)))
  1308. result = SqlDbType.UniqueIdentifier;
  1309. return result;
  1310. }
  1311. #endregion
  1312. #region 优化sql新增修改的方法
  1313. /// <summary>
  1314. /// 通过泛型实体生成更新语句和对象
  1315. /// </summary>
  1316. /// <typeparam name="T"></typeparam>
  1317. /// <param name="date"></param>
  1318. /// <param name="filterFieldName"></param>
  1319. /// <param name="operators"></param>
  1320. /// <param name="overlookFieldList"></param>
  1321. /// <param name="whereStr"></param>
  1322. /// <param name="type"></param>
  1323. /// <returns></returns>
  1324. public CommandInfo GetUpdateCommandInfoByEntity<T>(T date, string filterFieldName = "ID",
  1325. string operators = "=", string overlookFieldList = "ID", string whereStr = null,
  1326. EffentNextType type = EffentNextType.None)
  1327. {
  1328. var properties = ReflectionHelper.GetProperties(typeof(T));
  1329. StringBuilder strSql = new StringBuilder();
  1330. Dictionary<string, PropertyInfo> dicWhereData = new Dictionary<string, PropertyInfo>();
  1331. strSql.Append("update " + this.TableName + " set ");
  1332. List<SqlParameter> parameterlist = new List<SqlParameter>();
  1333. var ignorePropertyNames = new HashSet<string>();
  1334. var filterPropertyNames = new HashSet<string>();
  1335. ///跳过的字段数据
  1336. if (!string.IsNullOrEmpty(overlookFieldList))
  1337. {
  1338. foreach (var msg in overlookFieldList.Split(','))
  1339. {
  1340. ignorePropertyNames.Add(msg);
  1341. }
  1342. }
  1343. ///用于查询的where条件的数据
  1344. if (!string.IsNullOrEmpty(filterFieldName))
  1345. {
  1346. foreach (var msg in filterFieldName.Split(','))
  1347. {
  1348. filterPropertyNames.Add(msg);
  1349. }
  1350. }
  1351. foreach (var property in properties)
  1352. {
  1353. var ignoreProperty = ignorePropertyNames.SingleOrDefault(x => x.Equals(property.Value.Name, StringComparison.CurrentCultureIgnoreCase));
  1354. var filterProperty = filterPropertyNames.SingleOrDefault(x => x.Equals(property.Value.Name, StringComparison.CurrentCultureIgnoreCase));
  1355. if (filterProperty != null)
  1356. {
  1357. dicWhereData.Add(property.Key, property.Value);
  1358. }
  1359. if (ignoreProperty != null)
  1360. continue;
  1361. var propertyType = ReflectionHelper.GetPropertyType(property.Value);
  1362. if (propertyType.Name.ToLower().IndexOf("list") != -1)
  1363. {
  1364. continue;
  1365. }
  1366. var propertyValue = ReflectionHelper.GetPropertyValue(date, property.Value);
  1367. if (propertyValue == null)
  1368. {
  1369. continue;
  1370. }
  1371. else if (propertyValue != null && propertyType.Name.ToLower().Equals("datetime") && propertyValue.ToString().Contains("0001"))
  1372. {
  1373. continue;
  1374. }
  1375. else if (propertyValue != null && propertyType.Name.ToLower().Equals("datetime") && Convert.ToDateTime(propertyValue) <= DbHelper.GetNullDateTime())
  1376. {
  1377. continue;
  1378. }
  1379. // ColumnAction(property.Value.Name, propertyValue, propertyType, DataTypes.Object, 0);
  1380. if (propertyValue != null)
  1381. {
  1382. strSql.Append("" + property.Key + "=@" + property.Key + ",");
  1383. SqlParameter parameter = new SqlParameter("@" + property.Key, propertyValue);
  1384. parameterlist.Add(parameter);
  1385. }
  1386. }
  1387. //去除结尾多余的“,”
  1388. if (strSql.ToString().LastIndexOf(',') == strSql.Length - 1)
  1389. {
  1390. strSql.Remove(strSql.Length - 1, 1);
  1391. }
  1392. if (whereStr == null)
  1393. {
  1394. if (filterFieldName != "" && operators != "")
  1395. {
  1396. string strWhere = "";
  1397. foreach (var property in dicWhereData)
  1398. {
  1399. if (!string.IsNullOrEmpty(strWhere))
  1400. {
  1401. strWhere += " and ";
  1402. }
  1403. strWhere += property.Key + operators + "@" + property.Key;
  1404. var propertyValue = ReflectionHelper.GetPropertyValue(date, property.Value);
  1405. parameterlist.Add(new SqlParameter("@" + property.Key, propertyValue));
  1406. }
  1407. strSql.Append(" where " + strWhere);
  1408. }
  1409. }
  1410. else
  1411. {
  1412. strSql.Append(" where 1=1 and (" + whereStr + ")");
  1413. }
  1414. SqlParameter[] parameters = parameterlist.ToArray();
  1415. return new CommandInfo(strSql.ToString(), parameters, type);
  1416. }
  1417. /// <summary>
  1418. /// 通过泛型方法生成新增对象信息
  1419. /// </summary>
  1420. /// <typeparam name="T"></typeparam>
  1421. /// <param name="date"></param>
  1422. /// <param name="overlookFieldList"></param>
  1423. /// <param name="type"></param>
  1424. /// <returns></returns>
  1425. public CommandInfo GetAddCommandInfoByEntity<T>(T date, string overlookFieldList = "ID", EffentNextType type = EffentNextType.None)
  1426. {
  1427. StringBuilder strSql = new StringBuilder();
  1428. var properties = ReflectionHelper.GetProperties(typeof(T));
  1429. StringBuilder tempFieldNameString = new StringBuilder();
  1430. StringBuilder tempValueVariable = new StringBuilder();
  1431. List<SqlParameter> parameterlist = new List<SqlParameter>();
  1432. var ignorePropertyNames = new HashSet<string>();
  1433. if (!string.IsNullOrEmpty(overlookFieldList))
  1434. {
  1435. foreach (var msg in overlookFieldList.Split(','))
  1436. {
  1437. ignorePropertyNames.Add(msg);
  1438. }
  1439. }
  1440. foreach (var property in properties)
  1441. {
  1442. var ignoreProperty = ignorePropertyNames.SingleOrDefault(x => x.Equals(property.Value.Name, StringComparison.CurrentCultureIgnoreCase));
  1443. if (ignoreProperty != null)
  1444. continue;
  1445. var propertyType = ReflectionHelper.GetPropertyType(property.Value);
  1446. if (propertyType.Name.ToLower().IndexOf("list") != -1)
  1447. {
  1448. continue;
  1449. }
  1450. var propertyValue = ReflectionHelper.GetPropertyValue(date, property.Value);
  1451. if (propertyValue == null)
  1452. {
  1453. continue;
  1454. }
  1455. if (propertyValue != null && propertyType.Name.ToLower().Equals("datetime") && propertyValue.ToString().Contains("0001"))
  1456. {
  1457. continue;
  1458. }
  1459. if (propertyValue != null && propertyType.Name.ToLower().Equals("datetime") && Convert.ToDateTime(propertyValue) <= DbHelper.GetNullDateTime())
  1460. {
  1461. continue;
  1462. }
  1463. else if (property.Key.ToLower() == "id" && propertyType == typeof(long) && (propertyValue == null || Convert.ToInt64(propertyValue) == 0))
  1464. {
  1465. propertyValue = DbHelper.GenerateId();
  1466. }
  1467. // ColumnAction(property.Value.Name, propertyValue, propertyType, DataTypes.Object, 0);
  1468. if (propertyValue != null)
  1469. {
  1470. tempFieldNameString.Append("" + property.Key + ",");
  1471. tempValueVariable.Append("@" + property.Key + ",");
  1472. SqlParameter parameter = new SqlParameter("@" + property.Key, propertyValue);
  1473. parameterlist.Add(parameter);
  1474. }
  1475. }
  1476. if (tempFieldNameString.ToString().LastIndexOf(',') == tempFieldNameString.Length - 1)
  1477. {
  1478. tempFieldNameString.Remove(tempFieldNameString.Length - 1, 1);
  1479. }
  1480. //去除结尾多余的“,”
  1481. if (tempValueVariable.ToString().LastIndexOf(',') == tempValueVariable.Length - 1)
  1482. {
  1483. tempValueVariable.Remove(tempValueVariable.Length - 1, 1);
  1484. }
  1485. strSql.Append("insert into " + this.TableName + "(");
  1486. strSql.Append("" + tempFieldNameString.ToString() + ")");
  1487. strSql.Append(" values (");
  1488. strSql.Append("" + tempValueVariable.ToString() + ")");
  1489. strSql.Append(";select @@IDENTITY");
  1490. SqlParameter[] parameters = parameterlist.ToArray();
  1491. return new CommandInfo(strSql.ToString(), parameters, type);
  1492. }
  1493. public object GetAddCommandInfoByEntity<T>(T date, string overlookFieldList = "ID")
  1494. {
  1495. return GetAddCommandInfoByEntity(date, overlookFieldList, EffentNextType.None);
  1496. }
  1497. public object GetUpdateCommandInfoByEntity<T>(T date, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
  1498. {
  1499. return GetUpdateCommandInfoByEntity(date, filterFieldName, operators, overlookFieldList, whereStr, EffentNextType.None);
  1500. }
  1501. #endregion
  1502. #region 17-04-20 添加
  1503. /// <summary>
  1504. /// 将数据列表转泛型数据
  1505. /// </summary>
  1506. /// <typeparam name="T">泛型类型</typeparam>
  1507. /// <param name="dt">数据表数据</param>
  1508. /// <returns>数据集</returns>
  1509. public T GetListToEntity<T>(DataTable dt)
  1510. {
  1511. var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
  1512. T entityList = Activator.CreateInstance<T>();
  1513. entityList = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(dt), jSetting);
  1514. return entityList;
  1515. }
  1516. /// <summary>
  1517. /// 将sql查询出来的数据转成泛型列表
  1518. /// </summary>
  1519. /// <typeparam name="T">泛型类型</typeparam>
  1520. /// <param name="sql">sql数据查询字符串</param>
  1521. /// <returns>数据集</returns>
  1522. public T GetListToEntity<T>(string sql)
  1523. {
  1524. DataTable dt = GetViewDataSetByCustomer(sql).Tables[0];
  1525. return GetListToEntity<T>(dt);
  1526. }
  1527. #endregion
  1528. public DataSet GetListByPage(string strWhere, string orderby, int pageIndex, int pageSize, ref int pageCount, ref int Totalcount)
  1529. {
  1530. pageCount = GetByPageCount(strWhere, pageSize, ref Totalcount);
  1531. return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
  1532. }
  1533. /// <summary>
  1534. /// 获得数据列表
  1535. /// </summary>
  1536. /// <param name="dt">DataTable</param>
  1537. public List<object> GetDataTableToOblList(DataTable dt)
  1538. {
  1539. List<object> modelList = new List<object>();
  1540. int rowsCount = dt.Rows.Count;
  1541. if (rowsCount > 0)
  1542. {
  1543. object modelObj;
  1544. for (int n = 0; n < rowsCount; n++)
  1545. {
  1546. modelObj = DataRowToModelObject(dt.Rows[n]);
  1547. if (modelObj != null)
  1548. {
  1549. modelList.Add(modelObj);
  1550. }
  1551. }
  1552. }
  1553. return modelList;
  1554. }
  1555. /// <summary>
  1556. /// 获得数据列表
  1557. /// </summary>
  1558. /// <typeparam name="T"></typeparam>
  1559. /// <param name="dt"></param>
  1560. /// <returns></returns>
  1561. public List<T> GetDataTableToOblList<T>(DataTable dt)
  1562. {
  1563. List<T> modelList = new List<T>();
  1564. int rowsCount = dt.Rows.Count;
  1565. if (rowsCount > 0)
  1566. {
  1567. T modelObj;
  1568. for (int n = 0; n < rowsCount; n++)
  1569. {
  1570. modelObj = (T)DataRowToModelObject(dt.Rows[n]);
  1571. if (modelObj != null)
  1572. {
  1573. modelList.Add(modelObj);
  1574. }
  1575. }
  1576. }
  1577. return modelList;
  1578. }
  1579. }
  1580. #region 反射助手,用于泛型实体的反射操作
  1581. internal static class ReflectionHelper
  1582. {
  1583. private static readonly ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> _cachedProperties = new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
  1584. /// <summary>
  1585. /// 得到实体反射的表达式
  1586. /// </summary>
  1587. /// <typeparam name="T"></typeparam>
  1588. /// <param name="expression"></param>
  1589. /// <returns></returns>
  1590. public static string GetPropertyNameFromExpression<T>(Expression<Func<T, object>> expression)
  1591. {
  1592. string propertyPath = null;
  1593. if (expression.Body is UnaryExpression)
  1594. {
  1595. var unaryExpression = (UnaryExpression)expression.Body;
  1596. if (unaryExpression.NodeType == ExpressionType.Convert)
  1597. propertyPath = unaryExpression.Operand.ToString();
  1598. }
  1599. if (propertyPath == null)
  1600. propertyPath = expression.Body.ToString();
  1601. propertyPath = propertyPath.Replace(expression.Parameters[0] + ".", string.Empty);
  1602. return propertyPath;
  1603. }
  1604. public static List<string> GetPropertyNamesFromExpressions<T>(Expression<Func<T, object>>[] expressions)
  1605. {
  1606. var propertyNames = new List<string>();
  1607. foreach (var expression in expressions)
  1608. {
  1609. var propertyName = GetPropertyNameFromExpression(expression);
  1610. propertyNames.Add(propertyName);
  1611. }
  1612. return propertyNames;
  1613. }
  1614. public static object GetPropertyValue(object item, PropertyInfo property)
  1615. {
  1616. var value = property.GetValue(item, null);
  1617. return value;
  1618. }
  1619. public static object GetPropertyValue(object item, string propertyName)
  1620. {
  1621. PropertyInfo property;
  1622. foreach (var part in propertyName.Split('.'))
  1623. {
  1624. if (item == null)
  1625. return null;
  1626. var type = item.GetType();
  1627. property = type.GetProperty(part);
  1628. if (property == null)
  1629. return null;
  1630. item = GetPropertyValue(item, property);
  1631. }
  1632. return item;
  1633. }
  1634. public static object GetPropertyValueDynamic(object item, string name)
  1635. {
  1636. var dictionary = (IDictionary<string, object>)item;
  1637. return dictionary[name];
  1638. }
  1639. public static Dictionary<string, PropertyInfo> GetProperties(Type type)
  1640. {
  1641. var properties = _cachedProperties.GetOrAdd(type, BuildPropertyDictionary);
  1642. return properties;
  1643. }
  1644. private static Dictionary<string, PropertyInfo> BuildPropertyDictionary(Type type)
  1645. {
  1646. var result = new Dictionary<string, PropertyInfo>();
  1647. var properties = type.GetProperties();
  1648. foreach (var property in properties)
  1649. {
  1650. result.Add(property.Name.ToLower(), property);
  1651. }
  1652. return result;
  1653. }
  1654. public static bool IsList(object item)
  1655. {
  1656. if (item is ICollection)
  1657. return true;
  1658. return false;
  1659. }
  1660. public static bool IsNullable(PropertyInfo property)
  1661. {
  1662. if (property.PropertyType.IsGenericType &&
  1663. property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
  1664. return true;
  1665. return false;
  1666. }
  1667. /// <summary>
  1668. /// Includes a work around for getting the actual type of a Nullable type.
  1669. /// </summary>
  1670. public static Type GetPropertyType(PropertyInfo property)
  1671. {
  1672. if (IsNullable(property))
  1673. return property.PropertyType.GetGenericArguments()[0];
  1674. return property.PropertyType;
  1675. }
  1676. public static object GetDefault(Type type)
  1677. {
  1678. if (type.IsValueType)
  1679. return Activator.CreateInstance(type);
  1680. return null;
  1681. }
  1682. public static bool IsBasicClrType(Type type)
  1683. {
  1684. if (type.IsEnum
  1685. || type.IsPrimitive
  1686. || type.IsValueType
  1687. || type == typeof(string)
  1688. || type == typeof(DateTime))
  1689. return true;
  1690. return false;
  1691. }
  1692. public static bool IsCustomEntity<T>()
  1693. {
  1694. var type = typeof(T);
  1695. if (type.IsClass && Type.GetTypeCode(type) == TypeCode.Object)
  1696. return true;
  1697. return false;
  1698. }
  1699. }
  1700. #endregion
  1701. }