You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.4 KiB

  1. using CustomerOnlineV2.Common.Helper;
  2. using CustomerOnlineV2.Common.Models;
  3. using Microsoft.Data.SqlClient;
  4. using System.Data;
  5. namespace CustomerOnlineV2.Repository.Helper
  6. {
  7. public class DBHelper
  8. {
  9. private SqlConnection _connection;
  10. public DBHelper()
  11. {
  12. Init();
  13. }
  14. public void Init()
  15. {
  16. _connection = new SqlConnection(GetConnectionString());
  17. }
  18. public void OpenConnection()
  19. {
  20. if (_connection.State == ConnectionState.Open)
  21. _connection.Close();
  22. _connection.Open();
  23. }
  24. public void CloseConnection()
  25. {
  26. if (_connection.State == ConnectionState.Open)
  27. this._connection.Close();
  28. }
  29. public string GetConnectionString()
  30. {
  31. return Common.Helper.Utilities.ReadAppSettings("ConnectionStrings:ApplicationDb");
  32. }
  33. public String FilterString(string strVal)
  34. {
  35. var str = FilterQuote(strVal);
  36. if (str.ToLower() != "null")
  37. str = "'" + str + "'";
  38. return str;
  39. }
  40. public String FilterQuote(string strVal)
  41. {
  42. if (string.IsNullOrEmpty(strVal))
  43. {
  44. strVal = "";
  45. }
  46. var str = strVal.Trim();
  47. if (!string.IsNullOrEmpty(str))
  48. {
  49. str = str.Replace(";", "");
  50. //str = str.Replace(",", "");
  51. str = str.Replace("--", "");
  52. str = str.Replace("'", "");
  53. str = str.Replace("/*", "");
  54. str = str.Replace("*/", "");
  55. str = str.Replace(" select ", "");
  56. str = str.Replace(" insert ", "");
  57. str = str.Replace(" update ", "");
  58. str = str.Replace(" delete ", "");
  59. str = str.Replace(" drop ", "");
  60. str = str.Replace(" truncate ", "");
  61. str = str.Replace(" create ", "");
  62. str = str.Replace(" begin ", "");
  63. str = str.Replace(" end ", "");
  64. str = str.Replace(" char(", "");
  65. str = str.Replace(" exec ", "");
  66. str = str.Replace(" xp_cmd ", "");
  67. str = str.Replace("<script", "");
  68. }
  69. else
  70. {
  71. str = "null";
  72. }
  73. return str;
  74. }
  75. public DataTable ExecuteDataTable(string sql)
  76. {
  77. using (var ds = ExecuteDataset(sql))
  78. {
  79. if (ds == null || ds.Tables.Count == 0)
  80. return null;
  81. return ds.Tables[0];
  82. }
  83. }
  84. public DataSet ExecuteDataset(string sql)
  85. {
  86. var ds = new DataSet();
  87. SqlDataAdapter da;
  88. try
  89. {
  90. OpenConnection();
  91. da = new SqlDataAdapter(sql, _connection);
  92. da.Fill(ds);
  93. da.Dispose();
  94. CloseConnection();
  95. }
  96. catch (Exception ex)
  97. {
  98. throw ex;
  99. }
  100. finally
  101. {
  102. da = null;
  103. CloseConnection();
  104. }
  105. return ds;
  106. }
  107. public DataRow ExecuteDataRow(string sql)
  108. {
  109. using (var ds = ExecuteDataset(sql))
  110. {
  111. if (ds == null || ds.Tables.Count == 0)
  112. return null;
  113. if (ds.Tables[0].Rows.Count == 0)
  114. return null;
  115. return ds.Tables[0].Rows[0];
  116. }
  117. }
  118. public CommonResponse ParseDbResult(string sql)
  119. {
  120. DataTable dt = ExecuteDataTable(sql);
  121. return ParseDbResult(dt);
  122. }
  123. public CommonResponse ParseDbResult(DataTable dt)
  124. {
  125. CommonResponse response = new CommonResponse(101, "Error parsing data!");
  126. if (dt == null || dt.Rows.Count == 0)
  127. return response;
  128. response = new CommonResponse(Convert.ToInt16(Utilities.GetRowCellValue(dt.Rows[0][0])), Utilities.GetRowCellValue(dt.Rows[0][1]));
  129. if (dt.Columns.Count > 2)
  130. response.Id = Utilities.GetRowCellValue(dt.Rows[0][2]);
  131. if (dt.Columns.Count > 3)
  132. response.Extra = Utilities.GetRowCellValue(dt.Rows[0][3]);
  133. return response;
  134. }
  135. }
  136. }