BaseDataOperate.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Data.SqlClient;
  5. using System.Collections.Generic;
  6. using LYFZ.Helper;
  7. namespace LYFZ.WeixinServiceDate.DAL
  8. {
  9. /// <summary>
  10. /// 数据操作抽象基类
  11. /// </summary>
  12. public abstract class BaseDataOperate : LYFZ.WeixinServiceDate.StandardInterface.IBaseDataLaye
  13. {
  14. #region 属性
  15. /// <summary>
  16. /// 数据表模型对象
  17. /// </summary>
  18. public abstract object ObjModel { get; }
  19. /// <summary>
  20. /// 数据表名
  21. /// </summary>
  22. public abstract string TableName { get; set; }
  23. /// <summary>
  24. /// 过期属性请不要使用 数据表字段名字符串,字段名以“,”号分隔
  25. /// </summary>
  26. public abstract string TableFieldNameString { get; set; }
  27. string _TableFieldNameStr = "";
  28. /// <summary>
  29. /// 数据表字段名字符串,字段名以“,”号分隔
  30. /// </summary>
  31. public string TableFieldNameStr
  32. {
  33. get
  34. {
  35. if (this._TableFieldNameStr.Trim() == "")
  36. {
  37. for (int i = 0; i < this.TableFieldNames.Length; i++)
  38. {
  39. if (this._TableFieldNameStr.Trim() == "")
  40. {
  41. this._TableFieldNameStr += "[" + this.TableFieldNames[i] + "]";
  42. }
  43. else
  44. {
  45. this._TableFieldNameStr += ",[" + this.TableFieldNames[i] + "]";
  46. }
  47. }
  48. }
  49. if (this._TableFieldNameStr.Trim() == "")
  50. {
  51. this._TableFieldNameStr = "*";
  52. }
  53. return this._TableFieldNameStr;
  54. }
  55. set { this._TableFieldNameStr = value; }
  56. }
  57. /// <summary>
  58. /// 数据表字段名数组
  59. /// </summary>
  60. public string[] TableFieldNames
  61. {
  62. get
  63. {
  64. System.Reflection.PropertyInfo[] propertyInfos = this.PropertyInfos;
  65. string[] fieldNames = new string[propertyInfos.Length];
  66. for (int i = 0; i < propertyInfos.Length; i++)
  67. {
  68. if (!propertyInfos[i].IsSpecialName)
  69. {
  70. System.Reflection.PropertyInfo propertyInfo = propertyInfos[i];
  71. fieldNames[i] = propertyInfo.Name;
  72. }
  73. }
  74. // System.Reflection.PropertyInfo propertyInfo = ObjModel.GetType().GetProperty(propertyInfos[0].Name);
  75. return fieldNames;//this.TableFieldNameString.Replace("[", "").Replace("]", "").Replace(" ", "").Split(',');
  76. }
  77. }
  78. /// <summary>
  79. /// 获取要查询的当前表的字段集合字符串 用","号分隔
  80. /// </summary>
  81. /// <param name="filters"></param>
  82. /// <param name="isContain">如果为true 表示只返回 filters 集合中的字段 如果为false 表示返回排除 filters 集合中的字段以外的所有字段</param>
  83. /// <returns></returns>
  84. public string GetQueryTableFieldNames(string[] filters = null, bool isContain = false)
  85. {
  86. List<string> tempFilters = new List<string>();
  87. if (filters != null)
  88. {
  89. tempFilters.AddRange(filters);
  90. }
  91. System.Reflection.PropertyInfo[] propertyInfos = this.PropertyInfos;
  92. string fieldNames = "";
  93. for (int i = 0; i < propertyInfos.Length; i++)
  94. {
  95. if (!propertyInfos[i].IsSpecialName)
  96. {
  97. System.Reflection.PropertyInfo propertyInfo = propertyInfos[i];
  98. if ((!tempFilters.Contains(propertyInfo.Name) && !isContain) || (tempFilters.Contains(propertyInfo.Name) && isContain))
  99. {
  100. if (fieldNames.Trim().Length <= 0)
  101. {
  102. fieldNames = "[" + propertyInfo.Name + "]";
  103. }
  104. else
  105. {
  106. fieldNames += ",[" + propertyInfo.Name + "]";
  107. }
  108. }
  109. }
  110. }
  111. return fieldNames;
  112. }
  113. /// <summary>
  114. /// 获取属性集合
  115. /// </summary>
  116. public System.Reflection.PropertyInfo[] PropertyInfos
  117. {
  118. get
  119. {
  120. List<System.Reflection.PropertyInfo> list = new List<System.Reflection.PropertyInfo>();
  121. System.Reflection.PropertyInfo[] infos = ObjModel.GetType().GetProperties();
  122. foreach (System.Reflection.PropertyInfo info in infos)
  123. {
  124. if (info.CanWrite)
  125. {
  126. list.Add(info);
  127. }
  128. }
  129. return list.ToArray();
  130. }
  131. }
  132. #endregion
  133. #region 检查记录
  134. /// <summary>
  135. /// 是否存在该记录
  136. /// </summary>
  137. public bool Exists(int id)
  138. {
  139. return this.Exists("ID", id);
  140. }
  141. /// <summary>
  142. /// 根据筛选条件判断是否存在该记录
  143. /// </summary>
  144. /// <param name="filterFieldName">筛选字段名</param>
  145. /// <param name="filterValue">筛选值</param>
  146. /// <returns></returns>
  147. public bool Exists(string filterFieldName, object filterValue)
  148. {
  149. if (filterFieldName != "" && filterValue != null)
  150. {
  151. StringBuilder strSql = new StringBuilder();
  152. strSql.Append("select count(1) from " + this.TableName + " ");
  153. strSql.Append(" where 1=1 and " + filterFieldName + "=@" + filterFieldName);
  154. SqlParameter[] parameters = {
  155. new SqlParameter("@"+filterFieldName, filterValue)
  156. };
  157. return SQLHelper.Exists(strSql.ToString(), parameters);
  158. }
  159. else
  160. {
  161. return false;
  162. }
  163. }
  164. /// <summary>
  165. /// 根据筛选条件判断是否存在该记录
  166. /// </summary>
  167. /// <param name="filterFieldName">筛选字段名</param>
  168. /// <param name="filterValue">筛选值</param>
  169. /// <returns></returns>
  170. public bool Exists(string whereString)
  171. {
  172. if (whereString.Trim().Length > 0)
  173. {
  174. StringBuilder strSql = new StringBuilder();
  175. strSql.Append("select count(1) from " + this.TableName + " ");
  176. strSql.Append(" where " + whereString);
  177. return SQLHelper.Exists(strSql.ToString());
  178. }
  179. else
  180. {
  181. return false;
  182. }
  183. }
  184. /// <summary>
  185. /// 判断是否存某个字段
  186. /// </summary>
  187. /// <param name="columnName">列名称</param>
  188. /// <returns>是否存在</returns>
  189. public bool ColumnExists(string columnName)
  190. {
  191. return SQLHelper.ColumnExists(this.TableName, columnName);
  192. }
  193. #endregion
  194. #region 公共方法
  195. /// <summary>
  196. /// 获取最大ID()
  197. /// </summary>
  198. /// <returns></returns>
  199. public int GetMaxID()
  200. {
  201. return this.GetMaxID("ID");
  202. }
  203. /// <summary>
  204. /// 获取某字段最大值(获取字段必须为数字类型)
  205. /// </summary>
  206. /// <param name="FieldName">字段名</param>
  207. /// <returns></returns>
  208. public int GetMaxID(string FieldName)
  209. {
  210. return SQLHelper.GetMaxID(FieldName, this.TableName);
  211. }
  212. /// <summary>
  213. /// 检查CLR功能是否开启
  214. /// </summary>
  215. /// <returns></returns>
  216. public static bool CheckingCLR()
  217. {
  218. try
  219. {
  220. DataSet cds = LYFZ.Helper.SQLHelper.Query("SELECT TOP 1 [Cus_Name] ,[Cus_Name_py],[Cus_Sex_cs] FROM [dbo].[Vw_AggregationCustomer]");
  221. return true;
  222. }
  223. catch
  224. {
  225. return false;
  226. }
  227. }
  228. #endregion
  229. #region 判断方法
  230. /// <summary>
  231. /// 获取是否为忽略字段 返回 true 表示忽略 false 不忽略
  232. /// </summary>
  233. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  234. /// <param name="fieldName">要判断的字段名</param>
  235. /// <returns></returns>
  236. public bool IsOverlookFiel(string overlookFieldList, string fieldName)
  237. {
  238. bool bl = false;
  239. string[] tempOverlookFieldList = overlookFieldList.Split(',');
  240. for (int i = 0; i < tempOverlookFieldList.Length; i++)
  241. {
  242. if (tempOverlookFieldList[i].Trim().ToLower() == fieldName.Trim().ToLower())
  243. {
  244. bl = true;
  245. break;
  246. }
  247. }
  248. return bl;
  249. }
  250. #endregion
  251. #region 增加数据
  252. /// <summary>
  253. /// 增加一条数据
  254. /// </summary>
  255. /// <param name="model">Model对象</param>
  256. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  257. /// <returns></returns>
  258. public bool Add(object model, string overlookFieldList = "ID")
  259. {
  260. LYFZ.Helper.CommandInfo comdInfo = this.GetAddCommandInfo(model, overlookFieldList);
  261. int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  262. if (rows > 0)
  263. {
  264. return true;
  265. }
  266. else
  267. {
  268. return false;
  269. }
  270. }
  271. /// <summary>
  272. /// 获取插入数CommandInfo对象
  273. /// </summary>
  274. /// <param name="model"></param>
  275. /// <param name="overlookFieldList">要忽略的字段集合 多个字段名有“,”号分隔</param>
  276. /// <returns></returns>
  277. public object GetAddCommandInfoObject(object model, string overlookFieldList = "ID")
  278. {
  279. return this.GetAddCommandInfo(model, overlookFieldList);
  280. }
  281. /// <summary>
  282. /// 获取插入数CommandInfo对象
  283. /// </summary>
  284. /// <param name="model"></param>
  285. /// <param name="overlookFieldList">要忽略的字段集合 多个字段名有“,”号分隔</param>
  286. /// <param name="type"></param>
  287. /// <returns></returns>
  288. public LYFZ.Helper.CommandInfo GetAddCommandInfo(object model, string overlookFieldList = "ID", EffentNextType type = EffentNextType.None)
  289. {
  290. StringBuilder strSql = new StringBuilder();
  291. StringBuilder tempFieldNameString = new StringBuilder();
  292. StringBuilder tempValueVariable = new StringBuilder();
  293. List<SqlParameter> parameterlist = new List<SqlParameter>();
  294. for (int i = 0; i < PropertyInfos.Length; i++)
  295. {
  296. try
  297. {
  298. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  299. string tempFieldName = propertyInfo.Name;
  300. object tempValue = propertyInfo.GetValue(model, null);
  301. Type tempType = propertyInfo.PropertyType;
  302. if (!IsOverlookFiel(overlookFieldList, tempFieldName) || (tempFieldName.ToLower() == "id" && tempType == typeof(long)))
  303. {
  304. try
  305. {
  306. if (tempType.Name == "DateTime")
  307. {
  308. if (tempValue.ToString().Contains("0001"))
  309. {
  310. tempValue = null;
  311. }
  312. try
  313. {
  314. DateTime tempTime;
  315. if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
  316. {
  317. tempValue = null;
  318. }
  319. else if (tempTime <= LYFZ.WinAPI.CustomPublicMethod.GetNullDateTime())
  320. {
  321. tempValue = null;
  322. }
  323. }
  324. catch { tempValue = null; }
  325. }
  326. else if (tempFieldName.ToLower() == "id" && tempType == typeof(long) && (tempValue == null || Convert.ToInt64(tempValue) == 0))
  327. {
  328. tempValue = LYFZ.WinAPI.CustomPublicMethod.GenerateId();
  329. }
  330. if (tempValue != null)
  331. {
  332. tempFieldNameString.Append("" + tempFieldName + ",");
  333. tempValueVariable.Append("@" + tempFieldName + ",");
  334. SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
  335. parameterlist.Add(parameter);
  336. }
  337. }
  338. catch { }
  339. }
  340. }
  341. catch { }
  342. }
  343. //去除结尾多余的“,”
  344. if (tempFieldNameString.ToString().LastIndexOf(',') == tempFieldNameString.Length - 1)
  345. {
  346. tempFieldNameString.Remove(tempFieldNameString.Length - 1, 1);
  347. }
  348. //去除结尾多余的“,”
  349. if (tempValueVariable.ToString().LastIndexOf(',') == tempValueVariable.Length - 1)
  350. {
  351. tempValueVariable.Remove(tempValueVariable.Length - 1, 1);
  352. }
  353. strSql.Append("insert into " + this.TableName + "(");
  354. strSql.Append("" + tempFieldNameString.ToString() + ")");
  355. strSql.Append(" values (");
  356. strSql.Append("" + tempValueVariable.ToString() + ")");
  357. strSql.Append(";select @@IDENTITY");
  358. SqlParameter[] parameters = parameterlist.ToArray();
  359. return new CommandInfo(strSql.ToString(), parameters, type);
  360. }
  361. #endregion
  362. #region 更新数据
  363. /// <summary>
  364. /// 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  365. /// </summary>
  366. /// <param name="model">Model对象</param>
  367. /// <param name="filterFieldName">筛选字段名称</param>
  368. /// <param name="operators">SQL筛选运算符号</param>
  369. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  370. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  371. /// <returns></returns>
  372. protected bool InsideUpdate(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
  373. {
  374. LYFZ.Helper.CommandInfo comdInfo = GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
  375. int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  376. if (rows > 0)
  377. {
  378. return true;
  379. }
  380. else
  381. {
  382. return false;
  383. }
  384. }
  385. /// <summary>
  386. /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  387. /// </summary>
  388. /// <param name="model">Model对象</param>
  389. /// <param name="filterFieldName">筛选字段名称</param>
  390. /// <param name="operators">SQL筛选运算符号</param>
  391. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  392. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  393. public object GetUpdateCommandInfoObject(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
  394. {
  395. return this.GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
  396. }
  397. /// <summary>
  398. /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  399. /// </summary>
  400. /// <param name="model">Model对象</param>
  401. /// <param name="filterFieldName">筛选字段名称</param>
  402. /// <param name="operators">SQL筛选运算符号</param>
  403. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  404. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  405. /// <param name="type"></param>
  406. public LYFZ.Helper.CommandInfo GetUpdateCommandInfo(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null, EffentNextType type = EffentNextType.None)
  407. {
  408. StringBuilder strSql = new StringBuilder();
  409. strSql.Append("update " + this.TableName + " set ");
  410. List<SqlParameter> parameterlist = new List<SqlParameter>();
  411. overlookFieldList += "," + filterFieldName;
  412. for (int i = 0; i < PropertyInfos.Length; i++)
  413. {
  414. try
  415. {
  416. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  417. string tempFieldName = propertyInfo.Name;//字段名
  418. object tempValue = propertyInfo.GetValue(model, null);//对应字段值
  419. //如果字段名不为忽略字段则进行处理
  420. if (!IsOverlookFiel(overlookFieldList, tempFieldName))
  421. {
  422. try
  423. {
  424. if (tempValue.GetType() == typeof(DateTime))
  425. {
  426. DateTime tempTime;
  427. try
  428. {
  429. if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
  430. {
  431. tempValue = null;
  432. }
  433. else if (tempTime <= LYFZ.WinAPI.CustomPublicMethod.GetNullDateTime())
  434. {
  435. tempValue = null;
  436. }
  437. else
  438. if (tempTime.Year == Convert.ToDateTime("0001-01-01").Year || tempTime.Year == Convert.ToDateTime("1753-01-01").Year)
  439. {
  440. tempValue = null;
  441. }
  442. }
  443. catch { tempValue = null; }
  444. }
  445. }
  446. catch { }
  447. strSql.Append("" + tempFieldName + "=@" + tempFieldName + ",");
  448. SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
  449. parameterlist.Add(parameter);
  450. }
  451. }
  452. catch { }
  453. }
  454. //去除结尾多余的“,”
  455. if (strSql.ToString().LastIndexOf(',') == strSql.Length - 1)
  456. {
  457. strSql.Remove(strSql.Length - 1, 1);
  458. }
  459. if (whereStr == null)
  460. {
  461. if (filterFieldName != "" && operators != "")
  462. {
  463. try
  464. {
  465. object FilterFieldValue = LYFZ.WinAPI.CustomPublicMethod.GetPropertyValue(model, filterFieldName);// model.GetType().GetProperty(filterFieldName).GetValue(model, null);
  466. strSql.Append(" where " + filterFieldName + operators + "@" + filterFieldName);
  467. parameterlist.Add(new SqlParameter("@" + filterFieldName, FilterFieldValue));
  468. }
  469. catch
  470. {
  471. }
  472. }
  473. }
  474. else
  475. {
  476. strSql.Append(" where 1=1 and (" + whereStr + ")");
  477. }
  478. SqlParameter[] parameters = parameterlist.ToArray();
  479. return new CommandInfo(strSql.ToString(), parameters, type);
  480. }
  481. /// <summary>
  482. /// 根据筛选字段和SQL筛选运算符号更新数据
  483. /// </summary>
  484. /// <param name="model">Model对象</param>
  485. /// <param name="filterFieldName">筛选字段名称</param>
  486. /// <param name="operators">SQL筛选运算符号</param>
  487. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  488. /// <returns></returns>
  489. public bool Update(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID")
  490. {
  491. return InsideUpdate(model, filterFieldName, operators, overlookFieldList);
  492. }
  493. /// <summary>
  494. /// 根据Wher条件更新数据 不建义使用此方法
  495. /// </summary>
  496. /// <param name="model">Model对象</param>
  497. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  498. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  499. /// <returns></returns>
  500. public bool Update(object model, string whereStr, string overlookFieldList = "ID")
  501. {
  502. return InsideUpdate(model, overlookFieldList: overlookFieldList, whereStr: whereStr);
  503. }
  504. #endregion
  505. #region 删除数据
  506. /// <summary>
  507. /// 根据筛选字段或where条件删除数据
  508. /// </summary>
  509. /// <param name="filterFieldName">筛选字段名</param>
  510. /// <param name="operators">筛选SQL运算符</param>
  511. /// <param name="filterValue">筛选值</param>
  512. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  513. /// <returns></returns>
  514. protected bool InsideDelete(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
  515. {
  516. LYFZ.Helper.CommandInfo comdInfo = GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
  517. int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  518. if (rows > 0)
  519. {
  520. return true;
  521. }
  522. else
  523. {
  524. return false;
  525. }
  526. }
  527. /// <summary>
  528. /// 根据筛选字段或where条件删除数据
  529. /// </summary>
  530. /// <param name="filterFieldName">筛选字段名</param>
  531. /// <param name="operators">筛选SQL运算符</param>
  532. /// <param name="filterValue">筛选值</param>
  533. /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
  534. /// <returns></returns>
  535. public object GetDeleteCommandInfoObject(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
  536. {
  537. return this.GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
  538. }
  539. /// <summary>
  540. /// 根据筛选字段或where条件删除数据
  541. /// </summary>
  542. /// <param name="filterFieldName">筛选字段名</param>
  543. /// <param name="operators">筛选SQL运算符</param>
  544. /// <param name="filterValue">筛选值</param>
  545. /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
  546. /// <param name="type"></param>
  547. /// <returns></returns>
  548. public LYFZ.Helper.CommandInfo GetDeleteCommandInfo(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null, EffentNextType type = EffentNextType.None)
  549. {
  550. if (filterValue == null) { filterValue = ""; }
  551. List<SqlParameter> parameterlist = new List<SqlParameter>();
  552. StringBuilder strSql = new StringBuilder();
  553. strSql.Append("delete from " + this.TableName + " ");
  554. if (whereStr == null)
  555. {
  556. strSql.Append(" where " + filterFieldName + "" + operators + "@" + filterFieldName + "");
  557. parameterlist.Add(new SqlParameter("@" + filterFieldName, filterValue));
  558. }
  559. else
  560. {
  561. strSql.Append(" where ID>@ID and (" + whereStr + ")");
  562. parameterlist.Add(new SqlParameter("@ID", Convert.ToInt32(0)));
  563. }
  564. SqlParameter[] parameters = parameterlist.ToArray();
  565. return new CommandInfo(strSql.ToString(), parameters, type);
  566. }
  567. /// <summary>
  568. /// 删除一条数据
  569. /// </summary>
  570. /// <param name="ID">id</param>
  571. public bool Delete(long ID)
  572. {
  573. return this.InsideDelete(filterValue: ID);
  574. }
  575. /// <summary>
  576. /// 删除一条数据
  577. /// </summary>
  578. /// <param name="ID">id</param>
  579. public bool Delete(int ID)
  580. {
  581. return this.InsideDelete(filterValue: ID);
  582. }
  583. /// <summary>
  584. /// 根据筛选字段删除数据
  585. /// </summary>
  586. /// <param name="filterValue">筛选值</param>
  587. /// <param name="filterFieldName">筛选字段名</param>
  588. /// <param name="operators">筛选SQL运算符</param>
  589. /// <returns></returns>
  590. public bool Delete(object filterValue, string filterFieldName = "ID", string operators = "=")
  591. {
  592. return this.InsideDelete(filterFieldName, operators, filterValue);
  593. }
  594. /// <summary>
  595. /// 根据where条件删除数据 不建义使用此方法
  596. /// </summary>
  597. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  598. /// <returns></returns>
  599. public bool Delete(string whereStr)
  600. {
  601. return this.InsideDelete(whereStr: whereStr);
  602. }
  603. /// <summary>
  604. /// 批量删除数据
  605. /// </summary>
  606. /// <param name="filterFieldName">筛选字段名</param>
  607. /// <param name="valueList">值列表,多个值用“,”分隔,字符串值用“'”号包含</param>
  608. public bool DeleteList(string filterFieldName, string valueList)
  609. {
  610. if (filterFieldName != "" && valueList != "")
  611. {
  612. StringBuilder strSql = new StringBuilder();
  613. strSql.Append("delete from " + this.TableName + " ");
  614. strSql.Append(" where " + filterFieldName + " in (" + valueList + ") ");
  615. int rows = SQLHelper.ExecuteSql(strSql.ToString());
  616. if (rows > 0)
  617. {
  618. return true;
  619. }
  620. else
  621. {
  622. return false;
  623. }
  624. }
  625. else
  626. {
  627. return false;
  628. }
  629. }
  630. /// <summary>
  631. /// 按ID列表批量删除数据
  632. /// </summary>
  633. /// <param name="idList">ID列表,多个用“,”分隔</param>
  634. public bool DeleteList(string idList)
  635. {
  636. return this.DeleteList("ID", idList);
  637. }
  638. #endregion
  639. #region 查询数据
  640. /// <summary>
  641. /// 得到一个Object对象实体
  642. /// </summary>
  643. /// <param name="ID"></param>
  644. /// <returns></returns>
  645. public object GetModelObject(long ID)
  646. {
  647. return DataRowToModelObject(GetDataRow(ID));
  648. }
  649. /// <summary>
  650. /// 得到一个Object对象实体
  651. /// </summary>
  652. /// <param name="ID"></param>
  653. /// <returns></returns>
  654. public object GetModelObject(int ID)
  655. {
  656. return DataRowToModelObject(GetDataRow(ID));
  657. }
  658. /// <summary>
  659. /// 根据筛选条件获取一条数据Model对象
  660. /// </summary>
  661. /// <param name="filterFieldName">筛选条件字段名</param>
  662. /// <param name="filterValue">值</param>
  663. /// <returns></returns>
  664. public object GetModelObject(string filterFieldName, object filterValue)
  665. {
  666. return DataRowToModelObject(GetDataRow(filterFieldName, filterValue));
  667. }
  668. /// <summary>
  669. /// 根据筛选条件获取一条数据Model对象
  670. /// </summary>
  671. /// <param name="filterFieldName">筛选条件字段名</param>
  672. /// <param name="filterValue">值</param>
  673. /// <param name="operators">SQL筛选运算符号</param>
  674. /// <returns></returns>
  675. public object GetModelObject(string filterFieldName, object filterValue, string operators)
  676. {
  677. return DataRowToModelObject(GetDataRow(filterFieldName, filterValue, operators));
  678. }
  679. /// <summary>
  680. /// 根据筛选条件获取一条数据Model对象
  681. /// </summary>
  682. /// <param name="whereString">筛选条件</param>
  683. /// <returns></returns>
  684. public object GetModelObject(string whereString)
  685. {
  686. return DataRowToModelObject(GetDataRow(whereString));
  687. }
  688. /// <summary>
  689. /// 得到一个object对象实体
  690. /// </summary>
  691. /// <param name="row"></param>
  692. /// <returns></returns>
  693. public object DataRowToModelObject(DataRow row)
  694. {
  695. return DataRowToModel(row, this.ObjModel);
  696. }
  697. /// <summary>
  698. /// 获取一条数据DataRow对象
  699. /// </summary>
  700. /// <param name="ID">id</param>
  701. /// <returns></returns>
  702. public DataRow GetDataRow(long id)
  703. {
  704. return GetDataRow("ID", id);
  705. }
  706. /// <summary>
  707. /// 获取一条数据DataRow对象
  708. /// </summary>
  709. /// <param name="ID">id</param>
  710. /// <returns></returns>
  711. public DataRow GetDataRow(int id)
  712. {
  713. return GetDataRow("ID", id);
  714. }
  715. /// <summary>
  716. /// 根据筛选条件获取一条数据DataRow对象
  717. /// </summary>
  718. /// <param name="whereString">筛选条件</param>
  719. /// <returns></returns>
  720. public DataRow GetDataRow(string whereString)
  721. {
  722. if (whereString.Trim().Length > 0)
  723. {
  724. StringBuilder strSql = new StringBuilder();
  725. strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
  726. strSql.Append(" where " + whereString);
  727. DataSet ds = SQLHelper.Query(strSql.ToString());
  728. if (ds.Tables[0].Rows.Count > 0)
  729. {
  730. return ds.Tables[0].Rows[0];
  731. }
  732. else
  733. {
  734. return null;
  735. }
  736. }
  737. else
  738. {
  739. return null;
  740. }
  741. }
  742. /// <summary>
  743. /// 根据筛选条件获取一条数据DataRow对象
  744. /// </summary>
  745. /// <param name="filterFieldName">筛选条件字段名</param>
  746. /// <param name="filterValue">值</param>
  747. /// <param name="operators">SQL筛选运算符号</param>
  748. /// <returns></returns>
  749. public DataRow GetDataRow(string filterFieldName, object filterValue, string operators = "=")
  750. {
  751. if (filterFieldName != "" && filterValue != null)
  752. {
  753. StringBuilder strSql = new StringBuilder();
  754. strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
  755. strSql.Append(" where 1=1 and " + filterFieldName + "" + operators + "@" + filterFieldName + " ");
  756. SqlParameter[] parameters = {
  757. new SqlParameter("@"+filterFieldName, filterValue)
  758. };
  759. DataSet ds = SQLHelper.Query(strSql.ToString(), parameters);
  760. if (ds.Tables[0].Rows.Count > 0)
  761. {
  762. return ds.Tables[0].Rows[0];
  763. }
  764. else
  765. {
  766. return null;
  767. }
  768. }
  769. else
  770. {
  771. return null;
  772. }
  773. }
  774. /// <summary>
  775. /// 根据筛选条件获取一条数据DataRow对象(运算符是“=”)
  776. /// </summary>
  777. /// <param name="filterFieldName">筛选条件字段名</param>
  778. /// <param name="filterValue">值</param>
  779. /// <returns></returns>
  780. public DataRow GetDataRow(string filterFieldName, object filterValue)
  781. {
  782. return GetDataRow(filterFieldName, filterValue, "=");
  783. }
  784. /// <summary>
  785. /// 得到一个对象实体
  786. /// </summary>
  787. public object DataRowToModel(DataRow row, object model)
  788. {
  789. if (row != null)
  790. {
  791. for (int i = 0; i < PropertyInfos.Length; i++)
  792. {
  793. try
  794. {
  795. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  796. string tempFieldName = propertyInfo.Name;//字段名
  797. if (row[tempFieldName] != null)
  798. {
  799. object tempValue = row[tempFieldName];//对应字段值
  800. LYFZ.WinAPI.CustomPublicMethod.SetPropertyValue(model, propertyInfo, tempValue);
  801. }
  802. }
  803. catch
  804. {
  805. }
  806. }
  807. //for (int i = 0; i < this.TableFieldNames.Length; i++)
  808. //{
  809. // try
  810. // {
  811. // string tempFieldName = this.TableFieldNames[i].Trim();
  812. // if (row[tempFieldName] != null)
  813. // {
  814. // object tempValue = row[tempFieldName];
  815. // LYFZ.WinAPI.CustomPublicMethod.SetPropertyValue(model, tempFieldName, tempValue);
  816. // }
  817. // }
  818. // catch
  819. // {
  820. // }
  821. //}
  822. }
  823. return model;
  824. }
  825. /// <summary>
  826. /// 获得数据列表
  827. /// </summary>
  828. /// <param name="strWhere">条件语句 不包含 where 关键字</param>
  829. /// <param name="filedOrder">SQL排序 如:id desc</param>
  830. public DataSet GetList(string strWhere, string filedOrder = "ID asc")
  831. {
  832. return this.GetList(0, strWhere, filedOrder);
  833. }
  834. /// <summary>
  835. /// 获得全部数据列表
  836. /// </summary>
  837. /// <param name="filedOrder">SQL排序 如:id desc</param>
  838. public DataSet GetAllList(string filedOrder = "ID asc")
  839. {
  840. return this.GetList("", filedOrder);
  841. }
  842. /// <summary>
  843. /// 获得前几行数据
  844. /// </summary>
  845. /// <param name="Top">行数</param>
  846. /// <param name="strWhere">条件 不包含 where 关键字</param>
  847. /// <param name="filedOrder">SQL排序 如:id asc</param>
  848. /// <returns></returns>
  849. public DataSet GetList(int Top, string strWhere, string filedOrder = "ID asc")
  850. {
  851. StringBuilder strSql = new StringBuilder();
  852. strSql.Append("select ");
  853. if (Top > 0)
  854. {
  855. strSql.Append(" top " + Top.ToString());
  856. }
  857. strSql.Append(" " + this.TableFieldNameStr + " from " + this.TableName + " ");
  858. if (strWhere.Trim() != "")
  859. {
  860. strSql.Append(" where 1=1 and (" + strWhere + ")");
  861. }
  862. strSql.Append(" order by " + filedOrder);
  863. DataSet ds = SQLHelper.Query(strSql.ToString());
  864. ds.Tables[0].TableName = this.TableName;
  865. return ds;
  866. }
  867. #endregion
  868. #region 数据分页
  869. /// <summary>
  870. /// 获取记录总数
  871. /// </summary>
  872. public int GetRecordCount(string strWhere)
  873. {
  874. StringBuilder strSql = new StringBuilder();
  875. strSql.Append("select count(1) FROM " + this.TableName + " ");
  876. if (strWhere.Trim() != "")
  877. {
  878. strSql.Append(" where 1=1 and (" + strWhere + ")");
  879. }
  880. object obj = SQLHelper.GetSingle(strSql.ToString());
  881. if (obj == null)
  882. {
  883. return 0;
  884. }
  885. else
  886. {
  887. return Convert.ToInt32(obj);
  888. }
  889. }
  890. /// <summary>
  891. /// 获取分页后总页数
  892. /// </summary>
  893. /// <param name="strWhere">筛选条件</param>
  894. /// <param name="pageSize">页面大小</param>
  895. /// <returns></returns>
  896. public int GetByPageCount(string strWhere, int pageSize)
  897. {
  898. int pageCount = 0;
  899. if (pageSize > 0)
  900. {
  901. int allCount = GetRecordCount(strWhere);
  902. pageCount = allCount / pageSize;
  903. if (pageCount * pageSize < allCount)
  904. {
  905. pageCount++;
  906. }
  907. }
  908. return pageCount;
  909. }
  910. /// <summary>
  911. /// 分页获取数据列表
  912. /// </summary>
  913. /// <param name="strWhere">筛选条件</param>
  914. /// <param name="pageIndex">当前页 不能小于0的整数</param>
  915. /// <param name="pageSize">页面大小,每页显示条数 不能小于0的整数</param>
  916. /// <param name="orderby">排序</param>
  917. /// <returns></returns>
  918. public DataSet GetListByPage(string strWhere, int pageIndex, int pageSize, string orderby = "id desc")
  919. {
  920. return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
  921. }
  922. /// <summary>
  923. /// 分页获取数据列表
  924. /// </summary>
  925. /// <param name="strWhere">筛选条件</param>
  926. /// <param name="orderby">排序</param>
  927. /// <param name="pageIndex">当前页</param>
  928. /// <param name="pageSize">页面大小,每页显示条数</param>
  929. /// <param name="pageCount">返回总页数</param>
  930. /// <returns></returns>
  931. public DataSet GetListByPage(string strWhere, string orderby, int pageIndex, int pageSize, ref int pageCount)
  932. {
  933. pageCount = GetByPageCount(strWhere, pageSize);
  934. return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
  935. }
  936. /// <summary>
  937. /// 分页获取数据列表
  938. /// </summary>
  939. /// <param name="strWhere">条件</param>
  940. /// <param name="orderby">排序</param>
  941. /// <param name="startIndex">开始index</param>
  942. /// <param name="endIndex">结束index</param>
  943. /// <returns></returns>
  944. public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
  945. {
  946. if (!string.IsNullOrEmpty(strWhere.Trim()))
  947. {
  948. strWhere = " Where " + strWhere;
  949. }
  950. if (!string.IsNullOrEmpty(orderby.Trim()))
  951. {
  952. orderby = " Order By " + orderby;
  953. }
  954. StringBuilder strSql = new StringBuilder();
  955. strSql.Append("SELECT * FROM " + this.TableName + " Where ID Not IN ");
  956. strSql.Append("(Select Top " + startIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
  957. strSql.Append(" And ID In ");
  958. strSql.Append("(Select Top " + endIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
  959. strSql.Append(orderby);
  960. DataSet ds = SQLHelper.Query(strSql.ToString());
  961. ds.Tables[0].TableName = this.TableName;
  962. return ds;
  963. }
  964. #endregion
  965. #region 2014-8-25 滕工
  966. /// <summary>
  967. /// Type转Dbtype
  968. /// </summary>
  969. /// <param name="type">变量的Type类型</param>
  970. /// <returns>对应的DbType类型</returns>
  971. public static SqlDbType GetDbType(Type type)
  972. {
  973. SqlDbType result = SqlDbType.NVarChar;
  974. if (type.Equals(typeof(Int32)) || type.IsEnum)
  975. result = SqlDbType.Int;
  976. else if (type.Equals(typeof(Int64)))
  977. result = SqlDbType.Int;
  978. else if (type.Equals(typeof(Double)) || type.Equals(typeof(Double)))
  979. result = SqlDbType.Decimal;
  980. else if (type.Equals(typeof(DateTime)))
  981. result = SqlDbType.DateTime;
  982. else if (type.Equals(typeof(Boolean)))
  983. result = SqlDbType.Bit;
  984. else if (type.Equals(typeof(String)))
  985. result = SqlDbType.NVarChar;
  986. else if (type.Equals(typeof(Decimal)))
  987. result = SqlDbType.Decimal;
  988. else if (type.Equals(typeof(Byte[])))
  989. result = SqlDbType.Image;
  990. else if (type.Equals(typeof(Guid)))
  991. result = SqlDbType.UniqueIdentifier;
  992. return result;
  993. }
  994. #endregion
  995. }
  996. }