MyExtensions.cs 3.3 KB

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