Advertisement
Guest User

Untitled

a guest
Mar 27th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. using System;
  2. using MySql.Data.MySqlClient;
  3. using System.Threading;
  4.  
  5. namespace JustProject
  6. {
  7. class Program
  8. {
  9. public static MySqlConnection Connection_Handle;
  10. public static bool Chat_Active = true;
  11.  
  12. const int THREAD_TIME = 500; //Время задержки потока после получения сообщений(Минимальное: 500. Рекомендованное: 1000)
  13. const bool TRUNCATE_TABLES = true; // Очищать таблицу сообщений при подключении к каналу?
  14.  
  15. const string SQL_HOSTING = "localhost"; // Хостинг базы
  16. const string SQL_DATABASE = "sqlsharp"; // Название базы
  17. const string SQL_USER = "root"; // Пользователь базы
  18. const string SQL_PASSWORD = ""; // Пароль от базы
  19.  
  20. class newThread{
  21. Thread thread;
  22.  
  23. public newThread(string name,int canal){
  24. thread = new Thread(CheckDataBaseThread);
  25. thread.Name = name;
  26. thread.Start(canal);
  27. }
  28. void CheckDataBaseThread(object Canal_Stream){
  29. string sqlquery = "SELECT * FROM `Messages` WHERE `Canal` != '"+(int)Canal_Stream+"' ORDER BY `Id` ASC LIMIT 1";
  30.  
  31. while(Chat_Active == true){
  32. MySqlCommand command = new MySqlCommand();
  33. command.Connection = Connection_Handle;
  34. command.CommandText = sqlquery;
  35. try{
  36. using(MySqlDataReader reader = command.ExecuteReader()){
  37. reader.Read();
  38. if(reader.HasRows){
  39. int row_id = -1;
  40. int canal = -1;
  41. string row_message = "";
  42.  
  43. if(row_message.Equals("стоп"))break;
  44. try{
  45. int message_row = reader.GetOrdinal("Message");
  46. row_message = reader.GetString(message_row);
  47. int id_row = reader.GetOrdinal("Id");
  48. row_id = reader.GetInt32(id_row);
  49. int canal_row = reader.GetOrdinal("Canal");
  50. canal = reader.GetInt32(canal_row);
  51. }catch(Exception e){
  52. Console.WriteLine("Ошибка при получении: "+e.Message);
  53. }
  54.  
  55. Console.WriteLine("<<--- Собеседник(Канал {1}): {0} --->>",row_message,canal);
  56.  
  57. reader.Close();
  58. reader.Dispose();
  59. DeleteFromMysql(Connection_Handle,row_id);
  60. }else{
  61. reader.Close();
  62. reader.Dispose();
  63. }
  64. }
  65. }catch(Exception){
  66. break;
  67. }
  68. Thread.Sleep(1000);
  69. }
  70. }
  71. }
  72. public static void Main(){
  73.  
  74. string connection_string = "";
  75. CreateConnectionString(ref connection_string,SQL_HOSTING,SQL_DATABASE,SQL_USER,SQL_PASSWORD);
  76.  
  77. Connection_Handle = new MySqlConnection(connection_string);
  78.  
  79. try{
  80. Console.WriteLine("<<---- Пытаюсь установить соединение... ---->>");
  81. Connection_Handle.Open();
  82. Console.WriteLine("<<---- Соединение установлено ---->>");
  83. }catch(Exception e){
  84. Console.WriteLine("Ошибка: {0}",e.Message);
  85. Console.WriteLine("Press any key to continue...");
  86. Console.ReadKey(true);
  87. return;
  88. }
  89.  
  90. int Canal = -1;
  91. while(Canal < 0 || Canal > 99){
  92. try{
  93. Console.WriteLine("Укажите номер канала (Не должен совпадать с другим каналом) 0-99");
  94. Canal = Convert.ToInt32(Console.ReadLine());
  95. if(Canal < 0 || Canal > 99)continue;
  96. break;
  97. }catch(Exception){
  98. continue;
  99. }
  100. }
  101. try{
  102. MySqlCommand truncate = new MySqlCommand("TRUNCATE TABLE `Messages`",Connection_Handle);
  103. truncate.ExecuteNonQuery();
  104.  
  105. newThread thread_check = new newThread("BaseCheck",Canal);
  106. ExecuteProgramm(Connection_Handle,Canal);
  107.  
  108. Connection_Handle.Close();
  109. }catch(Exception e){
  110. Console.WriteLine("Ошибка при выполнении: "+e.Message);
  111. }
  112. Console.WriteLine("Разговор окончен");
  113. Console.WriteLine("Press any key to continue...");
  114. Console.ReadKey(true);
  115. return;
  116. }
  117. private static void CreateConnectionString(ref string connection_string,string host,string database,string user,string pass,int port = 3306){
  118. connection_string = "Server="+ host +";Database="+database+";port="+port+";User Id="+user+";password="+pass;
  119. }
  120. private static void DeleteFromMysql(MySqlConnection handle,int id){
  121. string query = "DELETE FROM `Messages` WHERE `Id` = '"+id+"'";
  122. try{
  123. MySqlCommand deletecommand = new MySqlCommand(query,handle);
  124.  
  125. deletecommand.ExecuteNonQuery();
  126.  
  127. deletecommand.Dispose();
  128. }catch(Exception e){
  129. Console.WriteLine("Ошибка при выполнении запроса: "+e.Message);
  130. }
  131. }
  132. private static void ExecuteProgramm(MySqlConnection handle,int Canal_Stream){
  133. string text_insert = "";
  134.  
  135. Console.WriteLine("<<------------------------------>>");
  136. Console.WriteLine("Для остановки введите !стоп");
  137. Console.WriteLine("Введите первое сообщение");
  138. while(!text_insert.Equals("!стоп") && Chat_Active == true){
  139. text_insert = Console.ReadLine();
  140.  
  141. if(text_insert.Equals("!стоп")){
  142. Chat_Active = false;
  143. break;
  144. }
  145.  
  146. string query = "INSERT INTO `Messages` (`Message`,`Canal`) VALUES ('"+ text_insert +"','"+ Canal_Stream +"')";
  147. try{
  148. MySqlCommand command = new MySqlCommand(query,handle);
  149. command.ExecuteNonQuery();
  150. command.Dispose();
  151. Console.WriteLine("<<---- Сообщение отправлено ---->>");
  152. }catch(Exception e){
  153. Console.WriteLine("Ошибка при добавлении: "+e.Message);
  154. }
  155. }
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement