Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using MySql.Data.MySqlClient;
- using System.Threading;
- namespace JustProject
- {
- class Program
- {
- public static MySqlConnection Connection_Handle;
- public static bool Chat_Active = true;
- const int THREAD_TIME = 500; //Время задержки потока после получения сообщений(Минимальное: 500. Рекомендованное: 1000)
- const bool TRUNCATE_TABLES = true; // Очищать таблицу сообщений при подключении к каналу?
- const string SQL_HOSTING = "localhost"; // Хостинг базы
- const string SQL_DATABASE = "sqlsharp"; // Название базы
- const string SQL_USER = "root"; // Пользователь базы
- const string SQL_PASSWORD = ""; // Пароль от базы
- class newThread{
- Thread thread;
- public newThread(string name,int canal){
- thread = new Thread(CheckDataBaseThread);
- thread.Name = name;
- thread.Start(canal);
- }
- void CheckDataBaseThread(object Canal_Stream){
- string sqlquery = "SELECT * FROM `Messages` WHERE `Canal` != '"+(int)Canal_Stream+"' ORDER BY `Id` ASC LIMIT 1";
- while(Chat_Active == true){
- MySqlCommand command = new MySqlCommand();
- command.Connection = Connection_Handle;
- command.CommandText = sqlquery;
- try{
- using(MySqlDataReader reader = command.ExecuteReader()){
- reader.Read();
- if(reader.HasRows){
- int row_id = -1;
- int canal = -1;
- string row_message = "";
- if(row_message.Equals("стоп"))break;
- try{
- int message_row = reader.GetOrdinal("Message");
- row_message = reader.GetString(message_row);
- int id_row = reader.GetOrdinal("Id");
- row_id = reader.GetInt32(id_row);
- int canal_row = reader.GetOrdinal("Canal");
- canal = reader.GetInt32(canal_row);
- }catch(Exception e){
- Console.WriteLine("Ошибка при получении: "+e.Message);
- }
- Console.WriteLine("<<--- Собеседник(Канал {1}): {0} --->>",row_message,canal);
- reader.Close();
- reader.Dispose();
- DeleteFromMysql(Connection_Handle,row_id);
- }else{
- reader.Close();
- reader.Dispose();
- }
- }
- }catch(Exception){
- break;
- }
- Thread.Sleep(1000);
- }
- }
- }
- public static void Main(){
- string connection_string = "";
- CreateConnectionString(ref connection_string,SQL_HOSTING,SQL_DATABASE,SQL_USER,SQL_PASSWORD);
- Connection_Handle = new MySqlConnection(connection_string);
- try{
- Console.WriteLine("<<---- Пытаюсь установить соединение... ---->>");
- Connection_Handle.Open();
- Console.WriteLine("<<---- Соединение установлено ---->>");
- }catch(Exception e){
- Console.WriteLine("Ошибка: {0}",e.Message);
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey(true);
- return;
- }
- int Canal = -1;
- while(Canal < 0 || Canal > 99){
- try{
- Console.WriteLine("Укажите номер канала (Не должен совпадать с другим каналом) 0-99");
- Canal = Convert.ToInt32(Console.ReadLine());
- if(Canal < 0 || Canal > 99)continue;
- break;
- }catch(Exception){
- continue;
- }
- }
- try{
- MySqlCommand truncate = new MySqlCommand("TRUNCATE TABLE `Messages`",Connection_Handle);
- truncate.ExecuteNonQuery();
- newThread thread_check = new newThread("BaseCheck",Canal);
- ExecuteProgramm(Connection_Handle,Canal);
- Connection_Handle.Close();
- }catch(Exception e){
- Console.WriteLine("Ошибка при выполнении: "+e.Message);
- }
- Console.WriteLine("Разговор окончен");
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey(true);
- return;
- }
- private static void CreateConnectionString(ref string connection_string,string host,string database,string user,string pass,int port = 3306){
- connection_string = "Server="+ host +";Database="+database+";port="+port+";User Id="+user+";password="+pass;
- }
- private static void DeleteFromMysql(MySqlConnection handle,int id){
- string query = "DELETE FROM `Messages` WHERE `Id` = '"+id+"'";
- try{
- MySqlCommand deletecommand = new MySqlCommand(query,handle);
- deletecommand.ExecuteNonQuery();
- deletecommand.Dispose();
- }catch(Exception e){
- Console.WriteLine("Ошибка при выполнении запроса: "+e.Message);
- }
- }
- private static void ExecuteProgramm(MySqlConnection handle,int Canal_Stream){
- string text_insert = "";
- Console.WriteLine("<<------------------------------>>");
- Console.WriteLine("Для остановки введите !стоп");
- Console.WriteLine("Введите первое сообщение");
- while(!text_insert.Equals("!стоп") && Chat_Active == true){
- text_insert = Console.ReadLine();
- if(text_insert.Equals("!стоп")){
- Chat_Active = false;
- break;
- }
- string query = "INSERT INTO `Messages` (`Message`,`Canal`) VALUES ('"+ text_insert +"','"+ Canal_Stream +"')";
- try{
- MySqlCommand command = new MySqlCommand(query,handle);
- command.ExecuteNonQuery();
- command.Dispose();
- Console.WriteLine("<<---- Сообщение отправлено ---->>");
- }catch(Exception e){
- Console.WriteLine("Ошибка при добавлении: "+e.Message);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement