纽威
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.

484 lines
18 KiB

1 year ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace NFine.Data.Extensions
  12. {
  13. public class DatabaseCommon
  14. {
  15. #region 对象参数转换DbParameter
  16. /// <summary>
  17. /// 对象参数转换DbParameter
  18. /// </summary>
  19. /// <returns></returns>
  20. public static DbParameter[] GetParameter<T>(T entity)
  21. {
  22. IList<DbParameter> parameter = new List<DbParameter>();
  23. DbType dbtype = new DbType();
  24. Type type = entity.GetType();
  25. PropertyInfo[] props = type.GetProperties();
  26. foreach (PropertyInfo pi in props)
  27. {
  28. if (pi.GetValue(entity, null) != null)
  29. {
  30. switch (pi.PropertyType.ToString())
  31. {
  32. case "System.Nullable`1[System.Int32]":
  33. dbtype = DbType.Int32;
  34. break;
  35. case "System.Nullable`1[System.Decimal]":
  36. dbtype = DbType.Decimal;
  37. break;
  38. case "System.Nullable`1[System.DateTime]":
  39. dbtype = DbType.DateTime;
  40. break;
  41. default:
  42. dbtype = DbType.String;
  43. break;
  44. }
  45. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pi.Name, pi.GetValue(entity, null), dbtype));
  46. }
  47. }
  48. return parameter.ToArray();
  49. }
  50. /// <summary>
  51. /// 对象参数转换DbParameter
  52. /// </summary>
  53. /// <returns></returns>
  54. public static DbParameter[] GetParameter(Hashtable ht)
  55. {
  56. IList<DbParameter> parameter = new List<DbParameter>();
  57. DbType dbtype = new DbType();
  58. foreach (string key in ht.Keys)
  59. {
  60. if (ht[key] is DateTime)
  61. dbtype = DbType.DateTime;
  62. else
  63. dbtype = DbType.String;
  64. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + key, ht[key], dbtype));
  65. }
  66. return parameter.ToArray();
  67. }
  68. #endregion
  69. #region 获取实体类自定义信息
  70. /// <summary>
  71. /// 获取实体类主键字段
  72. /// </summary>
  73. /// <returns></returns>
  74. public static object GetKeyField<T>()
  75. {
  76. Type objTye = typeof(T);
  77. string _KeyField = "";
  78. PrimaryKeyAttribute KeyField;
  79. var name = objTye.Name;
  80. foreach (Attribute attr in objTye.GetCustomAttributes(true))
  81. {
  82. KeyField = attr as PrimaryKeyAttribute;
  83. if (KeyField != null)
  84. _KeyField = KeyField.Name;
  85. }
  86. return _KeyField;
  87. }
  88. #region 获取实体类自定义信息
  89. /// <summary>
  90. /// 获取实体类主键字段
  91. /// </summary>
  92. /// <returns></returns>
  93. public static string GetKeyField(string className)
  94. {
  95. Assembly asmb = Assembly.LoadFrom(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "ASE.Entity.dll"));
  96. Type objTye = asmb.GetType(className);
  97. string _KeyField = "";
  98. PrimaryKeyAttribute KeyField;
  99. var name = objTye.Name;
  100. foreach (Attribute attr in objTye.GetCustomAttributes(true))
  101. {
  102. KeyField = attr as PrimaryKeyAttribute;
  103. if (KeyField != null)
  104. _KeyField = KeyField.Name;
  105. }
  106. return _KeyField;
  107. }
  108. #endregion
  109. /// <summary>
  110. /// 获取实体类主键字段
  111. /// </summary>
  112. /// <param name="entity">实体类</param>
  113. /// <returns></returns>
  114. public static object GetKeyFieldValue<T>(T entity)
  115. {
  116. Type objTye = typeof(T);
  117. string _KeyField = "";
  118. PrimaryKeyAttribute KeyField;
  119. var name = objTye.Name;
  120. foreach (Attribute attr in objTye.GetCustomAttributes(true))
  121. {
  122. KeyField = attr as PrimaryKeyAttribute;
  123. if (KeyField != null)
  124. _KeyField = KeyField.Name;
  125. }
  126. PropertyInfo property = objTye.GetProperty(_KeyField);
  127. return property.GetValue(entity, null).ToString();
  128. }
  129. /// <summary>
  130. /// 获取实体类 字段中文名称
  131. /// </summary>
  132. /// <param name="pi">字段属性信息</param>
  133. /// <returns></returns>
  134. public static string GetFieldText(PropertyInfo pi)
  135. {
  136. DisplayNameAttribute descAttr;
  137. string txt = "";
  138. var descAttrs = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true);
  139. if (descAttrs.Any())
  140. {
  141. descAttr = descAttrs[0] as DisplayNameAttribute;
  142. txt = descAttr.DisplayName;
  143. }
  144. else
  145. {
  146. txt = pi.Name;
  147. }
  148. return txt;
  149. }
  150. /// <summary>
  151. /// 获取实体类中文名称
  152. /// </summary>
  153. /// <returns></returns>
  154. public static string GetClassName<T>()
  155. {
  156. Type objTye = typeof(T);
  157. string entityName = "";
  158. var busingessNames = objTye.GetCustomAttributes(true).OfType<DisplayNameAttribute>();
  159. var descriptionAttributes = busingessNames as DisplayNameAttribute[] ?? busingessNames.ToArray();
  160. if (descriptionAttributes.Any())
  161. entityName = descriptionAttributes.ToList()[0].DisplayName;
  162. else
  163. {
  164. entityName = objTye.Name;
  165. }
  166. return entityName;
  167. }
  168. #endregion
  169. #region 拼接 Insert SQL语句
  170. /// <summary>
  171. /// 哈希表生成Insert语句
  172. /// </summary>
  173. /// <param name="tableName">表名</param>
  174. /// <param name="ht">Hashtable</param>
  175. /// <returns>int</returns>
  176. public static StringBuilder InsertSql(string tableName, Hashtable ht)
  177. {
  178. StringBuilder sb = new StringBuilder();
  179. sb.Append(" Insert Into ");
  180. sb.Append(tableName);
  181. sb.Append("(");
  182. StringBuilder sp = new StringBuilder();
  183. StringBuilder sb_prame = new StringBuilder();
  184. foreach (string key in ht.Keys)
  185. {
  186. if (ht[key] != null)
  187. {
  188. sb_prame.Append("," + key);
  189. sp.Append("," + DbHelper.DbParmChar + "" + key);
  190. }
  191. }
  192. sb.Append(sb_prame.ToString().Substring(1, sb_prame.ToString().Length - 1) + ") Values (");
  193. sb.Append(sp.ToString().Substring(1, sp.ToString().Length - 1) + ")");
  194. return sb;
  195. }
  196. /// <summary>
  197. /// 泛型方法,反射生成InsertSql语句
  198. /// </summary>
  199. /// <param name="entity">实体类</param>
  200. /// <returns>int</returns>
  201. public static StringBuilder InsertSql<T>(T entity)
  202. {
  203. Type type = entity.GetType();
  204. StringBuilder sb = new StringBuilder();
  205. sb.Append(" Insert Into ");
  206. sb.Append(type.Name);
  207. sb.Append("(");
  208. StringBuilder sp = new StringBuilder();
  209. StringBuilder sb_prame = new StringBuilder();
  210. PropertyInfo[] props = type.GetProperties();
  211. foreach (PropertyInfo prop in props)
  212. {
  213. if (prop.GetValue(entity, null) != null)
  214. {
  215. sb_prame.Append("," + (prop.Name));
  216. sp.Append("," + DbHelper.DbParmChar + "" + (prop.Name));
  217. }
  218. }
  219. sb.Append(sb_prame.ToString().Substring(1, sb_prame.ToString().Length - 1) + ") Values (");
  220. sb.Append(sp.ToString().Substring(1, sp.ToString().Length - 1) + ")");
  221. return sb;
  222. }
  223. #endregion
  224. #region 拼接 Update SQL语句
  225. /// <summary>
  226. /// 哈希表生成UpdateSql语句
  227. /// </summary>
  228. /// <param name="tableName">表名</param>
  229. /// <param name="ht">Hashtable</param>
  230. /// <param name="pkName">主键</param>
  231. /// <returns></returns>
  232. public static StringBuilder UpdateSql(string tableName, Hashtable ht, string pkName)
  233. {
  234. StringBuilder sb = new StringBuilder();
  235. sb.Append(" Update ");
  236. sb.Append(tableName);
  237. sb.Append(" Set ");
  238. bool isFirstValue = true;
  239. foreach (string key in ht.Keys)
  240. {
  241. if (ht[key] != null && pkName != key)
  242. {
  243. if (isFirstValue)
  244. {
  245. isFirstValue = false;
  246. sb.Append(key);
  247. sb.Append("=");
  248. sb.Append(DbHelper.DbParmChar + key);
  249. }
  250. else
  251. {
  252. sb.Append("," + key);
  253. sb.Append("=");
  254. sb.Append(DbHelper.DbParmChar + key);
  255. }
  256. }
  257. }
  258. sb.Append(" Where ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
  259. return sb;
  260. }
  261. /// <summary>
  262. /// 泛型方法,反射生成UpdateSql语句
  263. /// </summary>
  264. /// <param name="entity">实体类</param>
  265. /// <param name="pkName">主键</param>
  266. /// <returns>int</returns>
  267. public static StringBuilder UpdateSql<T>(T entity, string pkName)
  268. {
  269. Type type = entity.GetType();
  270. PropertyInfo[] props = type.GetProperties();
  271. StringBuilder sb = new StringBuilder();
  272. sb.Append(" Update ");
  273. sb.Append(type.Name);
  274. sb.Append(" Set ");
  275. bool isFirstValue = true;
  276. foreach (PropertyInfo prop in props)
  277. {
  278. if (prop.GetValue(entity, null) != null && GetKeyField<T>().ToString() != prop.Name)
  279. {
  280. if (isFirstValue)
  281. {
  282. isFirstValue = false;
  283. sb.Append(prop.Name);
  284. sb.Append("=");
  285. sb.Append(DbHelper.DbParmChar + prop.Name);
  286. }
  287. else
  288. {
  289. sb.Append("," + prop.Name);
  290. sb.Append("=");
  291. sb.Append(DbHelper.DbParmChar + prop.Name);
  292. }
  293. }
  294. }
  295. sb.Append(" Where ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
  296. return sb;
  297. }
  298. /// <summary>
  299. /// 泛型方法,反射生成UpdateSql语句
  300. /// </summary>
  301. /// <param name="entity">实体类</param>
  302. /// <returns>int</returns>
  303. public static StringBuilder UpdateSql<T>(T entity)
  304. {
  305. string pkName = GetKeyField<T>().ToString();
  306. Type type = entity.GetType();
  307. PropertyInfo[] props = type.GetProperties();
  308. StringBuilder sb = new StringBuilder();
  309. sb.Append("Update ");
  310. sb.Append(type.Name);
  311. sb.Append(" Set ");
  312. bool isFirstValue = true;
  313. foreach (PropertyInfo prop in props)
  314. {
  315. if (prop.GetValue(entity, null) != null && pkName != prop.Name)
  316. {
  317. if (isFirstValue)
  318. {
  319. isFirstValue = false;
  320. sb.Append(prop.Name);
  321. sb.Append("=");
  322. sb.Append(DbHelper.DbParmChar + prop.Name);
  323. }
  324. else
  325. {
  326. sb.Append("," + prop.Name);
  327. sb.Append("=");
  328. sb.Append(DbHelper.DbParmChar + prop.Name);
  329. }
  330. }
  331. }
  332. sb.Append(" Where ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
  333. return sb;
  334. }
  335. #endregion
  336. #region 拼接 Delete SQL语句
  337. /// <summary>
  338. /// 拼接删除SQL语句
  339. /// </summary>
  340. /// <param name="tableName">表名</param>
  341. /// <param name="pkName">字段主键</param>
  342. /// <returns></returns>
  343. public static StringBuilder DeleteSql(string tableName, string pkName)
  344. {
  345. return new StringBuilder("Delete From " + tableName + " Where " + pkName + " = " + DbHelper.DbParmChar + pkName + "");
  346. }
  347. /// <summary>
  348. /// 拼接删除SQL语句
  349. /// </summary>
  350. /// <param name="tableName">表名</param>
  351. /// <param name="ht">多参数</param>
  352. /// <returns></returns>
  353. public static StringBuilder DeleteSql(string tableName, Hashtable ht)
  354. {
  355. StringBuilder sb = new StringBuilder("Delete From " + tableName + " Where 1=1");
  356. foreach (string key in ht.Keys)
  357. {
  358. sb.Append(" AND " + key + " = " + DbHelper.DbParmChar + "" + key + "");
  359. }
  360. return sb;
  361. }
  362. /// <summary>
  363. /// 拼接删除SQL语句
  364. /// </summary>
  365. /// <param name="entity">实体类</param>
  366. /// <returns></returns>
  367. public static StringBuilder DeleteSql<T>(T entity)
  368. {
  369. Type type = entity.GetType();
  370. PropertyInfo[] props = type.GetProperties();
  371. StringBuilder sb = new StringBuilder("Delete From " + type.Name + " Where 1=1");
  372. foreach (PropertyInfo prop in props)
  373. {
  374. if (prop.GetValue(entity, null) != null)
  375. {
  376. sb.Append(" AND " + prop.Name + " = " + DbHelper.DbParmChar + "" + prop.Name + "");
  377. }
  378. }
  379. return sb;
  380. }
  381. #endregion
  382. #region 拼接 Select SQL语句
  383. /// <summary>
  384. /// 拼接 查询 SQL语句
  385. /// </summary>
  386. /// <returns></returns>
  387. public static StringBuilder SelectSql<T>() where T : new()
  388. {
  389. string tableName = typeof(T).Name;
  390. PropertyInfo[] props = GetProperties(new T().GetType());
  391. StringBuilder sbColumns = new StringBuilder();
  392. foreach (PropertyInfo prop in props)
  393. {
  394. string propertytype = prop.PropertyType.ToString();
  395. sbColumns.Append(prop.Name + ",");
  396. }
  397. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  398. string strSql = "SELECT {0} FROM {1} WHERE 1=1 ";
  399. strSql = string.Format(strSql, sbColumns.ToString(), tableName + " ");
  400. return new StringBuilder(strSql);
  401. }
  402. /// <summary>
  403. /// 拼接 查询 SQL语句
  404. /// </summary>
  405. /// <param name="Top">显示条数</param>
  406. /// <returns></returns>
  407. public static StringBuilder SelectSql<T>(int Top) where T : new()
  408. {
  409. string tableName = typeof(T).Name;
  410. PropertyInfo[] props = GetProperties(new T().GetType());
  411. StringBuilder sbColumns = new StringBuilder();
  412. foreach (PropertyInfo prop in props)
  413. {
  414. sbColumns.Append(prop.Name + ",");
  415. }
  416. if (sbColumns.Length > 0) sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
  417. string strSql = "SELECT top {0} {1} FROM {2} WHERE 1=1 ";
  418. strSql = string.Format(strSql, Top, sbColumns.ToString(), tableName + " ");
  419. return new StringBuilder(strSql);
  420. }
  421. /// <summary>
  422. /// 拼接 查询 SQL语句
  423. /// </summary>
  424. /// <param name="tableName">表名</param>
  425. /// <returns></returns>
  426. public static StringBuilder SelectSql(string tableName)
  427. {
  428. StringBuilder strSql = new StringBuilder();
  429. strSql.Append("SELECT * FROM " + tableName + " WHERE 1=1 ");
  430. return strSql;
  431. }
  432. /// <summary>
  433. /// 拼接 查询 SQL语句
  434. /// </summary>
  435. /// <param name="tableName">表名</param>
  436. /// <param name="Top">显示条数</param>
  437. /// <returns></returns>
  438. public static StringBuilder SelectSql(string tableName, int Top)
  439. {
  440. StringBuilder strSql = new StringBuilder();
  441. strSql.Append("SELECT top " + Top + " * FROM " + tableName + " WHERE 1=1 ");
  442. return strSql;
  443. }
  444. /// <summary>
  445. /// 拼接 查询条数 SQL语句
  446. /// </summary>
  447. /// <returns></returns>
  448. public static StringBuilder SelectCountSql<T>() where T : new()
  449. {
  450. string tableName = typeof(T).Name;//获取表名
  451. return new StringBuilder("SELECT Count(1) FROM " + tableName + " WHERE 1=1 ");
  452. }
  453. /// <summary>
  454. /// 拼接 查询最大数 SQL语句
  455. /// </summary>
  456. /// <param name="propertyName">属性字段</param>
  457. /// <returns></returns>
  458. public static StringBuilder SelectMaxSql<T>(string propertyName) where T : new()
  459. {
  460. string tableName = typeof(T).Name;//获取表名
  461. return new StringBuilder("SELECT MAX(" + propertyName + ") FROM " + tableName + " WHERE 1=1 ");
  462. }
  463. #endregion
  464. #region 扩展
  465. /// <summary>
  466. /// 获取访问元素
  467. /// </summary>
  468. /// <param name="type"></param>
  469. /// <returns></returns>
  470. public static PropertyInfo[] GetProperties(Type type)
  471. {
  472. return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
  473. }
  474. #endregion
  475. }
  476. }