MyExtensions.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.ComponentModel.DataAnnotations.Schema;
  2. using System.Data;
  3. using System.Reflection;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace SnapshotWinFormsApp.Tools;
  7. public static class MyExtensions
  8. {
  9. /// <summary>
  10. /// 获取字符串的 md5
  11. /// </summary>
  12. /// <param name="value"> 字符串 </param>
  13. /// <returns> MD5 后的字符串<see cref="string"/>. </returns>
  14. public static string GetMD5(this string value)
  15. {
  16. using (var md5 = MD5.Create())
  17. {
  18. return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(value))).Replace("-", string.Empty);
  19. }
  20. }
  21. public static string GetTableName<T>(this T value) where T : class
  22. {
  23. var tableAttribute = typeof(T).GetCustomAttribute<TableAttribute>();
  24. return tableAttribute?.Name ?? string.Empty;
  25. }
  26. /// <summary>
  27. /// 将DataTable转换为指定类型的实体集合
  28. /// </summary>
  29. /// <typeparam name="T">实体类型</typeparam>
  30. /// <param name="dt">DataTable</param>
  31. /// <returns>实体集合</returns>
  32. public static List<T> ToList<T>(this DataTable dt) where T : new()
  33. {
  34. List<T> list = new List<T>();
  35. if (dt == null || dt.Rows.Count == 0)
  36. return list;
  37. PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
  38. foreach (DataRow row in dt.Rows)
  39. {
  40. T item = new T();
  41. foreach (PropertyInfo property in properties)
  42. {
  43. if (dt.Columns.Contains(property.Name) && !row.IsNull(property.Name))
  44. {
  45. object value = Convert.ChangeType(row[property.Name], property.PropertyType);
  46. property.SetValue(item, value, null);
  47. }
  48. }
  49. list.Add(item);
  50. }
  51. return list;
  52. }
  53. public static string ToSqlInsert<T>(this T entity) where T : class
  54. {
  55. if (entity == null)
  56. throw new ArgumentNullException(nameof(entity));
  57. var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  58. var tableName = entity.GetTableName();
  59. var columns = new StringBuilder();
  60. var values = new StringBuilder();
  61. foreach (var property in properties)
  62. {
  63. if (!property.CanWrite)
  64. continue;
  65. var columnName = property.Name;
  66. var value = property.GetValue(entity);
  67. if (value == null)
  68. {
  69. //values.Append("NULL, ");
  70. continue;
  71. }
  72. if (property.PropertyType == typeof(string) || property.PropertyType == typeof(double))
  73. {
  74. value = $"'{value.ToString().Replace("'", "''")}'";
  75. }
  76. else if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
  77. {
  78. value = $"'{(DateTime)value:yyyy-MM-dd hh:mm:ss.ffffff}'";
  79. }
  80. else if (property.PropertyType.BaseType == typeof(Enum))
  81. {
  82. value = $"{(int)value}";
  83. }
  84. else
  85. {
  86. value = value.ToString();
  87. }
  88. columns.Append($"\"{columnName}\", ");
  89. values.Append($"{value}, ");
  90. }
  91. if (columns.Length == 0)
  92. throw new InvalidOperationException("没有可插入的属性。");
  93. // 移除最后的逗号和空格
  94. columns.Length -= 2;
  95. values.Length -= 2;
  96. var sql = $"INSERT INTO {tableName} ({columns}) VALUES ({values});";
  97. return sql;
  98. }
  99. }