BaseDAL.cs 73 KB

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