static void Main(string[] args) { var connectionString = "Server=localhost;Port=3306;User=u1090_113default;Password=E51wd#2i;Database=u1090113_default;"; var db = new MySqlDataBase(connectionString); var parser = new CSVParser("csvData.csv", new QuestionAndAnswerRepository(db), new WordAndAnswerRepository(db)); // parser.Parse(); var qr = new QuestionAndAnswerRepository(db); var answers = new WordAndAnswerRepository(db).FindSeveralByPhrase("что курсовая когда зачем баллы"); foreach (var answer in qr.FindSeveralByAnswers(answers)) { Console.WriteLine(answer.Question); } }
public static Employe getEmployeById(long id) { Employe employe = null; MySqlConnection connection = null; try { MySqlDataBase dataBase = new MySqlDataBase(); Console.WriteLine("Récupération d'une connexion de la BDD en cours..."); connection = dataBase.getConnection(); Console.WriteLine("Ouverture de la connexion à la BDD en cours..."); connection.Open(); Console.WriteLine("Ouverture de la connexion à la BDD réussie."); Console.WriteLine("Préparation de la requête"); MySqlCommand cmd = connection.CreateCommand(); cmd.CommandText = "SELECT * FROM employe WHERE id = @id"; cmd.Parameters.AddWithValue("@id", id); Console.WriteLine("Exécution de la requête"); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { employe = new Employe(); employe.Id = reader.GetInt32(0); employe.Nom = reader.GetString(1); employe.Prenom = reader.GetString(2); employe.PosteOccupe = reader.GetString(3); } connection.Close(); } catch (Exception ex) { Console.WriteLine("Erreur lors de l'ajout d'un employé"); Console.WriteLine(ex.Message); } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } return(employe); }
public static IList <Employe> getEmployes() { IList <Employe> listeEmployes = new List <Employe>(); MySqlConnection connection = null; try { MySqlDataBase dataBase = new MySqlDataBase(); Console.WriteLine("Récupération d'une connexion de la BDD en cours..."); connection = dataBase.getConnection(); Console.WriteLine("Ouverture de la connexion à la BDD en cours..."); connection.Open(); Console.WriteLine("Ouverture de la connexion à la BDD réussie."); Console.WriteLine("Préparation de la requête"); MySqlCommand cmd = connection.CreateCommand(); cmd.CommandText = "SELECT * FROM employe"; Console.WriteLine("Exécution de la requête"); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Employe employe = new Employe(); employe.Id = reader.GetInt32(0); employe.Nom = reader.GetString(1); employe.Prenom = reader.GetString(2); employe.PosteOccupe = reader.GetString(3); listeEmployes.Add(employe); } connection.Close(); } catch (Exception ex) { Console.WriteLine("Erreur lors de l'ajout d'un employé"); Console.WriteLine(ex.Message); } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } return(listeEmployes); }
public static MySqlDataTableScope <TEntity> CreateScope(MySqlDataBase dataBase, MySqlTable <TEntity> table) { return(new MySqlDataTableScope <TEntity>(dataBase, table)); }
private MySqlDataTableScope(MySqlDataBase dataBase, MySqlTable <TEntity> table) : base(dataBase) { Table = table; }
static void Main() { while (true) { try { Console.WriteLine("Begin to Start TcpSvr SVN..."); TestTcpSvr tts = new TestTcpSvr(); tts.InitSys(); #region 定时器事件 System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed += new ElapsedEventHandler(TimeEvent); aTimer.Interval = 5 * 1000; //配置文件中配置的秒数 aTimer.Enabled = true; #endregion string ipaddr = getLocalIP(); Console.WriteLine(ipaddr); ushort portNumber = 5632; IniAc ini = new IniAc(); ipaddr = ini.ReadValue("NET", "ip"); Console.WriteLine(ipaddr); string portStr = ini.ReadValue("NET", "port"); portNumber = System.Convert.ToUInt16(portStr); //TcpSvr svr = new TcpSvr(9050,4);//默认使用Encoding.Default编码方式 TcpSvr svr = new TcpSvr(IPAddress.Parse(ipaddr), 5632, 80, new Coder(Coder.EncodingMothord.Unicode), "D:\\MyVidio"); svr.Resovlver = new DatagramResolver("]");//0x5d //定义服务器的4个事件 //服务器满 svr.ServerFull += new NetEvent(tts.ServerFull); //新客户端连接 svr.ClientConn += new NetEvent(tts.ClientConn); //客户端关闭 svr.ClientClose += new NetEvent(tts.ClientClose); //接收到数据 svr.RecvData += new NetEvent(tts.RecvData); // svr.LogIn += new NetEvent(this.LogIn); svr.Start(); Console.WriteLine("Server is listen...{0}", svr.ServerSocket.LocalEndPoint.ToString()); //命令控制循环 #region while (true) { Console.Write(">"); string cmd = Console.ReadLine(); //退出测试程序 if (cmd.ToLower() == "exit") { break; } //停止服务器程序 if (cmd.ToLower() == "stop") { svr.Stop(); Console.WriteLine("Server is Stop."); continue; } //运行服务器程序 if (cmd.ToLower() == "start") { svr.Start(); Console.WriteLine("Server is listen...{0}", svr.ServerSocket.LocalEndPoint.ToString()); continue; } //察看服务器在线客户端数目和容量 if (cmd.ToLower() == "count") { Console.WriteLine("Current count of Client is {0}/{1}", svr.SessionCount, svr.Capacity); continue; } //发送数据到客户端格式:send [Session] [stringData] if (cmd.ToLower().IndexOf("send") != -1) { cmd = cmd.ToLower(); string[] para = cmd.Split(' '); if (para.Length == 3) { Session client = (Session)svr.SessionTable[new SessionId(int.Parse (para[1]))]; if (client != null) { svr.SendText(client, para[2]); } else { Console.WriteLine("The Session is Null"); } } else { Console.WriteLine("Error Command"); } continue; } //从服务器上踢掉一个客户端 if (cmd.ToLower().IndexOf("kick") != -1) { cmd = cmd.ToLower(); string[] para = cmd.Split(' '); if (para.Length == 2) { Session client = (Session)svr.SessionTable[new SessionId(int.Parse (para[1]))]; if (client != null) { svr.CloseSession(client); } else { Console.WriteLine("The Session is Null"); } } else { Console.WriteLine("Error command"); } continue; } //列出服务器上所有的客户端信息 if (cmd.ToLower() == "list") { int i = 0; foreach (Session Client in svr.SessionTable.Values) { if (Client != null) { i++; string info = string.Format("{0} Client:{1} connected server Session:{2}. Socket Handle:{3}", i, Client.ClientSocket.RemoteEndPoint.ToString(), Client.ID, Client.ClientSocket.Handle); Console.WriteLine(info); } else { i++; string info = string.Format("{0} null Client", i); Console.WriteLine(info); } } continue; } Console.WriteLine("Unkown Command"); }//end of while #endregion Console.WriteLine("End service"); } catch (Exception ex) { string sql = "UPDATE device SET Device_Status = 0,Sockets = ''"; IDataBase database = MySqlDataBase.getInstance(); database.Open(); database.ExcuteNonQuery(sql); database.Close(); Console.WriteLine(ex.ToString()); } } }
public static Employe addEmploye(Employe employe) { if (employe == null || employe.Adresse == null) { Console.WriteLine("l'objet employe et/ou l'adresse de l'employe ne doivent pas être nuls !"); return(null); } MySqlConnection connection = null; MySqlTransaction transaction = null; try { MySqlDataBase dataBase = new MySqlDataBase(); Console.WriteLine("Récupération d'une connexion de la BDD en cours..."); connection = dataBase.getConnection(); Console.WriteLine("Ouverture de la connexion à la BDD en cours..."); connection.Open(); Console.WriteLine("Ouverture de la connexion à la BDD réussie."); transaction = connection.BeginTransaction(); Console.WriteLine("Préparation de la requête d'ajout de l'employe"); MySqlCommand cmd = connection.CreateCommand(); cmd.Connection = connection; cmd.Transaction = transaction; cmd.CommandText = "INSERT INTO employe(nom, prenom, poste_occupe) VALUES(@nom, @prenom, @posteOccupe)"; cmd.Parameters.AddWithValue("@nom", employe.Nom); cmd.Parameters.AddWithValue("@prenom", employe.Prenom); cmd.Parameters.AddWithValue("@posteOccupe", employe.PosteOccupe); Console.WriteLine("Exécution de la requête"); int nbLignesEmploye = cmd.ExecuteNonQuery(); Console.WriteLine("Nombre de lignes ajoutées dans la table 'employe' : " + nbLignesEmploye); // récupérer l'id de l'employé qui vient d'être ajouté puis de l'injecter dans l'objet employé. employe.Id = cmd.LastInsertedId; Console.WriteLine("Préparation de la requête d'ajout de l'adresse de l'employe"); cmd.CommandText = "INSERT INTO adresse(numero, rue, ville, code_postal, employe_id)" + "VALUES(@numero, @rue, @ville, @codePostal, @employe_id)"; cmd.Parameters.AddWithValue("@numero", employe.Adresse.Numero); cmd.Parameters.AddWithValue("@rue", employe.Adresse.Rue); cmd.Parameters.AddWithValue("@ville", employe.Adresse.Ville); cmd.Parameters.AddWithValue("@codePostal", employe.Adresse.CodePostal); cmd.Parameters.AddWithValue("@employe_id", employe.Id); Console.WriteLine("Exécution de la requête"); int nbLignesAdresse = cmd.ExecuteNonQuery(); transaction.Commit(); Console.WriteLine("Nombre de lignes ajoutées dans la table 'adresse' : " + nbLignesAdresse); // récupérer l'id de l'employé qui vient d'être ajouté puis de l'injecter dans l'objet employé. employe.Adresse.Id = cmd.LastInsertedId; connection.Close(); } catch (Exception ex) { // RollBack sur l'employé ajouté try { if (transaction != null) { transaction.Rollback(); } } catch (MySqlException except) { if (transaction.Connection != null) { Console.WriteLine("Une exception du type " + except.GetType() + " a été rencontrée lors d'une tentative d'annulation de la transaction."); Console.WriteLine(except.Message); } } Console.WriteLine("Une erreur du type " + ex.GetType() + " a été rencontrée lors de l'insertion de données dans la BDD !"); Console.WriteLine(ex.Message); } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } return(employe); }