DbContextExtensions.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using YLShipBuildLandMap.Services.SystemSetting;
  2. using YLShipBuildLandMap.Entity.ViewModel;
  3. using System.Runtime.InteropServices.ComTypes;
  4. using System.Data;
  5. using Microsoft.EntityFrameworkCore.Infrastructure;
  6. using System.Data.Common;
  7. using System.Reflection;
  8. using Microsoft.Data.SqlClient;
  9. using Microsoft.EntityFrameworkCore;
  10. using System.Collections.Generic;
  11. using System;
  12. using Bowin.Common.Data;
  13. namespace YLShipBuildLandMap.Services
  14. {
  15. public static class DbContextExtensions
  16. {
  17. private static void CombineParams(ref DbCommand command, params object[] parameters)
  18. {
  19. if (parameters != null)
  20. {
  21. foreach (SqlParameter parameter in parameters)
  22. {
  23. if (!parameter.ParameterName.Contains("@"))
  24. parameter.ParameterName = $"@{parameter.ParameterName}";
  25. command.Parameters.Add(parameter);
  26. }
  27. }
  28. }
  29. private static DbCommand CreateCommand(DatabaseFacade facade, string sql, out DbConnection dbConn, params object[] parameters)
  30. {
  31. DbConnection conn = facade.GetDbConnection();
  32. dbConn = conn;
  33. conn.Open();
  34. DbCommand cmd = conn.CreateCommand();
  35. if (facade.IsSqlServer())
  36. {
  37. cmd.CommandText = sql;
  38. CombineParams(ref cmd, parameters);
  39. }
  40. return cmd;
  41. }
  42. public static DataTable SqlQuery(this DatabaseFacade facade, string sql, params object[] parameters)
  43. {
  44. DbCommand cmd = CreateCommand(facade, sql, out DbConnection conn, parameters);
  45. DbDataReader reader = cmd.ExecuteReader();
  46. DataTable dt = new DataTable();
  47. try
  48. {
  49. dt.Load(reader);
  50. reader.Close();
  51. conn.Close();
  52. }
  53. catch (System.Data.DataException e)
  54. {
  55. System.Data.DataRow[] rowsInError;
  56. System.Text.StringBuilder sbError = new System.Text.StringBuilder();
  57. // Test if the table has errors. If not, skip it.
  58. if (dt.HasErrors)
  59. {
  60. // Get an array of all rows with errors.
  61. rowsInError = dt.GetErrors();
  62. // Print the error of each column in each row.
  63. for (int i = 0; i < rowsInError.Length; i++)
  64. {
  65. foreach (System.Data.DataColumn column in dt.Columns)
  66. {
  67. sbError.Append(column.ColumnName + " " + rowsInError[i].GetColumnError(column));
  68. }
  69. // Clear the row errors
  70. rowsInError[i].ClearErrors();
  71. }
  72. }
  73. }
  74. return dt;
  75. }
  76. public static IEnumerable<T> SqlQuery<T>(this DatabaseFacade facade, string sql, params object[] parameters) where T : class, new()
  77. {
  78. DataTable dt = SqlQuery(facade, sql, parameters);
  79. return dt.ToEnumerable<T>();
  80. }
  81. public static IEnumerable<T> ToEnumerable<T>(this DataTable dt) where T : class, new()
  82. {
  83. PropertyInfo[] propertyInfos = typeof(T).GetProperties();
  84. T[] ts = new T[dt.Rows.Count];
  85. int i = 0;
  86. foreach (DataRow row in dt.Rows)
  87. {
  88. T t = new T();
  89. foreach (PropertyInfo p in propertyInfos)
  90. {
  91. if (dt.Columns.IndexOf(p.Name) != -1 && row[p.Name] != DBNull.Value)
  92. p.SetValue(t, row[p.Name], null);
  93. }
  94. ts[i] = t;
  95. i++;
  96. }
  97. return ts;
  98. }
  99. }
  100. }