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.

239 lines
6.9 KiB

4 years ago
  1. using Common.Model;
  2. using System;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Text;
  7. using System.Web;
  8. namespace Repository.DAO.Application
  9. {
  10. public class ApplicationDAO : IApplicationDAO
  11. {
  12. private readonly SqlConnection _connection = new SqlConnection();
  13. private void OpenConnection()
  14. {
  15. _connection.ConnectionString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
  16. if (_connection.State == ConnectionState.Open)
  17. _connection.Close();
  18. _connection.Open();
  19. }
  20. private void CloseConnection()
  21. {
  22. if (_connection.State == ConnectionState.Open)
  23. this._connection.Close();
  24. }
  25. public DataSet ExecuteDataset(StringBuilder sql)
  26. {
  27. string sqlString = sql.ToString();
  28. var ds = new DataSet();
  29. SqlDataAdapter da;
  30. try
  31. {
  32. OpenConnection();
  33. da = new SqlDataAdapter(sqlString, _connection);
  34. da.Fill(ds);
  35. da.Dispose();
  36. CloseConnection();
  37. }
  38. catch (Exception ex)
  39. {
  40. throw ex;
  41. }
  42. finally
  43. {
  44. da = null;
  45. CloseConnection();
  46. }
  47. return ds;
  48. }
  49. public DataTable ExecuteDataTable(StringBuilder sql)
  50. {
  51. using (var ds = ExecuteDataset(sql))
  52. {
  53. if (ds == null || ds.Tables.Count == 0)
  54. return null;
  55. return ds.Tables[0];
  56. }
  57. }
  58. public DataRow ExecuteDataRow(StringBuilder sql)
  59. {
  60. using (var ds = ExecuteDataset(sql))
  61. {
  62. if (ds == null || ds.Tables.Count == 0)
  63. return null;
  64. if (ds.Tables[0].Rows.Count == 0)
  65. return null;
  66. return ds.Tables[0].Rows[0];
  67. }
  68. }
  69. public String FilterString(object strVal)
  70. {
  71. var stVal = Convert.ToString(strVal);
  72. var str = FilterQuote(stVal);
  73. if (str.ToLower() != "null")
  74. str = "'" + str + "'";
  75. return str;
  76. }
  77. public String FilterQuoteNative(string strVal)
  78. {
  79. if (string.IsNullOrEmpty(strVal))
  80. {
  81. strVal = "";
  82. }
  83. var str = Encode(strVal.Trim());
  84. if (!string.IsNullOrEmpty(str))
  85. {
  86. str = str.Replace(";", "");
  87. //str = str.Replace(",", "");
  88. str = str.Replace("--", "");
  89. str = str.Replace("'", "");
  90. str = str.Replace("/*", "");
  91. str = str.Replace("*/", "");
  92. str = str.Replace(" select ", "");
  93. str = str.Replace(" insert ", "");
  94. str = str.Replace(" update ", "");
  95. str = str.Replace(" delete ", "");
  96. str = str.Replace(" drop ", "");
  97. str = str.Replace(" truncate ", "");
  98. str = str.Replace(" create ", "");
  99. str = str.Replace(" begin ", "");
  100. str = str.Replace(" end ", "");
  101. str = str.Replace(" char(", "");
  102. str = str.Replace(" exec ", "");
  103. str = str.Replace(" xp_cmd ", "");
  104. str = str.Replace("<script", "");
  105. }
  106. else
  107. {
  108. str = "null";
  109. }
  110. return str;
  111. }
  112. private string Encode(string strVal)
  113. {
  114. var sb = new StringBuilder(HttpUtility.HtmlEncode(HttpUtility.JavaScriptStringEncode(strVal)));
  115. // Selectively allow <b> and <i>
  116. sb.Replace("&lt;b&gt;", "<b>");
  117. sb.Replace("&lt;/b&gt;", "");
  118. sb.Replace("&lt;i&gt;", "<i>");
  119. sb.Replace("&lt;/i&gt;", "");
  120. return sb.ToString();
  121. }
  122. public String FilterQuote(string strVal)
  123. {
  124. if (string.IsNullOrEmpty(strVal))
  125. {
  126. strVal = "";
  127. }
  128. var str = strVal.Trim();
  129. if (!string.IsNullOrEmpty(str))
  130. {
  131. str = str.Replace(";", "");
  132. //str = str.Replace(",", "");
  133. str = str.Replace("--", "");
  134. str = str.Replace("'", "");
  135. str = str.Replace("/*", "");
  136. str = str.Replace("*/", "");
  137. str = str.Replace(" select ", "");
  138. str = str.Replace(" insert ", "");
  139. str = str.Replace(" update ", "");
  140. str = str.Replace(" delete ", "");
  141. str = str.Replace(" drop ", "");
  142. str = str.Replace(" truncate ", "");
  143. str = str.Replace(" create ", "");
  144. str = str.Replace(" begin ", "");
  145. str = str.Replace(" end ", "");
  146. str = str.Replace(" char(", "");
  147. str = str.Replace(" exec ", "");
  148. str = str.Replace(" xp_cmd ", "");
  149. str = str.Replace("<script", "");
  150. }
  151. else
  152. {
  153. str = "null";
  154. }
  155. return str;
  156. }
  157. public DbResponse ParseDbResult(DataTable dt)
  158. {
  159. var res = new DbResponse();
  160. if (dt.Rows.Count > 0)
  161. {
  162. res.ResponseCode = dt.Rows[0][0].ToString();
  163. res.Msg = dt.Rows[0][1].ToString();
  164. res.Id = dt.Rows[0][2].ToString();
  165. if (dt.Columns.Count > 3)
  166. {
  167. res.Extra = dt.Rows[0][3].ToString();
  168. }
  169. if (dt.Columns.Count > 4)
  170. {
  171. res.Extra2 = dt.Rows[0][4].ToString();
  172. }
  173. }
  174. return res;
  175. }
  176. public DbResponse ParseDbResult(StringBuilder sql)
  177. {
  178. return ParseDbResult(ExecuteDataset(sql).Tables[0]);
  179. }
  180. public DataTable GetTable(StringBuilder sql)
  181. {
  182. var ds = new DataSet();
  183. SqlDataAdapter da;
  184. string sqlString = sql.ToString();
  185. try
  186. {
  187. OpenConnection();
  188. da = new SqlDataAdapter(sqlString, _connection);
  189. da.Fill(ds);
  190. da.Dispose();
  191. CloseConnection();
  192. }
  193. catch (Exception ex)
  194. {
  195. throw ex;
  196. }
  197. finally
  198. {
  199. da = null;
  200. CloseConnection();
  201. }
  202. return ds.Tables[0];
  203. }
  204. }
  205. }