Esempio n. 1
0
        public Form1()
        {
            InitializeComponent();
            //Ket noi voi co so du lieu
            string connStr = "Server=localhost;Database=ds_sinhvien;Port=3306;User ID=root;Password=root";

            connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MessageBox.Show("Ban da ket noi thanh cong", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                MySqlCommand command = connection.CreateCommand();
                string       sqlStr  = "SELECT * FROM `sinhvien`";
                command.CommandText = sqlStr;
                try
                {
                    MySqlDataReader reader = null;
                    reader = command.ExecuteReader();
                    //doc du lieu tu co so du lieu vao
                    while (reader.Read())
                    {
                        SinhVien sv        = null;
                        int      mssv      = Convert.ToInt32(reader["MSSV"]);
                        string   hoTen     = (string)reader["HoTen"];
                        DateTime ngaySinh  = Convert.ToDateTime((string)reader["NgaySinh"]);
                        string   diaChi    = (string)reader["DiaChi"];
                        string   dienThoai = (string)reader["DienThoai"];
                        string   nienKhoa  = (string)reader["NienKhoa"];
                        string   loaiHinh  = (string)reader["LoaiHinh"];
                        if (loaiHinh == "Dai hoc")
                        {
                            sv = new SinhVienDaiHoc(diaChi, dienThoai, hoTen, mssv, ngaySinh, nienKhoa, "");
                        }
                        this.studentList.Add(sv);
                    }
                    reader.Dispose();
                    UpdateList();
                }
                catch (MySqlException e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    connection.Dispose(); //Giai phong tai nguyen
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show("Ket noi that bai", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 2
0
        // Khi an nut them
        private void btnThem_Click(object sender, EventArgs e)
        {
            SinhVien sv   = null;
            string   dc   = txtDiaChi.Text;
            string   dt   = txtDienThoai.Text;
            string   ht   = txtHoTen.Text;
            int      mssv = Convert.ToInt32(txtMSSV.Text);
            DateTime ns   = dtpNS.Value;
            string   nk   = txtNienKhoa.Text;

            //Kiem tra xem MSSV da ton tai hay chu
            foreach (SinhVien SV in studentList)
            {
                // Neu trung ma so thi cho chuyen den catch
                if (mssv == SV.MSSV)
                {
                    MessageBox.Show("Mã số sinh viên đã tồn tại", "Message");
                    return;
                }
            }
            // Neu la SV Dai hoc
            if (radDaiHoc.Checked)
            {
                string cn = cboCN.Text;
                sv = new SinhVienDaiHoc(dc, dt, ht, mssv, ns, nk, cn);
                radDaiHoc.Checked = false;
            }
            //Neu la sinh vien bang hai
            if (radBang2.Checked)
            {
                string bang1 = txtBang1.Text;
                string dv    = txtCty.Text;
                sv = new SinhVienBangHai(dc, dt, ht, mssv, ns, nk, bang1, dv);
                radBang2.Checked = false;
            }

            //Neu la sinh vien cao dang
            if (radCaoDang.Checked)
            {
                sv = new SinhVienCaoDang(dc, dt, ht, mssv, ns, nk);
                radCaoDang.Checked = false;
            }
            this.studentList.Add(sv);
            string loaiHinh = sv.LoaiHinh();

            //Them vao co so du lieu
            try
            {
                connection.Open();
                MySqlCommand command = connection.CreateCommand();
                command.CommandText = String.Format(
                    "INSERT INTO `sinhvien`(`MSSV`,`HoTen`,`NgaySinh`,`DiaChi`,`DienThoai`,`NienKhoa`,`LoaiHinh`) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')"
                    , mssv, ht, ns.ToString("d"), dc, dt, nk, loaiHinh);
                command.ExecuteNonQuery();
                MessageBox.Show("Da thuc hien thanh cong", "MySQL Insert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                connection.Close();
                connection.Dispose();
            }
            UpdateList();
            reset();
        }