You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1072 lines
37 KiB

2 years ago
  1. using EasyNet.Common;
  2. using EasyNet.DBUtility;
  3. using Entity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. namespace EasyNet.Manager
  11. {
  12. public class DbManager
  13. {
  14. private IDbTransaction m_Transaction;
  15. private DbManager()
  16. {
  17. }
  18. public static DbManager PriviteInstance()
  19. {
  20. var m = new DbManager();
  21. return m;
  22. }
  23. public static DbManager GetCurrentManager()
  24. {
  25. var m = ManagerFactory.GetManager();
  26. return m;
  27. }
  28. public static DbManager NewManager()
  29. {
  30. var m = new DbManager();
  31. return m;
  32. }
  33. public void BeginTransaction()
  34. {
  35. m_Transaction = DbFactory.CreateDbTransaction();
  36. }
  37. public void Commit()
  38. {
  39. if (m_Transaction != null)
  40. {
  41. if (m_Transaction.Connection.State != ConnectionState.Closed)
  42. {
  43. m_Transaction.Commit();
  44. }
  45. }
  46. }
  47. public void Rollback()
  48. {
  49. if (m_Transaction != null)
  50. {
  51. if (m_Transaction.Connection.State != ConnectionState.Closed)
  52. {
  53. m_Transaction.Rollback();
  54. }
  55. }
  56. }
  57. private IDbTransaction GetTransaction()
  58. {
  59. if (m_Transaction != null) return m_Transaction;
  60. return DbFactory.CreateDbTransaction();
  61. }
  62. private void Commit(IDbTransaction transaction)
  63. {
  64. if (m_Transaction == null && transaction != null)
  65. {
  66. if (transaction.Connection.State != ConnectionState.Closed)
  67. {
  68. transaction.Commit();
  69. }
  70. }
  71. }
  72. private static void Rollback(IDbTransaction transaction)
  73. {
  74. if (transaction != null)
  75. {
  76. if (transaction.Connection.State != ConnectionState.Closed)
  77. {
  78. transaction.Rollback();
  79. }
  80. }
  81. }
  82. #region 將實體資料修改到資料庫
  83. public static int ExecuteSqlTran(Object param)
  84. {
  85. var val = 0;
  86. var lstCommandInfo = new List<CommandInfo>();
  87. CommandInfo oCommandInfo = null;
  88. try
  89. {
  90. var oActions = param as Dictionary<string, object>;
  91. foreach (string key in oActions.Keys)
  92. {
  93. if (oActions[key].GetType() == typeof(Object[]))
  94. {
  95. var saEntity = oActions[key] as Object[];
  96. foreach (Object jo in saEntity)
  97. {
  98. oCommandInfo = new CommandInfo();
  99. var oEntity = jo as Dictionary<string, object>;
  100. var parameters = new SqlParameter[oEntity.Keys.Count];
  101. var iIndx = 0;
  102. foreach (string pmkey in oEntity.Keys)
  103. {
  104. var sp = new SqlParameter
  105. {
  106. ParameterName = "@" + pmkey,
  107. Value = oEntity[pmkey]
  108. };
  109. parameters[iIndx] = sp;
  110. iIndx++;
  111. }
  112. oCommandInfo.Parameters = parameters;
  113. oCommandInfo.CommandText = Entity.SqlCommand.GetSqlCommand(key);
  114. lstCommandInfo.Add(oCommandInfo);
  115. }
  116. }
  117. else
  118. {
  119. oCommandInfo = new CommandInfo();
  120. var oEntity = oActions[key] as Dictionary<string, object>;
  121. var parameters = new SqlParameter[oEntity.Keys.Count];
  122. var iIndx = 0;
  123. foreach (string pmkey in oEntity.Keys)
  124. {
  125. var sp = new SqlParameter
  126. {
  127. ParameterName = "@" + pmkey,
  128. Value = oEntity[pmkey]
  129. };
  130. parameters[iIndx] = sp;
  131. iIndx++;
  132. }
  133. oCommandInfo.Parameters = parameters;
  134. oCommandInfo.CommandText = Entity.SqlCommand.GetSqlCommand(key);
  135. lstCommandInfo.Add(oCommandInfo);
  136. }
  137. }
  138. if (AdoHelper.DbType == DatabaseType.ACCESS)
  139. {
  140. }
  141. else
  142. {
  143. val = DbHelperSQL.ExecuteSqlTran(lstCommandInfo);
  144. }
  145. }
  146. catch (Exception e)
  147. {
  148. throw new Exception(e.Message, e);
  149. }
  150. return val;
  151. }
  152. #endregion 將實體資料修改到資料庫
  153. #region 批量根據參數新增資料
  154. public int Insert(Object obj)
  155. {
  156. IDbTransaction transaction = null;
  157. var iVal = 0;
  158. try
  159. {
  160. //獲取資料庫連接,如果開啟了事務,從事務中獲取
  161. transaction = GetTransaction();
  162. var oEntity = obj as Dictionary<string, object>;
  163. iVal = ExecuteInsert(oEntity, transaction);
  164. if (iVal < 0)
  165. {
  166. Rollback(transaction);
  167. }
  168. else
  169. {
  170. Commit(transaction);
  171. }
  172. }
  173. catch (Exception e)
  174. {
  175. Rollback(transaction);
  176. throw new Exception(e.Message, e);
  177. }
  178. return iVal;
  179. }
  180. #endregion 批量根據參數新增資料
  181. #region 批量根據參數修改資料
  182. public int Update(Object obj)
  183. {
  184. IDbTransaction transaction = null;
  185. var iVal = 0;
  186. try
  187. {
  188. //獲取資料庫連接,如果開啟了事務,從事務中獲取
  189. transaction = GetTransaction();
  190. var oEntity = obj as Dictionary<string, object>;
  191. iVal = ExecuteUpdate(oEntity, transaction);
  192. if (iVal < 0)
  193. {
  194. Rollback(transaction);
  195. }
  196. else
  197. {
  198. Commit(transaction);
  199. }
  200. }
  201. catch (Exception e)
  202. {
  203. Rollback(transaction);
  204. throw new Exception(e.Message, e);
  205. }
  206. return iVal;
  207. }
  208. #endregion 批量根據參數修改資料
  209. #region 批量根據參數刪除資料
  210. public int Delete(Object obj)
  211. {
  212. IDbTransaction transaction = null;
  213. var iVal = 0;
  214. try
  215. {
  216. //JObject jo = oEntity[key] as JObject;
  217. //JObject jo = (JObject)JsonConvert.DeserializeObject(oEntity[key]);
  218. //JObject jo = (JObject)JsonConvert.SerializeObject(oEntity[key]);
  219. //JObject jo = (JObject)JsonConvert.SerializeObject(oEntity[key], Formatting.Indented);
  220. //JObject jo = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(oEntity[key], Formatting.Indented));
  221. //獲取資料庫連接,如果開啟了事務,從事務中獲取
  222. transaction = GetTransaction();
  223. var oEntity = obj as Dictionary<string, object>;
  224. iVal = ExecuteDelete(oEntity, transaction);
  225. Commit(transaction);
  226. }
  227. catch (Exception e)
  228. {
  229. Rollback(transaction);
  230. throw new Exception(e.Message, e);
  231. }
  232. return iVal;
  233. }
  234. #endregion 批量根據參數刪除資料
  235. #region 批量根據參數修改資料(MasterDteil)
  236. public int UpdateTran(Object obj)
  237. {
  238. IDbTransaction transaction = null;
  239. var iVal = 0;
  240. try
  241. {
  242. //獲取資料庫連接,如果開啟了事務,從事務中獲取
  243. transaction = GetTransaction();
  244. var oActions = obj as Dictionary<string, object>;
  245. foreach (string key in oActions.Keys)
  246. {
  247. var oEntity = oActions[key] as Dictionary<string, object>;
  248. switch (key)
  249. {
  250. case EasyNetGlobalConstWord.DEL:
  251. iVal += ExecuteDelete(oEntity, transaction);
  252. break;
  253. case EasyNetGlobalConstWord.ADD:
  254. iVal += ExecuteInsert(oEntity, transaction);
  255. break;
  256. case EasyNetGlobalConstWord.UPD:
  257. iVal += ExecuteUpdate(oEntity, transaction);
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. if (iVal < 0)
  264. {
  265. Rollback(transaction);
  266. }
  267. else
  268. {
  269. Commit(transaction);
  270. }
  271. }
  272. catch (Exception e)
  273. {
  274. Rollback(transaction);
  275. throw new Exception(e.Message, e);
  276. }
  277. return iVal;
  278. }
  279. #endregion 批量根據參數修改資料(MasterDteil)
  280. #region 通過自訂SQL語句查詢記錄數
  281. public static int Count(string strSql, ParamMap param)
  282. {
  283. var count = 0;
  284. try
  285. {
  286. strSql = strSql.ToLower();
  287. //String columns = SQLBuilderHelper.fetchColumns(strSql);
  288. if (AdoHelper.DbType == DatabaseType.ACCESS)
  289. {
  290. strSql = SQLBuilderHelper.BuilderSQL(strSql, param.ToDbParameters());
  291. }
  292. count = Convert.ToInt32(AdoHelper.ExecuteScalar(AdoHelper.ConnectionString, CommandType.Text, strSql, param.ToDbParameters()));
  293. }
  294. catch (Exception e)
  295. {
  296. throw new Exception(e.Message, e);
  297. }
  298. return count;
  299. }
  300. #endregion 通過自訂SQL語句查詢記錄數
  301. #region 查询List數據
  302. public static Object QueryList(string sql, Object obj)
  303. {
  304. var oRel = new Object();
  305. IDataReader sdr = null;
  306. var entity = new Object();
  307. var sKey = "";
  308. try
  309. {
  310. do
  311. {
  312. var oEntity = obj as Dictionary<string, object>;
  313. Dictionary<string, object> wh = null;
  314. Dictionary<string, object> sort = null;
  315. if (oEntity.Count == 0)
  316. {
  317. oRel = QueryBySql(sql, obj, "list");
  318. break;
  319. }
  320. sKey = oEntity.Keys.First();
  321. entity = EntityHelper.GetEntity(sKey);
  322. if (entity.ToString() == "")
  323. {
  324. oRel = QueryBySql(sql, obj, "list");
  325. break;
  326. }
  327. var properties = ReflectionHelper.GetProperties(entity.GetType());
  328. var tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.SELECT, properties);
  329. wh = oEntity[sKey] as Dictionary<string, object>;
  330. if (oEntity.Keys.Contains(EasyNetGlobalConstWord.SORT))
  331. {
  332. sort = oEntity[EasyNetGlobalConstWord.SORT] as Dictionary<string, object>;
  333. }
  334. if (sql == "")
  335. {
  336. sql = DbEntityUtils.GetQueryByObjSql(tableInfo, ref wh, sort);
  337. }
  338. var parms = DbFactory.CreateDbParameters(wh.Keys.Count);
  339. var idx = 0;
  340. foreach (string _key in wh.Keys)
  341. {
  342. parms[idx].ParameterName = _key;
  343. parms[idx].Value = wh[_key];
  344. idx++;
  345. }
  346. sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, sql, parms);
  347. var list = new List<Object>();
  348. var indx = 0;
  349. while (sdr.Read())
  350. {
  351. entity = EntityHelper.GetEntity(sKey);
  352. for (int index = 0; index < sdr.FieldCount; index++)
  353. {
  354. var name = sdr.GetName(index);
  355. var property = ReflectionHelper.GetProperty(properties, name);
  356. if (property == null) continue;
  357. ReflectionHelper.SetPropertyValue(entity, property, sdr.GetValue(index));
  358. }
  359. indx++;
  360. var sValue = ReflectionHelper.GetPropertyValue(entity, EasyNetGlobalConstWord.ROWINDEX);
  361. if ((int)sValue == 0)
  362. {
  363. var property = ReflectionHelper.GetProperty(properties, EasyNetGlobalConstWord.ROWINDEX);
  364. ReflectionHelper.SetPropertyValue(entity, property, indx);
  365. }
  366. list.Add(entity);
  367. }
  368. oRel = list;
  369. }
  370. while (false);
  371. }
  372. catch (Exception e)
  373. {
  374. throw new Exception(e.Message, e);
  375. }
  376. finally
  377. {
  378. if (sdr != null)
  379. {
  380. sdr.Close();
  381. sdr.Dispose();
  382. }
  383. }
  384. return oRel;
  385. }
  386. #endregion 查询List數據
  387. #region 查询List數據筆數
  388. public static int QueryCount(Object obj)
  389. {
  390. var iCount = 0;
  391. var entity = new Object();
  392. var sKey = "";
  393. try
  394. {
  395. do
  396. {
  397. var oEntity = obj as Dictionary<string, object>;
  398. Dictionary<string, object> wh = null;
  399. sKey = oEntity.Keys.First();
  400. entity = EntityHelper.GetEntity(sKey);
  401. var properties = ReflectionHelper.GetProperties(entity.GetType());
  402. var tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.SELECT, properties);
  403. wh = oEntity[sKey] as Dictionary<string, object>;
  404. var sSql = "";
  405. sSql = DbEntityUtils.GetQueryByObjSql(tableInfo, ref wh);
  406. sSql = sSql.ToLower();
  407. var countSQL = SQLBuilderHelper.BuilderCountSQL(sSql);
  408. var parms = DbFactory.CreateDbParameters(wh.Keys.Count);
  409. var idx = 0;
  410. foreach (string _key in wh.Keys)
  411. {
  412. parms[idx].ParameterName = _key;
  413. parms[idx].Value = wh[_key];
  414. idx++;
  415. }
  416. iCount = Convert.ToInt32(AdoHelper.ExecuteScalar(AdoHelper.ConnectionString, CommandType.Text, countSQL, parms));
  417. }
  418. while (false);
  419. }
  420. catch (Exception e)
  421. {
  422. throw new Exception(e.Message, e);
  423. }
  424. return iCount;
  425. }
  426. #endregion 查询List數據筆數
  427. #region 查询單筆數據
  428. public static Object QueryOne(string sql, Object obj)
  429. {
  430. var entity = new Object();
  431. IDataReader sdr = null;
  432. try
  433. {
  434. do
  435. {
  436. var oEntity = obj as Dictionary<string, object>;
  437. var sKey = oEntity.Keys.First();
  438. entity = EntityHelper.GetEntity(sKey);
  439. if (entity.ToString() == "")
  440. {
  441. entity = QueryBySql(sql, obj, "one");
  442. break;
  443. }
  444. var properties = ReflectionHelper.GetProperties(entity.GetType());
  445. var tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.SELECT, properties);
  446. var wh = oEntity[sKey] as Dictionary<string, object>;
  447. if (sql == "")
  448. {
  449. sql = DbEntityUtils.GetQueryByObjSql(tableInfo, ref wh);
  450. }
  451. var parms = DbFactory.CreateDbParameters(wh.Keys.Count);
  452. var idx = 0;
  453. foreach (string _key in wh.Keys)
  454. {
  455. parms[idx].ParameterName = _key;
  456. parms[idx].Value = wh[_key];
  457. idx++;
  458. }
  459. sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, sql, parms);
  460. while (sdr.Read())
  461. {
  462. for (int index = 0; index < sdr.FieldCount; index++)
  463. {
  464. var name = sdr.GetName(index);
  465. var property = ReflectionHelper.GetProperty(properties, name);
  466. if (property == null) continue;
  467. ReflectionHelper.SetPropertyValue(entity, property, sdr.GetValue(index));
  468. }
  469. }
  470. }
  471. while (false);
  472. }
  473. catch (Exception e)
  474. {
  475. throw new Exception(e.Message, e);
  476. }
  477. finally
  478. {
  479. if (sdr != null)
  480. {
  481. sdr.Close();
  482. sdr.Dispose();
  483. }
  484. }
  485. return entity;
  486. }
  487. #endregion 查询單筆數據
  488. #region 查询实体对应表的所有数据
  489. public static List<T> Query<T>(string sql) where T : new()
  490. {
  491. var list = new List<T>();
  492. var properties = ReflectionHelper.GetProperties(new T().GetType());
  493. //TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.SELECT, properties);
  494. IDataReader sdr = null;
  495. try
  496. {
  497. sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, sql);
  498. while (sdr.Read())
  499. {
  500. var entity = new T();
  501. for (int index = 0; index < sdr.FieldCount; index++)
  502. {
  503. var name = sdr.GetName(index);
  504. var property = ReflectionHelper.GetProperty(properties, name);
  505. if (property == null) continue;
  506. ReflectionHelper.SetPropertyValue(entity, property, sdr.GetValue(index));
  507. }
  508. list.Add(entity);
  509. }
  510. }
  511. catch (Exception e)
  512. {
  513. throw new Exception(e.Message, e);
  514. }
  515. finally
  516. {
  517. if (sdr != null)
  518. {
  519. sdr.Close();
  520. sdr.Dispose();
  521. }
  522. }
  523. return list;
  524. }
  525. #endregion 查询实体对应表的所有数据
  526. #region 分頁查詢返回分頁結果
  527. public PageResult QueryPage(ParamMap param)
  528. {
  529. var pageResult = new PageResult();
  530. var sSql = "";
  531. var sEntityKey = param.Entity;
  532. IDataReader sdr = null;
  533. IDbConnection connection = null;
  534. try
  535. {
  536. connection = GetConnection();
  537. Dictionary<string, object> sort = null;
  538. var sSorts = new StringBuilder();
  539. var closeConnection = GetWillConnectionState();
  540. var entity = EntityHelper.GetEntity(sEntityKey);
  541. var properties = ReflectionHelper.GetProperties(entity.GetType());
  542. var tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.SELECT, properties);
  543. if (param.ContainsKey(EasyNetGlobalConstWord.SORT))
  544. {
  545. sort = param[EasyNetGlobalConstWord.SORT] as Dictionary<string, object>;
  546. }
  547. sSql = DbEntityUtils.GetQueryByPage(tableInfo, ref param);
  548. sSql = sSql.ToLower();
  549. var countSQL = SQLBuilderHelper.BuilderCountSQL(sSql);
  550. if (param.IsPage && !SQLBuilderHelper.IsPage(sSql))
  551. {
  552. sSql = SQLBuilderHelper.BuilderPageSQL(sSql, param.OrderFields, param.OrderType);
  553. }
  554. if (sort != null)
  555. {
  556. foreach (string key in sort.Keys)
  557. {
  558. var nKey = DbKeywords.FormatColumnName(key.Trim());
  559. sSorts.Append(nKey).Append(" " + sort[nKey]).Append(",");
  560. }
  561. }
  562. if (sSorts.Length > 0)
  563. {
  564. sSorts.Remove(sSorts.ToString().Length - 1, 1);
  565. sSql += " ORDER BY {0} ";
  566. sSql = string.Format(sSql, sSorts.ToString());
  567. param.Remove(EasyNetGlobalConstWord.SORT);
  568. };
  569. if (AdoHelper.DbType == DatabaseType.ACCESS)
  570. {
  571. sSql = SQLBuilderHelper.BuilderSQL(entity, sSql, param.ToDbParameters());
  572. sdr = AdoHelper.ExecuteReader(closeConnection, connection, CommandType.Text, sSql);
  573. }
  574. else
  575. {
  576. sdr = AdoHelper.ExecuteReader(closeConnection, connection, CommandType.Text, sSql, param.ToDbParameters());
  577. }
  578. var count = Count(countSQL, param);
  579. var list = DbEntityUtils.ToListForQuery(sdr, properties);
  580. pageResult.Total = count;
  581. pageResult.DataList = list;
  582. }
  583. catch (Exception e)
  584. {
  585. throw new Exception(e.Message, e);
  586. }
  587. finally
  588. {
  589. if (sdr != null)
  590. {
  591. sdr.Close();
  592. sdr.Dispose();
  593. }
  594. }
  595. return pageResult;
  596. }
  597. #endregion 分頁查詢返回分頁結果
  598. #region 分頁查詢返回分頁結果
  599. public static PageResult QueryPageByPrc(string sPrcName, Object param, bool bCount)
  600. {
  601. var pageResult = new PageResult();
  602. var iCount = 0;
  603. var ds = new DataSet();
  604. try
  605. {
  606. var dicQueryPm = param as Dictionary<string, object>;
  607. var dicQueryPmCount = new Dictionary<string, object>();
  608. var parameters_List = new SqlParameter[dicQueryPm.Keys.Count];
  609. var parameters_Count = new SqlParameter[dicQueryPm.Keys.Count - 3];
  610. var iIndx_List = 0;
  611. var iIndx_Count = 0;
  612. foreach (string pmkey in dicQueryPm.Keys)
  613. {
  614. if (pmkey == EasyNetGlobalConstWord.PAGEINDEX)
  615. {
  616. var sp = new SqlParameter
  617. {
  618. ParameterName = "@" + EasyNetGlobalConstWord.PAGESTART,
  619. Value = (int.Parse(dicQueryPm[EasyNetGlobalConstWord.PAGEINDEX].ToString()) - 1) * int.Parse(dicQueryPm[EasyNetGlobalConstWord.PAGESIZE].ToString()) + 1
  620. };
  621. parameters_List[iIndx_List] = sp;
  622. }
  623. else if (pmkey == EasyNetGlobalConstWord.PAGESIZE)
  624. {
  625. var sp = new SqlParameter
  626. {
  627. ParameterName = "@" + EasyNetGlobalConstWord.PAGEEND,
  628. Value = int.Parse(dicQueryPm[EasyNetGlobalConstWord.PAGEINDEX].ToString()) * int.Parse(dicQueryPm[EasyNetGlobalConstWord.PAGESIZE].ToString())
  629. };
  630. parameters_List[iIndx_List] = sp;
  631. }
  632. else
  633. {
  634. var sp = new SqlParameter
  635. {
  636. ParameterName = "@" + pmkey,
  637. Value = dicQueryPm[pmkey]
  638. };
  639. parameters_List[iIndx_List] = sp;
  640. if (pmkey != EasyNetGlobalConstWord.QUERYSORT)
  641. {
  642. parameters_Count[iIndx_Count] = sp;
  643. iIndx_Count++;
  644. }
  645. }
  646. iIndx_List++;
  647. }
  648. if (bCount)
  649. {
  650. iCount = (int)DbHelperSQL.GetSingle(sPrcName + nameof(Count), parameters_Count);
  651. }
  652. ds = DbHelperSQL.Query(sPrcName, parameters_List);
  653. pageResult.Total = iCount;
  654. pageResult.DataList = ds.Tables[0];
  655. }
  656. catch (Exception e)
  657. {
  658. throw new Exception(e.Message, e);
  659. }
  660. return pageResult;
  661. }
  662. #endregion 分頁查詢返回分頁結果
  663. #region 查询List數據
  664. public static Object QueryBySql(string sql, Object obj, string flag)
  665. {
  666. var oRel = new Object();
  667. var dic = obj as Dictionary<string, object>;
  668. try
  669. {
  670. var parms = DbFactory.CreateDbParameters(dic.Keys.Count);
  671. var idx = 0;
  672. foreach (string key in dic.Keys)
  673. {
  674. parms[idx].ParameterName = key;
  675. parms[idx].Value = dic[key];
  676. idx++;
  677. }
  678. var ds = AdoHelper.DataSet(AdoHelper.ConnectionString, sql.StartsWith("OSP_") ? CommandType.StoredProcedure : CommandType.Text, sql, parms);
  679. if (flag == "one")
  680. {
  681. foreach (DataRow row in ds.Tables[0].Rows)
  682. {
  683. var newData = new Dictionary<string, object>();
  684. foreach (var col in ds.Tables[0].Columns)
  685. {
  686. newData.Add(col.ToString(), row[col.ToString()]);
  687. }
  688. oRel = newData;
  689. break;
  690. }
  691. }
  692. else
  693. {
  694. oRel = ds.Tables[0];
  695. }
  696. }
  697. catch (Exception e)
  698. {
  699. throw new Exception(e.Message, e);
  700. }
  701. return oRel;
  702. }
  703. #endregion 查询List數據
  704. #region 私有方法
  705. private static void GetSqlAndParmForAdd(Object obj, TableInfo tbinfo, out string sql, out IDbDataParameter[] parms)
  706. {
  707. var idx = 0;
  708. var dic = obj as Dictionary<string, object>;
  709. dic = dic.Where(x => !"ExFeild1,ExFeild2,ExFeild3,ExFeild4,ExFeild5,ExFeild6".Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value);
  710. sql = DbEntityUtils.GetInsertByObjSql(tbinfo, dic) + Environment.NewLine;
  711. parms = DbFactory.CreateDbParameters(dic.Keys.Count);
  712. foreach (string _key in dic.Keys)
  713. {
  714. var sValue = dic[_key];
  715. var sValNew = sValue;
  716. if (_key.ToLower() == EasyNetGlobalConstWord.PASSWORD)
  717. {
  718. sValNew = SecurityUtil.Encrypt(sValue.ToString());
  719. }
  720. else if (sValue != null && sValue.GetType() == typeof(string) && sValue.ToString().StartsWith(EasyNetGlobalConstWord.SERIALNUMBER))
  721. {
  722. var saPm = sValue.ToString().Split('|');
  723. var sMidifyUser = "";
  724. if (dic.ContainsKey(EasyNetGlobalConstWord.MODIFYUSER))
  725. {
  726. sMidifyUser = dic[EasyNetGlobalConstWord.MODIFYUSER].ToString();
  727. }
  728. sValNew = SerialNumber.GetMaxNumberByType(saPm[1].ToString(), saPm[2].ToString(), SerialNumber.GetMaxNumberType(saPm[3].ToString()), sMidifyUser, int.Parse(saPm[4].ToString()), saPm.Length > 5 ? saPm[5] : "", saPm.Length > 6 ? saPm[6] : ""); //獲取最大編號
  729. if (sValue.ToString().StartsWith(EasyNetGlobalConstWord.SERIALNUMBER + "_"))
  730. {
  731. sValNew = sValNew.ToString() + SerialNumber.Pcheck(sValNew.ToString());
  732. }
  733. else if (sValue.ToString().StartsWith(EasyNetGlobalConstWord.SERIALNUMBER + "+"))
  734. {
  735. var saLen = saPm[0].ToString().Split('+');
  736. var iLen = int.Parse(saLen[1].ToString());
  737. sValNew = sValNew.ToString() + SecurityUtil.GetRandomNumber(iLen);
  738. }
  739. }
  740. sValue = sValNew;
  741. parms[idx].ParameterName = _key;
  742. parms[idx].Value = sValue;
  743. idx++;
  744. }
  745. }
  746. private static void GetSqlAndParmForUpd(Object obj, TableInfo tbinfo, out string sql, out IDbDataParameter[] parms)
  747. {
  748. var idx = 0;
  749. var dic = obj as Dictionary<string, object>;
  750. var values = dic["values"] as Dictionary<string, object>;
  751. var whkeys = dic["keys"] as Dictionary<string, object>;
  752. sql = DbEntityUtils.GetUpdateByObjSql(tbinfo, ref values, whkeys) + System.Environment.NewLine;
  753. var dicParms = DbFactory.ApllyDic(values, whkeys);
  754. parms = DbFactory.CreateDbParameters(dicParms.Keys.Count);
  755. foreach (string key in dicParms.Keys)
  756. {
  757. var sValue = dicParms[key];
  758. var sValNew = sValue;
  759. if (sValue != null && sValue.GetType() == typeof(string) && sValue.ToString().StartsWith(EasyNetGlobalConstWord.SERIALNUMBER))
  760. {
  761. var saPm = sValue.ToString().Split('|');
  762. var sMidifyUser = "";
  763. if (dic.ContainsKey(EasyNetGlobalConstWord.MODIFYUSER))
  764. {
  765. sMidifyUser = dic[EasyNetGlobalConstWord.MODIFYUSER].ToString();
  766. }
  767. sValNew = SerialNumber.GetMaxNumberByType(saPm[1].ToString(), saPm[2].ToString(), SerialNumber.GetMaxNumberType(saPm[3].ToString()), sMidifyUser, int.Parse(saPm[4].ToString()), saPm.Length > 5 ? saPm[5] : "", saPm.Length > 6 ? saPm[6] : ""); //獲取最大編號
  768. if (sValue.ToString().StartsWith(EasyNetGlobalConstWord.SERIALNUMBER + "_"))
  769. {
  770. sValNew = sValNew.ToString() + SerialNumber.Pcheck(sValNew.ToString());
  771. }
  772. else if (sValue.ToString().StartsWith(EasyNetGlobalConstWord.SERIALNUMBER + "+"))
  773. {
  774. var saLen = saPm[0].ToString().Split('+');
  775. var iLen = int.Parse(saLen[1].ToString());
  776. sValNew = sValNew.ToString() + SecurityUtil.GetRandomNumber(iLen);
  777. }
  778. }
  779. sValue = sValNew;
  780. parms[idx].ParameterName = key;
  781. parms[idx].Value = sValue;
  782. idx++;
  783. }
  784. }
  785. private static void GetSqlAndParmForDel(Object owh, TableInfo tbinfo, out string sql, out IDbDataParameter[] parms)
  786. {
  787. var idx = 0;
  788. var dic = owh as Dictionary<string, object>;
  789. sql = DbEntityUtils.GetDeleteByObjSql(tbinfo, dic) + System.Environment.NewLine;
  790. parms = DbFactory.CreateDbParameters(dic.Keys.Count);
  791. foreach (string _key in dic.Keys)
  792. {
  793. parms[idx].ParameterName = _key;
  794. parms[idx].Value = dic[_key];
  795. idx++;
  796. }
  797. }
  798. private static int ExecuteInsert(Object data, IDbTransaction tran)
  799. {
  800. IDbDataParameter[] parms = null;
  801. var sSql = "";
  802. var iVal = 0;
  803. var oEntity = data as Dictionary<string, object>;
  804. foreach (string key in oEntity.Keys)
  805. {
  806. var entity = EntityHelper.GetEntity(key);
  807. var properties = ReflectionHelper.GetProperties(entity.GetType());
  808. var tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.DELETE, properties);
  809. if (oEntity[key].GetType() == typeof(Object[]))
  810. {
  811. var saEntity = oEntity[key] as Object[];
  812. foreach (Object jo in saEntity)
  813. {
  814. GetSqlAndParmForAdd(jo, tableInfo, out sSql, out parms);
  815. iVal += Execute(tran, sSql, parms);
  816. }
  817. if (iVal < 0)
  818. {
  819. break;
  820. }
  821. }
  822. else
  823. {
  824. GetSqlAndParmForAdd(oEntity[key], tableInfo, out sSql, out parms);
  825. iVal += Execute(tran, sSql, parms);
  826. }
  827. }
  828. return iVal;
  829. }
  830. private static int ExecuteUpdate(Object data, IDbTransaction tran)
  831. {
  832. IDbDataParameter[] parms = null;
  833. var sSql = "";
  834. var iVal = 0;
  835. var oEntity = data as Dictionary<string, object>;
  836. foreach (string key in oEntity.Keys)
  837. {
  838. var entity = EntityHelper.GetEntity(key);
  839. var properties = ReflectionHelper.GetProperties(entity.GetType());
  840. var tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.DELETE, properties);
  841. if (oEntity[key].GetType() == typeof(Object[]))
  842. {
  843. var saEntity = oEntity[key] as Object[];
  844. foreach (Object jo in saEntity)
  845. {
  846. GetSqlAndParmForUpd(jo, tableInfo, out sSql, out parms);
  847. iVal += Execute(tran, sSql, parms);
  848. }
  849. if (iVal < 0)
  850. {
  851. break;
  852. }
  853. }
  854. else
  855. {
  856. GetSqlAndParmForUpd(oEntity[key], tableInfo, out sSql, out parms);
  857. iVal += Execute(tran, sSql, parms);
  858. }
  859. }
  860. return iVal;
  861. }
  862. private static int ExecuteDelete(Object data, IDbTransaction tran)
  863. {
  864. IDbDataParameter[] parms = null;
  865. var sSql = "";
  866. var iVal = 0;
  867. var oEntity = data as Dictionary<string, object>;
  868. foreach (string key in oEntity.Keys)
  869. {
  870. var entity = EntityHelper.GetEntity(key);
  871. var properties = ReflectionHelper.GetProperties(entity.GetType());
  872. var tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.DELETE, properties);
  873. if (oEntity[key].GetType() == typeof(Object[]))
  874. {
  875. var saEntity = oEntity[key] as Object[];
  876. foreach (Object jo in saEntity)
  877. {
  878. GetSqlAndParmForDel(jo, tableInfo, out sSql, out parms);
  879. iVal += Execute(tran, sSql, parms);
  880. }
  881. }
  882. else
  883. {
  884. GetSqlAndParmForDel(oEntity[key], tableInfo, out sSql, out parms);
  885. iVal += Execute(tran, sSql, parms);
  886. }
  887. }
  888. return iVal;
  889. }
  890. private static int Execute(IDbTransaction transaction, string sql, IDbDataParameter[] parms)
  891. {
  892. var iVal = AdoHelper.ExecuteNonQuery(transaction, sql.StartsWith("OSP_") ? CommandType.StoredProcedure : CommandType.Text, sql, parms);
  893. return iVal;
  894. }
  895. private IDbConnection GetConnection(string sKey = null)
  896. {
  897. //獲取資料庫連接,如果開啟了事務,從事務中獲取
  898. IDbConnection connection = null;
  899. if (m_Transaction != null)
  900. {
  901. connection = m_Transaction.Connection;
  902. }
  903. else
  904. {
  905. connection = DbFactory.CreateDbConnection(AdoHelper.ConnectionString);
  906. }
  907. return connection;
  908. }
  909. private bool GetWillConnectionState()
  910. {
  911. return m_Transaction == null;
  912. }
  913. #endregion 私有方法
  914. }
  915. }