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.

315 lines
8.8 KiB

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