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.

269 lines
7.1 KiB

  1. using JMETxnPushScheduler.Common;
  2. using System;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Text.RegularExpressions;
  7. namespace JMETxnPushScheduler.DAO
  8. {
  9. public class SwiftDao
  10. {
  11. SqlConnection _connection;
  12. public SwiftDao()
  13. {
  14. Init();
  15. }
  16. private void Init()
  17. {
  18. _connection = new SqlConnection(GetConnectionString());
  19. }
  20. private string GetConnectionString()
  21. {
  22. return ConfigurationManager.ConnectionStrings["Connections"].ConnectionString;
  23. }
  24. private int GetCommandTimeOut()
  25. {
  26. int cto = 60;
  27. return cto;
  28. }
  29. private void CloseConnection()
  30. {
  31. if (_connection.State == ConnectionState.Open)
  32. this._connection.Close();
  33. }
  34. public CommonResponse ParseDbResultWithSql(string sql)
  35. {
  36. return ParseDbResult(ExecuteDataTable(sql));
  37. }
  38. public CommonResponse ParseDbResult(DataTable dt)
  39. {
  40. var res = new CommonResponse();
  41. if (dt.Rows.Count > 0)
  42. {
  43. res.ResponseCode = dt.Rows[0][0].ToString();
  44. res.Msg = dt.Rows[0][1].ToString();
  45. res.Id = dt.Rows[0][2].ToString();
  46. if (dt.Columns.Count > 3)
  47. {
  48. res.Extra = dt.Rows[0][3].ToString();
  49. }
  50. if (dt.Columns.Count > 4)
  51. {
  52. res.Extra2 = dt.Rows[0][4].ToString();
  53. }
  54. }
  55. return res;
  56. }
  57. public DataSet ExecuteDataSet(string sql)
  58. {
  59. var ds = new DataSet();
  60. using (var con = new SqlConnection(GetConnectionString()))
  61. {
  62. var cmd = new SqlCommand(sql, con);
  63. cmd.CommandTimeout = GetCommandTimeOut();
  64. SqlDataAdapter da;
  65. try
  66. {
  67. da = new SqlDataAdapter(cmd);
  68. da.Fill(ds);
  69. da.Dispose();
  70. }
  71. catch (Exception ex)
  72. {
  73. throw ex;
  74. }
  75. finally
  76. {
  77. da = null;
  78. cmd.Dispose();
  79. }
  80. return ds;
  81. }
  82. }
  83. public DataTable ExecuteDataTable(string sql)
  84. {
  85. using (var ds = ExecuteDataSet(sql))
  86. {
  87. if (ds == null || ds.Tables.Count == 0)
  88. return null;
  89. return ds.Tables[0];
  90. }
  91. }
  92. public DataRow ExecuteDataRow(string sql)
  93. {
  94. using (var ds = ExecuteDataSet(sql))
  95. {
  96. if (ds == null || ds.Tables.Count == 0)
  97. return null;
  98. if (ds.Tables[0].Rows.Count == 0)
  99. return null;
  100. return ds.Tables[0].Rows[0];
  101. }
  102. }
  103. public String FilterString(string strVal)
  104. {
  105. var str = FilterQuote(strVal);
  106. if (str.ToLower() != "null")
  107. str = "'" + str + "'";
  108. return str.TrimEnd().TrimStart();
  109. }
  110. public String FilterXmlString(string strVal)
  111. {
  112. return "'" + strVal + "'";
  113. }
  114. public String FilterXmlNodeString(string strVal)
  115. {
  116. var str = FilterQuote(strVal);
  117. return str;
  118. }
  119. public String FilterQuote(string strVal)
  120. {
  121. if (string.IsNullOrEmpty(strVal))
  122. {
  123. strVal = "";
  124. }
  125. var str = strVal.Trim();
  126. if (!string.IsNullOrEmpty(str))
  127. {
  128. str = str.Replace(";", "");
  129. str = str.Replace("--", "");
  130. str = str.Replace("'", "");
  131. str = str.Replace("/*", "");
  132. str = str.Replace("*/", "");
  133. str = Regex.Replace(str, " select ", string.Empty, RegexOptions.IgnoreCase);
  134. str = Regex.Replace(str, " insert ", string.Empty, RegexOptions.IgnoreCase);
  135. str = Regex.Replace(str, " update ", string.Empty, RegexOptions.IgnoreCase);
  136. str = Regex.Replace(str, " delete ", string.Empty, RegexOptions.IgnoreCase);
  137. str = Regex.Replace(str, " drop ", string.Empty, RegexOptions.IgnoreCase);
  138. str = Regex.Replace(str, " truncate ", string.Empty, RegexOptions.IgnoreCase);
  139. str = Regex.Replace(str, " create ", string.Empty, RegexOptions.IgnoreCase);
  140. str = Regex.Replace(str, " begin ", string.Empty, RegexOptions.IgnoreCase);
  141. str = Regex.Replace(str, " end ", string.Empty, RegexOptions.IgnoreCase);
  142. str = Regex.Replace(str, " char ", string.Empty, RegexOptions.IgnoreCase);
  143. str = Regex.Replace(str, " exec ", string.Empty, RegexOptions.IgnoreCase);
  144. str = Regex.Replace(str, " xp_cmd ", string.Empty, RegexOptions.IgnoreCase);
  145. str = Regex.Replace(str, @"<.*?>", string.Empty);
  146. }
  147. else
  148. {
  149. str = "null";
  150. }
  151. return str;
  152. }
  153. public string ConvertDrToString(object dr)
  154. {
  155. if (dr != DBNull.Value)
  156. {
  157. return Convert.ToString(dr);
  158. }
  159. return string.Empty;
  160. }
  161. public Decimal ConvertDrToDecimal(object dr)
  162. {
  163. if (dr != DBNull.Value)
  164. {
  165. return Convert.ToDecimal(dr.ToString());
  166. }
  167. return 0;
  168. }
  169. public Int64 ConvertDrToInt64(object dr)
  170. {
  171. if (dr != DBNull.Value)
  172. {
  173. return Convert.ToInt64(dr.ToString());
  174. }
  175. return 0;
  176. }
  177. public DateTime ConvertDrToDate(object dr)
  178. {
  179. if (dr != DBNull.Value)
  180. {
  181. return Convert.ToDateTime(dr.ToString());
  182. }
  183. return DateTime.MinValue;
  184. }
  185. public Int16 ConvertDrToInt(object dr)
  186. {
  187. if (dr != DBNull.Value)
  188. {
  189. return Convert.ToInt16(dr.ToString());
  190. }
  191. return 0;
  192. }
  193. public string ParseQuote(string val)
  194. {
  195. return "\"" + val + "\"";
  196. }
  197. public string AutoSelect(string str1, string str2)
  198. {
  199. if (str1.ToLower() == str2.ToLower())
  200. return "selected=\"selected\"";
  201. return "";
  202. }
  203. public String GetSingleResult(string sql)
  204. {
  205. try
  206. {
  207. var ds = ExecuteDataSet(sql);
  208. if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
  209. return "";
  210. return ds.Tables[0].Rows[0][0].ToString();
  211. }
  212. catch (Exception ex)
  213. {
  214. throw ex;
  215. }
  216. finally
  217. {
  218. CloseConnection();
  219. }
  220. }
  221. }
  222. }