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.

186 lines
4.9 KiB

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