The easy to use and full featured Irc Bot everyone is talking about!
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.

Database.cs 4.9KB

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