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.

189 lines
5.1 KiB

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