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
4.8 KiB

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