The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MysqlDatabase.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using MySql.Data.MySqlClient;
  5. namespace Teknik.MailService
  6. {
  7. public class MysqlDatabase
  8. {
  9. public event EventHandler<string> MysqlErrorEvent;
  10. private bool Connected { get; set; }
  11. private MySqlConnection Connection { get; set; }
  12. private ReaderWriterLockSlim DatabaseLock { get; set; }
  13. public MysqlDatabase(string server, string database, string username, string password, int port)
  14. {
  15. Connected = false;
  16. Connection = null;
  17. DatabaseLock = new ReaderWriterLockSlim();
  18. Connect(server, database, username, password, port);
  19. }
  20. public List<Dictionary<string, object>> Query(string query, params object[] args)
  21. {
  22. List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
  23. if (Connected)
  24. {
  25. DatabaseLock.EnterWriteLock();
  26. MySqlCommand cmd = PrepareQuery(query, args);
  27. try
  28. {
  29. MySqlDataReader reader = cmd.ExecuteReader();
  30. while (reader.Read())
  31. {
  32. Dictionary<string, object> row = new Dictionary<string, object>();
  33. for (int i = 0; i < reader.FieldCount; i++)
  34. {
  35. row.Add(reader.GetName(i), reader.GetValue(i));
  36. }
  37. rows.Add(row);
  38. }
  39. reader.Close();
  40. }
  41. catch (MySqlException exception)
  42. {
  43. if (MysqlErrorEvent != null)
  44. {
  45. MysqlErrorEvent(this, exception.Message);
  46. }
  47. }
  48. catch (Exception exception)
  49. {
  50. if (MysqlErrorEvent != null)
  51. {
  52. MysqlErrorEvent(this, exception.Message);
  53. }
  54. }
  55. DatabaseLock.ExitWriteLock();
  56. }
  57. return rows;
  58. }
  59. public object ScalarQuery(string query, params object[] args)
  60. {
  61. if (Connected)
  62. {
  63. DatabaseLock.EnterWriteLock();
  64. MySqlCommand cmd = PrepareQuery(query, args);
  65. object result = null;
  66. try
  67. {
  68. result = cmd.ExecuteScalar();
  69. }
  70. catch (MySqlException exception)
  71. {
  72. if (MysqlErrorEvent != null)
  73. {
  74. MysqlErrorEvent(this, exception.Message);
  75. }
  76. }
  77. catch (Exception exception)
  78. {
  79. if (MysqlErrorEvent != null)
  80. {
  81. MysqlErrorEvent(this, exception.Message);
  82. }
  83. }
  84. DatabaseLock.ExitWriteLock();
  85. return result;
  86. }
  87. return null;
  88. }
  89. public void Execute(string query, params object[] args)
  90. {
  91. if (Connected)
  92. {
  93. DatabaseLock.EnterWriteLock();
  94. MySqlCommand cmd = PrepareQuery(query, args);
  95. try
  96. {
  97. int result = cmd.ExecuteNonQuery();
  98. }
  99. catch (MySqlException exception)
  100. {
  101. if (MysqlErrorEvent != null)
  102. {
  103. MysqlErrorEvent(this, exception.Message);
  104. }
  105. }
  106. catch (Exception exception)
  107. {
  108. if (MysqlErrorEvent != null)
  109. {
  110. MysqlErrorEvent(this, exception.Message);
  111. }
  112. }
  113. DatabaseLock.ExitWriteLock();
  114. }
  115. }
  116. private void Connect(string server, string database, string username, string password, int port)
  117. {
  118. if (Connection == null)
  119. {
  120. if (!string.IsNullOrEmpty(server) && !string.IsNullOrEmpty(database) && !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
  121. {
  122. string strCon = string.Format("Server={0}; database={1}; user={2}; password={3}; port={4}; charset=utf8; Allow Zero Datetime=true;", server, database, username, password, port);
  123. Connection = new MySqlConnection(strCon);
  124. try
  125. {
  126. Connection.Open();
  127. Connected = true;
  128. }
  129. catch (MySqlException)
  130. {
  131. Connected = false;
  132. }
  133. }
  134. }
  135. }
  136. private void Disconnect()
  137. {
  138. if (Connection != null && Connected)
  139. {
  140. Connected = false;
  141. Connection.Close();
  142. }
  143. }
  144. private MySqlCommand PrepareQuery(string query, object[] args)
  145. {
  146. if (Connected)
  147. {
  148. MySqlCommand cmd = new MySqlCommand();
  149. cmd.Connection = Connection;
  150. for (int i = 0; i < args.Length; i++)
  151. {
  152. string param = "{" + i + "}";
  153. string paramName = "@DBVar_" + i;
  154. query = query.Replace(param, paramName);
  155. cmd.Parameters.AddWithValue(paramName, args[i]);
  156. }
  157. cmd.CommandText = query;
  158. return cmd;
  159. }
  160. return null;
  161. }
  162. }
  163. }