SQLDataBase.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DataServices;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace DataServices
  9. {
  10. public class SQLDataBase:DataBase
  11. {
  12. SqlConnection con;
  13. public SQLDataBase()
  14. {
  15. con = new SqlConnection();
  16. }
  17. public SQLDataBase(string dataFile):this()
  18. {
  19. DataFile = dataFile;
  20. }
  21. public override void ExecuteNonQuery(string sqlCommand)
  22. {
  23. using (SqlCommand com = new SqlCommand())
  24. {
  25. com.CommandText = sqlCommand;
  26. com.ExecuteNonQuery();
  27. if (!IsAlwaysOpen)
  28. {
  29. if(con != null)
  30. con.Dispose();
  31. }
  32. }
  33. }
  34. public override object Execute(string sqlCommand)
  35. {
  36. DataTable table = new DataTable();
  37. try
  38. {
  39. using (SqlDataAdapter da = new SqlDataAdapter())
  40. {
  41. using (SqlCommand com = new SqlCommand())
  42. {
  43. if (con == null)
  44. {
  45. if (ConStr != null)
  46. {
  47. Open(ConStr);
  48. }
  49. }
  50. com.Connection = con;
  51. com.CommandText = sqlCommand;
  52. if (sqlCommand.Contains("select") || sqlCommand.Contains("SELECT") || sqlCommand.Contains("Select"))
  53. {
  54. da.SelectCommand = com;
  55. da.Fill(table);
  56. }
  57. else if (sqlCommand.Contains("insert") || sqlCommand.Contains("INSERT") || sqlCommand.Contains("Insert"))
  58. {
  59. da.InsertCommand = com;
  60. da.Fill(table);
  61. }
  62. else if (sqlCommand.Contains("update") || sqlCommand.Contains("UPDATE") || sqlCommand.Contains("Update"))
  63. {
  64. da.UpdateCommand = com;
  65. da.Fill(table);
  66. }
  67. else if (sqlCommand.Contains("delete") || sqlCommand.Contains("DELETE") || sqlCommand.Contains("Delete"))
  68. {
  69. da.DeleteCommand = com;
  70. da.Fill(table);
  71. }
  72. }
  73. }
  74. }
  75. catch (System.Exception ex)
  76. {
  77. throw new Exception(ex.Message);
  78. }
  79. finally
  80. {
  81. if (!IsAlwaysOpen)
  82. {
  83. if(con != null)
  84. con.Dispose();
  85. }
  86. }
  87. return table;
  88. }
  89. public override void Open()
  90. {
  91. }
  92. public override void Close()
  93. {
  94. if (con != null)
  95. {
  96. con.Dispose();
  97. }
  98. }
  99. public override void Open(string dataSource, string dataFile, string userName = null, string passWord = "")
  100. {
  101. if (string.IsNullOrEmpty(userName))
  102. {
  103. if (dataFile.Contains(@"\"))
  104. {
  105. ConStr = @"Data Source=" + dataSource + ";AttachDbFilename=" + dataFile + ";Integrated Security=True";
  106. }
  107. else
  108. {
  109. ConStr = @"Data Source=" + dataSource + @";AttachDbFilename=|DataDirectory|\" + dataFile + ";Integrated Security=True";
  110. }
  111. }
  112. else
  113. {
  114. ConStr = @"server=" + dataSource + ";database=" + dataFile + ";Integrated Security=False;" + "user=" + userName + ";pwd=" + passWord;
  115. }
  116. if (con == null)
  117. {
  118. con = new SqlConnection();
  119. }
  120. con.ConnectionString = ConStr;
  121. try
  122. {
  123. con.Open();
  124. }
  125. catch (System.Exception ex)
  126. {
  127. throw new Exception(ex.Message);
  128. }
  129. }
  130. public override void Open(string conStr)
  131. {
  132. try
  133. {
  134. if (con == null)
  135. con = new SqlConnection();
  136. con.ConnectionString = conStr;
  137. con.Open();
  138. }
  139. catch (System.Exception ex)
  140. {
  141. throw new Exception(ex.Message);
  142. }
  143. }
  144. public override void ExecuteNonQuery(string sqlCommand, string conStr)
  145. {
  146. if (con == null)
  147. {
  148. con = new SqlConnection();
  149. }
  150. try
  151. {
  152. con.Close();
  153. con.ConnectionString = ConStr;
  154. con.Open();
  155. ExecuteNonQuery(sqlCommand);
  156. }
  157. catch (System.Exception ex)
  158. {
  159. throw new Exception(ex.Message);
  160. }
  161. }
  162. public override object Execute(string sqlCommand, string conStr)
  163. {
  164. if (con == null)
  165. {
  166. con = new SqlConnection();
  167. }
  168. try
  169. {
  170. con.Close();
  171. con.ConnectionString = ConStr;
  172. con.Open();
  173. return Execute(sqlCommand);
  174. }
  175. catch (System.Exception ex)
  176. {
  177. throw new Exception(ex.Message);
  178. }
  179. }
  180. }
  181. }