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.

337 lines
9.6 KiB

1 year ago
1 year 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.RegularExpressions;
  7. namespace Repository
  8. {
  9. public class Dao
  10. {
  11. private string GetConnectionString()
  12. {
  13. return ConfigurationManager.ConnectionStrings["RemittanceDB"].ConnectionString;
  14. }
  15. /// <summary>
  16. /// Get Command TimeOut
  17. /// </summary>
  18. /// <returns></returns>
  19. private int GetCommandTimeOut()
  20. {
  21. int cto = 0;
  22. try
  23. {
  24. int.TryParse(ConfigurationManager.AppSettings["cto"].ToString(), out cto);
  25. if (cto == 0)
  26. cto = 30;
  27. }
  28. catch
  29. {
  30. cto = 30;
  31. }
  32. return cto;
  33. }
  34. /// <summary>
  35. /// Execute Data set
  36. /// </summary>
  37. /// <param name="sql"></param>
  38. /// <returns></returns>
  39. public DataSet ExecuteDataset(string sql)
  40. {
  41. var ds = new DataSet();
  42. var conStr = GetConnectionString();
  43. using (var con = new SqlConnection(conStr))
  44. {
  45. var cmd = new SqlCommand(sql, con);
  46. cmd.CommandTimeout = GetCommandTimeOut();
  47. SqlDataAdapter da;
  48. try
  49. {
  50. da = new SqlDataAdapter(cmd);
  51. da.Fill(ds);
  52. da.Dispose();
  53. }
  54. catch (Exception ex)
  55. {
  56. throw ex;
  57. }
  58. finally
  59. {
  60. da = null;
  61. cmd.Dispose();
  62. }
  63. return ds;
  64. }
  65. }
  66. /// <summary>
  67. /// Filter String to check Sql Injection
  68. /// </summary>
  69. /// <param name="strVal"></param>
  70. /// <returns></returns>
  71. public String FilterString(string strVal)
  72. {
  73. var str = FilterQuote(strVal);
  74. if (str.ToLower() != "null")
  75. str = "'" + str + "'";
  76. return str;
  77. }
  78. /// <summary>
  79. /// Filter String to check Sql Injection
  80. /// </summary>
  81. /// <param name="strVal"></param>
  82. /// <returns></returns>
  83. public String FilterStringUnicode(string strVal)
  84. {
  85. var str = FilterQuote(strVal);
  86. if (str.ToLower() != "null")
  87. str = "N'" + str + "'";
  88. return str;
  89. }
  90. /// <summary>
  91. /// Filter Xml Node String to check Sql injection
  92. /// </summary>
  93. /// <param name="strVal"></param>
  94. /// <returns></returns>
  95. public String FilterXmlNodeString(string strVal)
  96. {
  97. var str = FilterQuote(strVal);
  98. return str;
  99. }
  100. /// <summary>
  101. /// Filter Quote to check sql Injection
  102. /// </summary>
  103. /// <param name="strVal"></param>
  104. /// <returns></returns>
  105. public String FilterQuote(string strVal)
  106. {
  107. if (string.IsNullOrEmpty(strVal))
  108. {
  109. strVal = "";
  110. }
  111. var str = strVal.Trim();
  112. if (!string.IsNullOrEmpty(str))
  113. {
  114. str = str.Replace(";", "");
  115. //str = str.Replace(",", "");
  116. str = str.Replace("--", "");
  117. str = str.Replace("'", "");
  118. str = str.Replace("/*", "");
  119. str = str.Replace("*/", "");
  120. str = Regex.Replace(str, " select ", string.Empty, RegexOptions.IgnoreCase);
  121. str = Regex.Replace(str, " insert ", string.Empty, RegexOptions.IgnoreCase);
  122. str = Regex.Replace(str, " update ", string.Empty, RegexOptions.IgnoreCase);
  123. str = Regex.Replace(str, " delete ", string.Empty, RegexOptions.IgnoreCase);
  124. str = Regex.Replace(str, " drop ", string.Empty, RegexOptions.IgnoreCase);
  125. str = Regex.Replace(str, " truncate ", string.Empty, RegexOptions.IgnoreCase);
  126. str = Regex.Replace(str, " create ", string.Empty, RegexOptions.IgnoreCase);
  127. str = Regex.Replace(str, " begin ", string.Empty, RegexOptions.IgnoreCase);
  128. str = Regex.Replace(str, " end ", string.Empty, RegexOptions.IgnoreCase);
  129. str = Regex.Replace(str, " char ", string.Empty, RegexOptions.IgnoreCase);
  130. str = Regex.Replace(str, " exec ", string.Empty, RegexOptions.IgnoreCase);
  131. str = Regex.Replace(str, " xp_cmd ", string.Empty, RegexOptions.IgnoreCase);
  132. str = Regex.Replace(str, @"<.*?>", string.Empty);
  133. }
  134. else
  135. {
  136. str = "null";
  137. }
  138. return str;
  139. }
  140. /// <summary>
  141. /// Parse DbResult
  142. /// </summary>
  143. /// <param name="dt"></param>
  144. /// <returns></returns>
  145. public DbResult ParseDbResult(DataTable dt)
  146. {
  147. var res = new DbResult();
  148. if (dt.Rows.Count > 0)
  149. {
  150. res.ResponseCode = dt.Rows[0][0].ToString();
  151. res.Msg = dt.Rows[0][1].ToString();
  152. res.Id = dt.Rows[0][2].ToString();
  153. if (dt.Columns.Count > 3)
  154. {
  155. res.Extra = dt.Rows[0][3].ToString();
  156. }
  157. if (dt.Columns.Count > 4)
  158. {
  159. res.Extra2 = dt.Rows[0][4].ToString();
  160. }
  161. if (dt.Columns.Count > 4)
  162. {
  163. res.Extra2 = dt.Rows[0][4].ToString();
  164. }
  165. if (dt.Columns.Count > 5)
  166. {
  167. res.Extra3 = dt.Rows[0][5].ToString();
  168. }
  169. }
  170. return res;
  171. }
  172. /// <summary>
  173. /// Parse DbResult
  174. /// </summary>
  175. /// <param name="sql"></param>
  176. /// <returns></returns>
  177. public DbResult ParseDbResult(string sql)
  178. {
  179. return ParseDbResult(ExecuteDataset(sql).Tables[0]);
  180. }
  181. /// <summary>
  182. /// Execute sql and return Data Table
  183. /// </summary>
  184. /// <param name="sql"></param>
  185. /// <returns></returns>
  186. public DataTable ExecuteDataTable(string sql)
  187. {
  188. using (var ds = ExecuteDataset(sql))
  189. {
  190. if (ds == null || ds.Tables.Count == 0)
  191. return null;
  192. return ds.Tables[0];
  193. }
  194. }
  195. /// <summary>
  196. /// Execute sql and return Data Row
  197. /// </summary>
  198. /// <param name="sql"></param>
  199. /// <returns></returns>
  200. public DataRow ExecuteDataRow(string sql)
  201. {
  202. using (var ds = ExecuteDataset(sql))
  203. {
  204. if (ds == null || ds.Tables.Count == 0)
  205. return null;
  206. if (ds.Tables[0].Rows.Count == 0)
  207. return null;
  208. return ds.Tables[0].Rows[0];
  209. }
  210. }
  211. /// <summary>
  212. /// Convert data row To String
  213. /// </summary>
  214. /// <param name="dr"></param>
  215. /// <returns></returns>
  216. public string ConvertDrToString(object dr)
  217. {
  218. if (dr != DBNull.Value)
  219. {
  220. return dr.ToString();
  221. }
  222. return string.Empty;
  223. }
  224. /// <summary>
  225. /// Convert Datarow To Decimal
  226. /// </summary>
  227. /// <param name="dr"></param>
  228. /// <returns></returns>
  229. public Decimal ConvertDrToDecimal(object dr)
  230. {
  231. if (dr != DBNull.Value)
  232. {
  233. return Convert.ToDecimal(dr.ToString());
  234. }
  235. return 0;
  236. }
  237. /// <summary>
  238. /// Convert Datarow ToInt64
  239. /// </summary>
  240. /// <param name="dr"></param>
  241. /// <returns></returns>
  242. public Int64 ConvertDrToInt64(object dr)
  243. {
  244. if (dr != DBNull.Value)
  245. {
  246. return Convert.ToInt64(dr.ToString());
  247. }
  248. return 0;
  249. }
  250. /// <summary>
  251. /// Convert Datarow To Date
  252. /// </summary>
  253. /// <param name="dr"></param>
  254. /// <returns></returns>
  255. public DateTime ConvertDrToDate(object dr)
  256. {
  257. if (dr != DBNull.Value)
  258. {
  259. return Convert.ToDateTime(dr.ToString());
  260. }
  261. return DateTime.MinValue;
  262. }
  263. /// <summary>
  264. /// Convert Datarow To Int
  265. /// </summary>
  266. /// <param name="dr"></param>
  267. /// <returns></returns>
  268. public Int16 ConvertDrToInt(object dr)
  269. {
  270. if (dr != DBNull.Value)
  271. {
  272. return Convert.ToInt16(dr.ToString());
  273. }
  274. return 0;
  275. }
  276. /// <summary>
  277. /// Parse Quote to check sql Injection
  278. /// </summary>
  279. /// <param name="val"></param>
  280. /// <returns></returns>
  281. public string ParseQuote(string val)
  282. {
  283. return "\"" + val + "\"";
  284. }
  285. public String GetSingleResult(string sql)
  286. {
  287. try
  288. {
  289. var ds = ExecuteDataset(sql);
  290. if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
  291. return "";
  292. return ds.Tables[0].Rows[0][0].ToString();
  293. }
  294. catch (Exception ex)
  295. {
  296. throw ex;
  297. }
  298. }
  299. internal T ParseDbResult<T>(string sql)
  300. {
  301. throw new NotImplementedException();
  302. }
  303. }
  304. }