DbContextExtensions.cs 3.7 KB

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