SqlHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using System;
  4. using System.Collections.Generic;
  5. using Procurement.ViewModel;
  6. using System.Text.Json;
  7. using Procurement.EntityFrameworkCore.SqlRepositories;
  8. using Procurement.Controllers;
  9. using System.Collections;
  10. using Procurement.Enums;
  11. using Volo.Abp.Domain.Repositories;
  12. using Procurement.Services;
  13. using Newtonsoft.Json.Linq;
  14. using System.Data;
  15. using System.Data.SqlClient;
  16. namespace Procurement.Helpers
  17. {
  18. public class SqlHelper
  19. {
  20. /// <summary>
  21. /// 构建mysql参数数组
  22. /// </summary>
  23. /// <param name="json"></param>
  24. /// <returns></returns>
  25. //public static async Task<SqlParameterViewModel[]> CreateSqlParameters(ISqlRepository sqlRepository,string proc,JsonElement json)
  26. //{
  27. // Dictionary<string, JsonElement> dict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
  28. // Common common = new Common(sqlRepository);
  29. // var items = await common.GetCommonItems("", "parameterlist", proc, "");
  30. // List<SqlParameterViewModel> listParams = new List<SqlParameterViewModel>();
  31. // foreach(KeyValuePair<string,object> item in items)
  32. // {
  33. // foreach (var d in (ArrayList)item.Value)
  34. // {
  35. // Dictionary<string, object> param = (Dictionary<string, object>)d;
  36. // string paramMode = param["ParamMode"].ToString().ToUpper();
  37. // string paramName = param["ParamName"].ToString();
  38. // string dataType = param["DataType"].ToString().ToLower();
  39. // object paramValue = null;
  40. // if (dict.ContainsKey(paramName))
  41. // {
  42. // JsonElement valueObj = dict[paramName];
  43. // switch (valueObj.ValueKind)
  44. // {
  45. // case JsonValueKind.Null:
  46. // paramValue = null;
  47. // break;
  48. // case JsonValueKind.Number:
  49. // paramValue = valueObj.GetDouble();
  50. // break;
  51. // case JsonValueKind.False:
  52. // paramValue = false;
  53. // break;
  54. // case JsonValueKind.True:
  55. // paramValue = true;
  56. // break;
  57. // case JsonValueKind.Undefined:
  58. // paramValue = null;
  59. // break;
  60. // case JsonValueKind.String:
  61. // paramValue = valueObj.GetString();
  62. // break;
  63. // }
  64. // }
  65. // SqlParameterViewModel paramModel = new SqlParameterViewModel { ParameterName = "@" + paramName, Value = paramValue, SqlDbType = SqlDbTypes.VarChar };
  66. // if (dataType == "tinyint")
  67. // {
  68. // paramModel.SqlDbType = SqlDbTypes.Int;
  69. // }
  70. // else if (dataType == "int")
  71. // {
  72. // paramModel.SqlDbType = SqlDbTypes.Int;
  73. // }
  74. // else if (dataType == "bigint")
  75. // {
  76. // paramModel.SqlDbType = SqlDbTypes.BigInt;
  77. // }
  78. // else if (dataType == "decimal")
  79. // {
  80. // paramModel.SqlDbType = SqlDbTypes.Decimal;
  81. // }
  82. // else if (dataType == "date" || dataType == "datetime")
  83. // {
  84. // paramModel.SqlDbType = SqlDbTypes.DateTime;
  85. // }
  86. // if (paramMode == "OUT")
  87. // {
  88. // paramModel.Direction = SqlParamDirection.Output;
  89. // }
  90. // listParams.Add(paramModel);
  91. // }
  92. // }
  93. // return listParams.ToArray();
  94. //}
  95. /// <summary>
  96. /// 构建sql参数数组
  97. /// </summary>
  98. /// <param name="json"></param>
  99. /// <returns></returns>
  100. public static SqlParameter[] CreateSqlParameters(JsonElement json)
  101. {
  102. Dictionary<string, JsonElement> dict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
  103. List<SqlParameter> listParams = new List<SqlParameter>();
  104. foreach (KeyValuePair<string, JsonElement> d in dict)
  105. {
  106. string paramName = d.Key.ToString();
  107. object paramValue = null;
  108. JsonElement valueObj = dict[paramName];
  109. switch (valueObj.ValueKind)
  110. {
  111. case JsonValueKind.Null:
  112. paramValue = null;
  113. break;
  114. case JsonValueKind.Number:
  115. paramValue = valueObj.GetDouble();
  116. break;
  117. case JsonValueKind.False:
  118. paramValue = false;
  119. break;
  120. case JsonValueKind.True:
  121. paramValue = true;
  122. break;
  123. case JsonValueKind.Undefined:
  124. paramValue = null;
  125. break;
  126. case JsonValueKind.String:
  127. paramValue = valueObj.GetString();
  128. break;
  129. }
  130. SqlParameter param = new SqlParameter { ParameterName = "@" + paramName, Value = paramValue};
  131. listParams.Add(param);
  132. }
  133. return listParams.ToArray();
  134. }
  135. }
  136. }