private void btn_save_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(tb_ten.Text)) { throw new Exception(); } if (kh == null) { kh = new Khoahoc(tb_ten.Text, int.Parse(tb_max.Text), DateTime.Parse(tb_dangky.Text), DateTime.Parse(tb_hoc.Text)); sql.InsertKhoahoc(kh); } else { kh.ten = tb_ten.Text; kh.max_danhsach = int.Parse(tb_max.Text); kh.batdau_dangky = DateTime.Parse(tb_dangky.Text); kh.batdau_hoc = DateTime.Parse(tb_hoc.Text); sql.UpdateKhoahoc(kh); } gui.updatebroad("Khoahoc"); this.Dispose(); } catch (Exception ex) { l_error.Text = "Error"; Console.WriteLine(ex.Message); } }
public List<Khoahoc> SearchKhoahoc(string str) { string query; if (string.IsNullOrWhiteSpace(str)) { query = "SELECT * from khoahoc"; } else { query = "Select * from khoahoc WHERE ten like '%"+ str +"%' OR trang_thai like '%" + str + "%' OR batdau_dangki like '%" + str + "%' OR batdau_hoc like '%" + str + "%'"; } List<Khoahoc> khsearch_list = new List<Khoahoc>(); //Open connection if (this.OpenConnection() == true) { //Create Command MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { Khoahoc kh = new Khoahoc(int.Parse(dataReader["id"] + ""), dataReader["ten"] + "", idstringtosvlist(dataReader["danh_sach"] + ""), dataReader["trang_thai"] + "", int.Parse(dataReader["max_danhsach"] + ""), DateTime.Parse(dataReader["batdau_dangki"] + ""), DateTime.Parse(dataReader["batdau_hoc"] + "")); khsearch_list.Add(kh); } //close connection CloseConnection(); } return khsearch_list; }
public KhoahocGUI(GUI gui, Khoahoc kh) { this.gui = gui; this.kh = kh; InitializeComponent(); tb_ten.Text = kh.ten; tb_max.Text = kh.max_danhsach.ToString(); tb_dangky.Text = kh.batdau_dangky.ToString("MM/dd/yyyy"); tb_hoc.Text = kh.batdau_hoc.ToString("MM/dd/yyyy"); }
public void UpdateKhoahoc(Khoahoc kh) { string query = "UPDATE khoahoc SET ten = '" + kh.ten + "', trang_thai = '" + kh.trang_thai + "', max_danhsach = '" + kh.max_danhsach +"', danh_sach = '"+ svlisttoidstring(kh.danh_sach) + "', batdau_dangki = '" + kh.batdau_dangky.ToString("MM/dd/yyyy") + "', batdau_hoc = '" + kh.batdau_hoc.ToString("MM/dd/yyyy") + "' WHERE id = '" + kh.id + "';"; //open connection if (this.OpenConnection() == true) { //create command and assign the query and connection from the constructor MySqlCommand cmd = new MySqlCommand(query, connection); //Execute command cmd.ExecuteNonQuery(); //close connection CloseConnection(); update(); } }
public void InsertKhoahoc(Khoahoc kh) { string query = "INSERT INTO khoahoc (ten, trang_thai, max_danhsach, batdau_dangki, batdau_hoc) VALUES('" + kh.ten + "','" + kh.trang_thai + "','" + kh.max_danhsach + "','" + kh.batdau_dangky.ToString("MM/dd/yyyy") + "','" + kh.batdau_hoc.ToString("MM/dd/yyyy") + "')"; //open connection if (this.OpenConnection() == true) { //create command and assign the query and connection from the constructor MySqlCommand cmd = new MySqlCommand(query, connection); //Execute command cmd.ExecuteNonQuery(); //close connection CloseConnection(); update(); } }
public void DeleteKhoahoc(Khoahoc kh) { string query = "DELETE FROM khoahoc WHERE id='" + kh.id.ToString() + "'"; if (this.OpenConnection() == true) { try { MySqlCommand cmd = new MySqlCommand(query, connection); cmd.ExecuteNonQuery(); } catch (MySqlException ex) { } finally { CloseConnection(); update(); } } }