BaseDataOperate.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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 LoginDal
  8. {
  9. /// <summary>
  10. /// 数据操作抽象基类
  11. /// </summary>
  12. public abstract class BaseDataOperate : 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(SqlException ex)
  339. {
  340. System.Diagnostics.Debug.Write(ex.Message);
  341. }
  342. }
  343. }
  344. catch (SqlException ex)
  345. {
  346. System.Diagnostics.Debug.Write(ex.Message);
  347. }
  348. }
  349. //去除结尾多余的“,”
  350. if (tempFieldNameString.ToString().LastIndexOf(',') == tempFieldNameString.Length - 1)
  351. {
  352. tempFieldNameString.Remove(tempFieldNameString.Length - 1, 1);
  353. }
  354. //去除结尾多余的“,”
  355. if (tempValueVariable.ToString().LastIndexOf(',') == tempValueVariable.Length - 1)
  356. {
  357. tempValueVariable.Remove(tempValueVariable.Length - 1, 1);
  358. }
  359. strSql.Append("insert into " + this.TableName + "(");
  360. strSql.Append("" + tempFieldNameString.ToString() + ")");
  361. strSql.Append(" values (");
  362. strSql.Append("" + tempValueVariable.ToString() + ")");
  363. strSql.Append(";select @@IDENTITY");
  364. SqlParameter[] parameters = parameterlist.ToArray();
  365. return new CommandInfo(strSql.ToString(), parameters, type);
  366. }
  367. #endregion
  368. #region 更新数据
  369. /// <summary>
  370. /// 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  371. /// </summary>
  372. /// <param name="model">Model对象</param>
  373. /// <param name="filterFieldName">筛选字段名称</param>
  374. /// <param name="operators">SQL筛选运算符号</param>
  375. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  376. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  377. /// <returns></returns>
  378. protected bool InsideUpdate(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
  379. {
  380. LYFZ.Helper.CommandInfo comdInfo = GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
  381. int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  382. if (rows > 0)
  383. {
  384. return true;
  385. }
  386. else
  387. {
  388. return false;
  389. }
  390. }
  391. /// <summary>
  392. /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和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. public object GetUpdateCommandInfoObject(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null)
  400. {
  401. return this.GetUpdateCommandInfo(model, filterFieldName, operators, overlookFieldList, whereStr);
  402. }
  403. /// <summary>
  404. /// 获取更新数据的UpdateCommandInfo对象 根据筛选字段和SQL筛选运算符号更新数据(内部方法)
  405. /// </summary>
  406. /// <param name="model">Model对象</param>
  407. /// <param name="filterFieldName">筛选字段名称</param>
  408. /// <param name="operators">SQL筛选运算符号</param>
  409. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  410. /// <param name="whereStr">Wher条件,当指定条件语句时筛选字段设置无效,不包含“where”关键字 不建义使用此参数</param>
  411. /// <param name="type"></param>
  412. public LYFZ.Helper.CommandInfo GetUpdateCommandInfo(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID", string whereStr = null, EffentNextType type = EffentNextType.None)
  413. {
  414. StringBuilder strSql = new StringBuilder();
  415. strSql.Append("update " + this.TableName + " set ");
  416. List<SqlParameter> parameterlist = new List<SqlParameter>();
  417. overlookFieldList += "," + filterFieldName;
  418. for (int i = 0; i < PropertyInfos.Length; i++)
  419. {
  420. try
  421. {
  422. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  423. string tempFieldName = propertyInfo.Name;//字段名
  424. object tempValue = propertyInfo.GetValue(model, null);//对应字段值
  425. //如果字段名不为忽略字段则进行处理
  426. if (!IsOverlookFiel(overlookFieldList, tempFieldName))
  427. {
  428. try
  429. {
  430. if (tempValue.GetType() == typeof(DateTime))
  431. {
  432. DateTime tempTime;
  433. try
  434. {
  435. if (!DateTime.TryParse(tempValue.ToString(), out tempTime))
  436. {
  437. tempValue = null;
  438. }
  439. else if (tempTime <= LYFZ.WinAPI.CustomPublicMethod.GetNullDateTime())
  440. {
  441. tempValue = null;
  442. }
  443. else
  444. if (tempTime.Year == Convert.ToDateTime("0001-01-01").Year || tempTime.Year == Convert.ToDateTime("1753-01-01").Year)
  445. {
  446. tempValue = null;
  447. }
  448. }
  449. catch { tempValue = null; }
  450. }
  451. }
  452. catch { }
  453. strSql.Append("" + tempFieldName + "=@" + tempFieldName + ",");
  454. SqlParameter parameter = new SqlParameter("@" + tempFieldName, tempValue);
  455. parameterlist.Add(parameter);
  456. }
  457. }
  458. catch { }
  459. }
  460. //去除结尾多余的“,”
  461. if (strSql.ToString().LastIndexOf(',') == strSql.Length - 1)
  462. {
  463. strSql.Remove(strSql.Length - 1, 1);
  464. }
  465. if (whereStr == null)
  466. {
  467. if (filterFieldName != "" && operators != "")
  468. {
  469. try
  470. {
  471. object FilterFieldValue = LYFZ.WinAPI.CustomPublicMethod.GetPropertyValue(model, filterFieldName);// model.GetType().GetProperty(filterFieldName).GetValue(model, null);
  472. strSql.Append(" where " + filterFieldName + operators + "@" + filterFieldName);
  473. parameterlist.Add(new SqlParameter("@" + filterFieldName, FilterFieldValue));
  474. }
  475. catch
  476. {
  477. }
  478. }
  479. }
  480. else
  481. {
  482. strSql.Append(" where 1=1 and (" + whereStr + ")");
  483. }
  484. SqlParameter[] parameters = parameterlist.ToArray();
  485. return new CommandInfo(strSql.ToString(), parameters, type);
  486. }
  487. /// <summary>
  488. /// 根据筛选字段和SQL筛选运算符号更新数据
  489. /// </summary>
  490. /// <param name="model">Model对象</param>
  491. /// <param name="filterFieldName">筛选字段名称</param>
  492. /// <param name="operators">SQL筛选运算符号</param>
  493. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  494. /// <returns></returns>
  495. public bool Update(object model, string filterFieldName = "ID", string operators = "=", string overlookFieldList = "ID")
  496. {
  497. return InsideUpdate(model, filterFieldName, operators, overlookFieldList);
  498. }
  499. /// <summary>
  500. /// 根据Wher条件更新数据 不建义使用此方法
  501. /// </summary>
  502. /// <param name="model">Model对象</param>
  503. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  504. /// <param name="overlookFieldList">忽略字段名列表 字段名之间用“,”号分隔</param>
  505. /// <returns></returns>
  506. public bool Update(object model, string whereStr, string overlookFieldList = "ID")
  507. {
  508. return InsideUpdate(model, overlookFieldList: overlookFieldList, whereStr: whereStr);
  509. }
  510. #endregion
  511. #region 删除数据
  512. /// <summary>
  513. /// 根据筛选字段或where条件删除数据
  514. /// </summary>
  515. /// <param name="filterFieldName">筛选字段名</param>
  516. /// <param name="operators">筛选SQL运算符</param>
  517. /// <param name="filterValue">筛选值</param>
  518. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  519. /// <returns></returns>
  520. protected bool InsideDelete(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
  521. {
  522. LYFZ.Helper.CommandInfo comdInfo = GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
  523. int rows = SQLHelper.ExecuteSql(comdInfo.CommandText, (SqlParameter[])comdInfo.Parameters);
  524. if (rows > 0)
  525. {
  526. return true;
  527. }
  528. else
  529. {
  530. return false;
  531. }
  532. }
  533. /// <summary>
  534. /// 根据筛选字段或where条件删除数据
  535. /// </summary>
  536. /// <param name="filterFieldName">筛选字段名</param>
  537. /// <param name="operators">筛选SQL运算符</param>
  538. /// <param name="filterValue">筛选值</param>
  539. /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
  540. /// <returns></returns>
  541. public object GetDeleteCommandInfoObject(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null)
  542. {
  543. return this.GetDeleteCommandInfo(filterFieldName, operators, filterValue, whereStr);
  544. }
  545. /// <summary>
  546. /// 根据筛选字段或where条件删除数据
  547. /// </summary>
  548. /// <param name="filterFieldName">筛选字段名</param>
  549. /// <param name="operators">筛选SQL运算符</param>
  550. /// <param name="filterValue">筛选值</param>
  551. /// <param name="whereStr">Wher条件,传入条件语句时筛选字段无效不建义使用 不包含“where”关键字</param>
  552. /// <param name="type"></param>
  553. /// <returns></returns>
  554. public LYFZ.Helper.CommandInfo GetDeleteCommandInfo(string filterFieldName = "ID", string operators = "=", object filterValue = null, string whereStr = null, EffentNextType type = EffentNextType.None)
  555. {
  556. if (filterValue == null) { filterValue = ""; }
  557. List<SqlParameter> parameterlist = new List<SqlParameter>();
  558. StringBuilder strSql = new StringBuilder();
  559. strSql.Append("delete from " + this.TableName + " ");
  560. if (whereStr == null)
  561. {
  562. strSql.Append(" where " + filterFieldName + "" + operators + "@" + filterFieldName + "");
  563. parameterlist.Add(new SqlParameter("@" + filterFieldName, filterValue));
  564. }
  565. else
  566. {
  567. strSql.Append(" where ID>@ID and (" + whereStr + ")");
  568. parameterlist.Add(new SqlParameter("@ID", Convert.ToInt32(0)));
  569. }
  570. SqlParameter[] parameters = parameterlist.ToArray();
  571. return new CommandInfo(strSql.ToString(), parameters, type);
  572. }
  573. /// <summary>
  574. /// 删除一条数据
  575. /// </summary>
  576. /// <param name="ID">id</param>
  577. public bool Delete(long ID)
  578. {
  579. return this.InsideDelete(filterValue: ID);
  580. }
  581. /// <summary>
  582. /// 删除一条数据
  583. /// </summary>
  584. /// <param name="ID">id</param>
  585. public bool Delete(int ID)
  586. {
  587. return this.InsideDelete(filterValue: ID);
  588. }
  589. /// <summary>
  590. /// 根据筛选字段删除数据
  591. /// </summary>
  592. /// <param name="filterValue">筛选值</param>
  593. /// <param name="filterFieldName">筛选字段名</param>
  594. /// <param name="operators">筛选SQL运算符</param>
  595. /// <returns></returns>
  596. public bool Delete(object filterValue, string filterFieldName = "ID", string operators = "=")
  597. {
  598. return this.InsideDelete(filterFieldName, operators, filterValue);
  599. }
  600. /// <summary>
  601. /// 根据where条件删除数据 不建义使用此方法
  602. /// </summary>
  603. /// <param name="whereStr">Wher条件,不包含“where”关键字</param>
  604. /// <returns></returns>
  605. public bool Delete(string whereStr)
  606. {
  607. return this.InsideDelete(whereStr: whereStr);
  608. }
  609. /// <summary>
  610. /// 批量删除数据
  611. /// </summary>
  612. /// <param name="filterFieldName">筛选字段名</param>
  613. /// <param name="valueList">值列表,多个值用“,”分隔,字符串值用“'”号包含</param>
  614. public bool DeleteList(string filterFieldName, string valueList)
  615. {
  616. if (filterFieldName != "" && valueList != "")
  617. {
  618. StringBuilder strSql = new StringBuilder();
  619. strSql.Append("delete from " + this.TableName + " ");
  620. strSql.Append(" where " + filterFieldName + " in (" + valueList + ") ");
  621. int rows = SQLHelper.ExecuteSql(strSql.ToString());
  622. if (rows > 0)
  623. {
  624. return true;
  625. }
  626. else
  627. {
  628. return false;
  629. }
  630. }
  631. else
  632. {
  633. return false;
  634. }
  635. }
  636. /// <summary>
  637. /// 按ID列表批量删除数据
  638. /// </summary>
  639. /// <param name="idList">ID列表,多个用“,”分隔</param>
  640. public bool DeleteList(string idList)
  641. {
  642. return this.DeleteList("ID", idList);
  643. }
  644. #endregion
  645. #region 查询数据
  646. /// <summary>
  647. /// 得到一个Object对象实体
  648. /// </summary>
  649. /// <param name="ID"></param>
  650. /// <returns></returns>
  651. public object GetModelObject(long ID)
  652. {
  653. return DataRowToModelObject(GetDataRow(ID));
  654. }
  655. /// <summary>
  656. /// 得到一个Object对象实体
  657. /// </summary>
  658. /// <param name="ID"></param>
  659. /// <returns></returns>
  660. public object GetModelObject(int ID)
  661. {
  662. return DataRowToModelObject(GetDataRow(ID));
  663. }
  664. /// <summary>
  665. /// 根据筛选条件获取一条数据Model对象
  666. /// </summary>
  667. /// <param name="filterFieldName">筛选条件字段名</param>
  668. /// <param name="filterValue">值</param>
  669. /// <returns></returns>
  670. public object GetModelObject(string filterFieldName, object filterValue)
  671. {
  672. return DataRowToModelObject(GetDataRow(filterFieldName, filterValue));
  673. }
  674. /// <summary>
  675. /// 根据筛选条件获取一条数据Model对象
  676. /// </summary>
  677. /// <param name="filterFieldName">筛选条件字段名</param>
  678. /// <param name="filterValue">值</param>
  679. /// <param name="operators">SQL筛选运算符号</param>
  680. /// <returns></returns>
  681. public object GetModelObject(string filterFieldName, object filterValue, string operators)
  682. {
  683. return DataRowToModelObject(GetDataRow(filterFieldName, filterValue, operators));
  684. }
  685. /// <summary>
  686. /// 根据筛选条件获取一条数据Model对象
  687. /// </summary>
  688. /// <param name="whereString">筛选条件</param>
  689. /// <returns></returns>
  690. public object GetModelObject(string whereString)
  691. {
  692. return DataRowToModelObject(GetDataRow(whereString));
  693. }
  694. /// <summary>
  695. /// 得到一个object对象实体
  696. /// </summary>
  697. /// <param name="row"></param>
  698. /// <returns></returns>
  699. public object DataRowToModelObject(DataRow row)
  700. {
  701. return DataRowToModel(row, this.ObjModel);
  702. }
  703. /// <summary>
  704. /// 获取一条数据DataRow对象
  705. /// </summary>
  706. /// <param name="ID">id</param>
  707. /// <returns></returns>
  708. public DataRow GetDataRow(long id)
  709. {
  710. return GetDataRow("ID", id);
  711. }
  712. /// <summary>
  713. /// 获取一条数据DataRow对象
  714. /// </summary>
  715. /// <param name="ID">id</param>
  716. /// <returns></returns>
  717. public DataRow GetDataRow(int id)
  718. {
  719. return GetDataRow("ID", id);
  720. }
  721. /// <summary>
  722. /// 根据筛选条件获取一条数据DataRow对象
  723. /// </summary>
  724. /// <param name="whereString">筛选条件</param>
  725. /// <returns></returns>
  726. public DataRow GetDataRow(string whereString)
  727. {
  728. if (whereString.Trim().Length > 0)
  729. {
  730. StringBuilder strSql = new StringBuilder();
  731. strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
  732. strSql.Append(" where " + whereString);
  733. DataSet ds = SQLHelper.Query(strSql.ToString());
  734. if (ds.Tables[0].Rows.Count > 0)
  735. {
  736. return ds.Tables[0].Rows[0];
  737. }
  738. else
  739. {
  740. return null;
  741. }
  742. }
  743. else
  744. {
  745. return null;
  746. }
  747. }
  748. /// <summary>
  749. /// 根据筛选条件获取一条数据DataRow对象
  750. /// </summary>
  751. /// <param name="filterFieldName">筛选条件字段名</param>
  752. /// <param name="filterValue">值</param>
  753. /// <param name="operators">SQL筛选运算符号</param>
  754. /// <returns></returns>
  755. public DataRow GetDataRow(string filterFieldName, object filterValue, string operators = "=")
  756. {
  757. if (filterFieldName != "" && filterValue != null)
  758. {
  759. StringBuilder strSql = new StringBuilder();
  760. strSql.Append("select top 1 " + this.TableFieldNameStr + " from " + this.TableName + " ");
  761. strSql.Append(" where 1=1 and " + filterFieldName + "" + operators + "@" + filterFieldName + " ");
  762. SqlParameter[] parameters = {
  763. new SqlParameter("@"+filterFieldName, filterValue)
  764. };
  765. DataSet ds = SQLHelper.Query(strSql.ToString(), parameters);
  766. if (ds.Tables[0].Rows.Count > 0)
  767. {
  768. return ds.Tables[0].Rows[0];
  769. }
  770. else
  771. {
  772. return null;
  773. }
  774. }
  775. else
  776. {
  777. return null;
  778. }
  779. }
  780. /// <summary>
  781. /// 根据筛选条件获取一条数据DataRow对象(运算符是“=”)
  782. /// </summary>
  783. /// <param name="filterFieldName">筛选条件字段名</param>
  784. /// <param name="filterValue">值</param>
  785. /// <returns></returns>
  786. public DataRow GetDataRow(string filterFieldName, object filterValue)
  787. {
  788. return GetDataRow(filterFieldName, filterValue, "=");
  789. }
  790. /// <summary>
  791. /// 得到一个对象实体
  792. /// </summary>
  793. public object DataRowToModel(DataRow row, object model)
  794. {
  795. if (row != null)
  796. {
  797. for (int i = 0; i < PropertyInfos.Length; i++)
  798. {
  799. try
  800. {
  801. System.Reflection.PropertyInfo propertyInfo = PropertyInfos[i];
  802. string tempFieldName = propertyInfo.Name;//字段名
  803. if (row[tempFieldName] != null)
  804. {
  805. object tempValue = row[tempFieldName];//对应字段值
  806. LYFZ.WinAPI.CustomPublicMethod.SetPropertyValue(model, propertyInfo, tempValue);
  807. }
  808. }
  809. catch
  810. {
  811. }
  812. }
  813. //for (int i = 0; i < this.TableFieldNames.Length; i++)
  814. //{
  815. // try
  816. // {
  817. // string tempFieldName = this.TableFieldNames[i].Trim();
  818. // if (row[tempFieldName] != null)
  819. // {
  820. // object tempValue = row[tempFieldName];
  821. // LYFZ.WinAPI.CustomPublicMethod.SetPropertyValue(model, tempFieldName, tempValue);
  822. // }
  823. // }
  824. // catch
  825. // {
  826. // }
  827. //}
  828. }
  829. return model;
  830. }
  831. /// <summary>
  832. /// 获得数据列表
  833. /// </summary>
  834. /// <param name="strWhere">条件语句 不包含 where 关键字</param>
  835. /// <param name="filedOrder">SQL排序 如:id desc</param>
  836. public DataSet GetList(string strWhere, string filedOrder = "ID asc")
  837. {
  838. return this.GetList(0, strWhere, filedOrder);
  839. }
  840. /// <summary>
  841. /// 获得全部数据列表
  842. /// </summary>
  843. /// <param name="filedOrder">SQL排序 如:id desc</param>
  844. public DataSet GetAllList(string filedOrder = "ID asc")
  845. {
  846. return this.GetList("", filedOrder);
  847. }
  848. /// <summary>
  849. /// 获得前几行数据
  850. /// </summary>
  851. /// <param name="Top">行数</param>
  852. /// <param name="strWhere">条件 不包含 where 关键字</param>
  853. /// <param name="filedOrder">SQL排序 如:id asc</param>
  854. /// <returns></returns>
  855. public DataSet GetList(int Top, string strWhere, string filedOrder = "ID asc")
  856. {
  857. StringBuilder strSql = new StringBuilder();
  858. strSql.Append("select ");
  859. if (Top > 0)
  860. {
  861. strSql.Append(" top " + Top.ToString());
  862. }
  863. strSql.Append(" " + this.TableFieldNameStr + " from " + this.TableName + " ");
  864. if (strWhere.Trim() != "")
  865. {
  866. strSql.Append(" where 1=1 and (" + strWhere + ")");
  867. }
  868. strSql.Append(" order by " + filedOrder);
  869. DataSet ds = SQLHelper.Query(strSql.ToString());
  870. ds.Tables[0].TableName = this.TableName;
  871. return ds;
  872. }
  873. #endregion
  874. #region 数据分页
  875. /// <summary>
  876. /// 获取记录总数
  877. /// </summary>
  878. public int GetRecordCount(string strWhere)
  879. {
  880. StringBuilder strSql = new StringBuilder();
  881. strSql.Append("select count(1) FROM " + this.TableName + " ");
  882. if (strWhere.Trim() != "")
  883. {
  884. strSql.Append(" where 1=1 and (" + strWhere + ")");
  885. }
  886. object obj = SQLHelper.GetSingle(strSql.ToString());
  887. if (obj == null)
  888. {
  889. return 0;
  890. }
  891. else
  892. {
  893. return Convert.ToInt32(obj);
  894. }
  895. }
  896. /// <summary>
  897. /// 获取分页后总页数
  898. /// </summary>
  899. /// <param name="strWhere">筛选条件</param>
  900. /// <param name="pageSize">页面大小</param>
  901. /// <returns></returns>
  902. public int GetByPageCount(string strWhere, int pageSize)
  903. {
  904. int pageCount = 0;
  905. if (pageSize > 0)
  906. {
  907. int allCount = GetRecordCount(strWhere);
  908. pageCount = allCount / pageSize;
  909. if (pageCount * pageSize < allCount)
  910. {
  911. pageCount++;
  912. }
  913. }
  914. return pageCount;
  915. }
  916. /// <summary>
  917. /// 分页获取数据列表
  918. /// </summary>
  919. /// <param name="strWhere">筛选条件</param>
  920. /// <param name="pageIndex">当前页 不能小于0的整数</param>
  921. /// <param name="pageSize">页面大小,每页显示条数 不能小于0的整数</param>
  922. /// <param name="orderby">排序</param>
  923. /// <returns></returns>
  924. public DataSet GetListByPage(string strWhere, int pageIndex, int pageSize, string orderby = "id desc")
  925. {
  926. return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
  927. }
  928. /// <summary>
  929. /// 分页获取数据列表
  930. /// </summary>
  931. /// <param name="strWhere">筛选条件</param>
  932. /// <param name="orderby">排序</param>
  933. /// <param name="pageIndex">当前页</param>
  934. /// <param name="pageSize">页面大小,每页显示条数</param>
  935. /// <param name="pageCount">返回总页数</param>
  936. /// <returns></returns>
  937. public DataSet GetListByPage(string strWhere, string orderby, int pageIndex, int pageSize, ref int pageCount)
  938. {
  939. pageCount = GetByPageCount(strWhere, pageSize);
  940. return this.GetListByPage(strWhere, orderby, (pageIndex - 1) * pageSize, pageIndex * pageSize);
  941. }
  942. /// <summary>
  943. /// 分页获取数据列表
  944. /// </summary>
  945. /// <param name="strWhere">条件</param>
  946. /// <param name="orderby">排序</param>
  947. /// <param name="startIndex">开始index</param>
  948. /// <param name="endIndex">结束index</param>
  949. /// <returns></returns>
  950. public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
  951. {
  952. if (!string.IsNullOrEmpty(strWhere.Trim()))
  953. {
  954. strWhere = " Where " + strWhere;
  955. }
  956. if (!string.IsNullOrEmpty(orderby.Trim()))
  957. {
  958. orderby = " Order By " + orderby;
  959. }
  960. StringBuilder strSql = new StringBuilder();
  961. strSql.Append("SELECT * FROM " + this.TableName + " Where ID Not IN ");
  962. strSql.Append("(Select Top " + startIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
  963. strSql.Append(" And ID In ");
  964. strSql.Append("(Select Top " + endIndex + " ID From " + this.TableName + " " + strWhere + orderby + ")");
  965. strSql.Append(orderby);
  966. DataSet ds = SQLHelper.Query(strSql.ToString());
  967. ds.Tables[0].TableName = this.TableName;
  968. return ds;
  969. }
  970. #endregion
  971. #region 2014-8-25 滕工
  972. /// <summary>
  973. /// Type转Dbtype
  974. /// </summary>
  975. /// <param name="type">变量的Type类型</param>
  976. /// <returns>对应的DbType类型</returns>
  977. public static SqlDbType GetDbType(Type type)
  978. {
  979. SqlDbType result = SqlDbType.NVarChar;
  980. if (type.Equals(typeof(Int32)) || type.IsEnum)
  981. result = SqlDbType.Int;
  982. else if (type.Equals(typeof(Int64)))
  983. result = SqlDbType.Int;
  984. else if (type.Equals(typeof(Double)) || type.Equals(typeof(Double)))
  985. result = SqlDbType.Decimal;
  986. else if (type.Equals(typeof(DateTime)))
  987. result = SqlDbType.DateTime;
  988. else if (type.Equals(typeof(Boolean)))
  989. result = SqlDbType.Bit;
  990. else if (type.Equals(typeof(String)))
  991. result = SqlDbType.NVarChar;
  992. else if (type.Equals(typeof(Decimal)))
  993. result = SqlDbType.Decimal;
  994. else if (type.Equals(typeof(Byte[])))
  995. result = SqlDbType.Image;
  996. else if (type.Equals(typeof(Guid)))
  997. result = SqlDbType.UniqueIdentifier;
  998. return result;
  999. }
  1000. #endregion
  1001. }
  1002. }