Exemplo n.º 1
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var LopDangChon = BDSLopHoc.Current as Classroom;

            if (LopDangChon != null)
            {
                var rs = MessageBox.Show("Bạn có thực sự muốn xóa không? ",
                                         "chú ý",
                                         MessageBoxButtons.OKCancel,
                                         MessageBoxIcon.Warning);
                if (rs == DialogResult.OK)
                {
                    //Xóa lớp đang chọn
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    var lop             = db.Classrooms.Where(t => t.ID == LopDangChon.ID).FirstOrDefault();
                    if (lop != null)
                    {
                        var HocSinh = db.Students.Where(t => t.IDClassroom == lop.ID);
                        foreach (Student student in HocSinh)
                        {
                            db.Students.Remove(student);
                        }

                        db.Classrooms.Remove(lop);
                        db.SaveChanges();
                        DanhSachLopHoc();
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void btnDongY_Click(object sender, EventArgs e)
        {
            var tenLop   = txtTenLop.Text;
            var phongHoc = txtPhongHoc.Text;

            //thêm lớp ở đây
            if (this.lopHoc == null)
            {
                var lop = new Classroom
                {
                    ID   = Guid.NewGuid().ToString(),
                    Name = tenLop,
                    Room = phongHoc
                };
                var db = new AppQLSVDBContext();
                db.Classrooms.Add(lop);
                db.SaveChanges();
                //nếu thêm thành công thì trả về ok
                DialogResult = DialogResult.OK;
            }
            else
            {
                var db  = new AppQLSVDBContext();
                var lop = db.Classrooms.Where(t => t.ID == lopHoc.ID).FirstOrDefault();
                lop.Name = tenLop;
                lop.Room = phongHoc;
                db.SaveChanges();
                DialogResult = DialogResult.OK;
            }
        }
Exemplo n.º 3
0
        private void btnXoaSV_Click(object sender, EventArgs e)
        {
            var SVDangChon = bdsSinhVien.Current as Student;

            if (SVDangChon != null)
            {
                var rs = MessageBox.Show(
                    "Bạn có thật sự muốn xóa không?",
                    "Chú ý",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Warning

                    );
                if (rs == DialogResult.OK)
                {
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    var SV = db.Students.Where(t => t.ID == SVDangChon.ID).FirstOrDefault();
                    if (SV != null)
                    {
                        db.Students.Remove(SV);
                        db.SaveChanges();
                        LoadDanhSachSinhVien();
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            var db          = new AppQLSVDBContext();
            var LopDuocChon = db.Classrooms.Where(t => t.Name == cbbLopHoc.Text).FirstOrDefault();

            for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
            {
                if (dataGridView1.Rows[rows].Cells[0].Value != null)
                {
                    var Student = new Student
                    {
                        ID           = dataGridView1.Rows[rows].Cells[0].Value.ToString(),
                        FirstName    = dataGridView1.Rows[rows].Cells[1].Value.ToString(),
                        LastName     = dataGridView1.Rows[rows].Cells[2].Value.ToString(),
                        DateOfBirth  = DateTime.Parse(dataGridView1.Rows[rows].Cells[3].Value.ToString()),
                        PlaceOfBirth = dataGridView1.Rows[rows].Cells[4].Value.ToString(),
                        Gender       = int.Parse(dataGridView1.Rows[rows].Cells[5].Value.ToString()),
                        IDClassroom  = LopDuocChon.ID
                    };
                    db.Students.Add(Student);
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch
            {
                MessageBox.Show("Dữ liệu không đúng hoặc trùng mã sinh viên");
            }
        }
Exemplo n.º 5
0
        private void btnLuu_Click_1(object sender, EventArgs e)
        {
            var      dbc       = new AppQLSVDBContext();
            var      maSv      = txtMaSV.Text;
            var      hoDem     = txtHoDem.Text;
            var      ten       = txtTen.Text;
            DateTime ngaySinh  = DateTime.Parse(txtNgaySInh.Text);
            var      noiSinh   = textBox1.Text;
            int      gioiTinh  = (int)txtGioiTinh.Value;
            var      tenLopHoc = cboLop.SelectedValue.ToString();

            /*if(lopHoc == null)
             * {
             *  MessageBox.Show("Không có lớp học này!");
             * } else
             * { */
            var maSvDuplicate = dbc.Students.Where(t => t.ID == maSv).FirstOrDefault();

            if (sinhVien == null)
            {
                if (maSvDuplicate == null)
                {
                    var student = new Student
                    {
                        ID           = maSv,
                        FirstName    = hoDem,
                        LastName     = ten,
                        DateOfBirth  = ngaySinh,
                        PlaceOfBirth = noiSinh,
                        Gender       = gioiTinh,
                        IDClassroom  = tenLopHoc
                    };
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    db.Students.Add(student);
                    db.SaveChanges();
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Mã sinh viên đã bị trùng!");
                }
                // Thêm mới sinh viên
            }
            else
            {
                var db = new AppQLSVDBContext();
                var sv = db.Students.Where(c => c.ID == sinhVien.ID).FirstOrDefault(); // Truy xuất sinh viên đang chọn dưới DB
                sv.ID           = maSv;                                                // chỉnh sửa id ở DB thành txtMaSV
                sv.FirstName    = hoDem;
                sv.LastName     = ten;
                sv.DateOfBirth  = ngaySinh;
                sv.PlaceOfBirth = noiSinh;
                sv.Gender       = gioiTinh;
                sv.IDClassroom  = tenLopHoc;
                // Lưu dữ liệu xuống DB
                db.SaveChanges();
                DialogResult = DialogResult.OK;
            }
            /*}*/
        }
Exemplo n.º 6
0
        private void btnXoaLop_Click(object sender, EventArgs e)
        {
            var lopDangChon = bdsLopHoc.Current as Classroom;

            if (lopDangChon != null)
            {
                var rs = MessageBox.Show(
                    "Bạn có thực sự muốn xóa không?",
                    "Chú ý",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Warning);
                if (rs == DialogResult.OK)
                {
                    //Xóa lớp đang chọn
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    var lop             = db.Classrooms.Where(t => t.ID == lopDangChon.ID).FirstOrDefault();
                    if (lop != null)
                    {
                        db.Classrooms.Remove(lop);
                        db.SaveChanges();
                        LoadDanhSachLopHoc();
                    }
                }
            }
        }
Exemplo n.º 7
0
        void LoadDanhSachLop()
        {
            var db = new AppQLSVDBContext();

            var ls = db.Classrooms.ToList();

            gridLopHoc.DataSource = ls;
        }
Exemplo n.º 8
0
        private void DanhSachLopHoc()
        {
            AppQLSVDBContext db = new AppQLSVDBContext();
            var ls = db.Classrooms.OrderBy(e => e.Name).ToList();

            BDSLopHoc.DataSource  = ls;
            gridLopHoc.DataSource = BDSLopHoc;
        }
Exemplo n.º 9
0
        private void DanhSachSinhVien()
        {
            AppQLSVDBContext db = new AppQLSVDBContext();
            var ls = db.Students.ToList();

            bdsSinhVien.DataSource  = ls;
            GridSinhVien.DataSource = bdsSinhVien;
        }
Exemplo n.º 10
0
        void LoadDanhSachSinhVien()
        {
            //Coi như bài tập tại lớp
            AppQLSVDBContext db = new AppQLSVDBContext();
            var ls = db.Students.Where(t => t.IDClassroom == MaLop).ToList();

            bdsSinhVien.DataSource  = ls;
            gridSinhVien.DataSource = bdsSinhVien;
        }
Exemplo n.º 11
0
        private void bdsSinhVien_CurrentChanged(object sender, EventArgs e)
        {
            var sinhVienDangChon = bdsSinhVien.Current as Student;

            if (sinhVienDangChon != null)
            {
                var db = new AppQLSVDBContext();
            }
        }
Exemplo n.º 12
0
        void LoadDanhSachLopHoc()
        {
            //Coi như bài tập tại lớp
            AppQLSVDBContext db = new AppQLSVDBContext();
            var ls = db.Classrooms.OrderBy(e => e.Name).ToList();

            bdsLopHoc.DataSource  = ls;
            gridLopHoc.DataSource = bdsLopHoc;
        }
Exemplo n.º 13
0
        void LoadNameClassroom()
        {
            AppQLSVDBContext db = new AppQLSVDBContext();

            listTenLop           = db.Classrooms.OrderBy(t => t.Name).ToList();
            bdsTenLop.DataSource = listTenLop;
            cboLop.DataSource    = bdsTenLop;
            cboLop.DisplayMember = "Name";
        }
Exemplo n.º 14
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (student == null)
     {
         var    db           = new AppQLSVDBContext();
         var    LopDuocChon  = db.Classrooms.Where(t => t.Name == CBBoxLopHoc.Text).FirstOrDefault();
         String ID           = txtMSV.Text;
         String FirstName    = txtHo.Text;
         String LastName     = txtTen.Text;
         String DateOfBirth  = DTPNgaySinh.Value.ToString("yyyy-MM-dd");
         String PlaceOfBirth = txtNoiSinh.Text;
         int    Gender       = RDBNam.Checked ? 1 : (RDBNu.Checked ? 0 : -1);
         String IDClassroom  = LopDuocChon.ID;
         if (ID.Equals(""))
         {
             MessageBox.Show("Bạn phải nhập mã sinh viên");
         }
         else
         {
             var Student = new Student
             {
                 ID           = ID,
                 FirstName    = FirstName,
                 LastName     = LastName,
                 DateOfBirth  = DateTime.Parse(DateOfBirth),
                 PlaceOfBirth = PlaceOfBirth,
                 Gender       = Gender,
                 IDClassroom  = IDClassroom
             };
             db.Students.Add(Student);
             db.SaveChanges();
             DialogResult = DialogResult.OK;
         }
     }
     else
     {
         var    db           = new AppQLSVDBContext();
         var    LopDuocChon  = db.Classrooms.Where(t => t.Name == CBBoxLopHoc.Text).FirstOrDefault();
         var    sv           = db.Students.Where(t => t.ID == student.ID).FirstOrDefault();
         String ID           = txtMSV.Text;
         String FirstName    = txtHo.Text;
         String LastName     = txtTen.Text;
         String DateOfBirth  = DTPNgaySinh.Value.ToString("yyyy-MM-dd");
         String PlaceOfBirth = txtNoiSinh.Text;
         int    Gender       = RDBNam.Checked ? 1 : (RDBNu.Checked ? 0 : -1);
         String IDClassroom  = LopDuocChon.ID;
         sv.FirstName    = FirstName;
         sv.LastName     = LastName;
         sv.DateOfBirth  = DateTime.Parse(DateOfBirth);
         sv.PlaceOfBirth = PlaceOfBirth;
         sv.Gender       = Gender;
         sv.IDClassroom  = IDClassroom;
         db.SaveChanges();
         DialogResult = DialogResult.OK;
     }
 }
Exemplo n.º 15
0
        void loadcombobox()
        {
            var db    = new AppQLSVDBContext();
            var cbbox = db.Classrooms.OrderBy(e => e.ID).ToList();

            for (int i = 0; i < cbbox.Count; i++)
            {
                cbMalop.Items.Add(cbbox.ElementAt(i).ID);
            }
        }
Exemplo n.º 16
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            {
                var firstname  = txtfirstnam.Text;
                var lastname   = txtlastname.Text;
                var dob        = txtbirthday.Value;
                var pod        = txtpod.Text;
                var gender     = 0;
                var idclassrom = cbMalop.Text;
                if (rdbMale.Checked == true)
                {
                    gender = 0;
                }
                else if (rdbFeMale.Checked == true)
                {
                    gender = 1;
                }
                else if (rdbOthersexual.Checked == true)
                {
                    gender = 3;
                }

                if (sinhvien == null)
                {
                    System.Random rd = new Random();
                    var           sv = new Student
                    {
                        ID           = "T" + rd.Next(1, 9999), //Guid.NewGuid().ToString(),
                        FirstName    = firstname,
                        LastName     = lastname,
                        DateOfBirth  = dob,
                        PlaceOfBirth = pod,
                        Gender       = gender,
                        IDClassroom  = idclassrom
                    };
                    var db = new AppQLSVDBContext();
                    db.Students.Add(sv);
                    db.SaveChanges();
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    var db    = new AppQLSVDBContext();
                    var newsv = db.Students.Where(t => t.ID == sinhvien.ID).FirstOrDefault();
                    newsv.FirstName    = firstname;
                    newsv.LastName     = lastname;
                    newsv.DateOfBirth  = dob;
                    newsv.PlaceOfBirth = pod;
                    newsv.Gender       = gender;
                    newsv.IDClassroom  = idclassrom;
                    db.SaveChanges();
                    DialogResult = DialogResult.OK;
                }
            }
        }
Exemplo n.º 17
0
        private void bdsLopHoc_CurrentChanged(object sender, EventArgs e)
        {
            var lopDangChon = bdsLopHoc.Current as Classroom;

            if (lopDangChon != null)
            {
                var db   = new AppQLSVDBContext();
                var dsSV = db.Students.Where(t => t.IDClassroom == lopDangChon.ID).ToList();
                bdsSinhVien.DataSource   = dsSV;
                dataGridView2.DataSource = bdsSinhVien;
            }
        }
Exemplo n.º 18
0
        private void BDSLopHoc_CurrentChanged(object sender, EventArgs e)
        {
            var LopDangChon = BDSLopHoc.Current as Classroom;

            if (LopDangChon != null)
            {
                var db   = new AppQLSVDBContext();
                var dsSv = db.Students.Where(h => h.IDClassroom == LopDangChon.ID).ToList();
                bdsSinhVien.DataSource  = dsSv;
                GridSinhVien.DataSource = bdsSinhVien;
            }
        }
Exemplo n.º 19
0
        public frmEcxel(String ID)
        {
            InitializeComponent();
            var db = new AppQLSVDBContext();
            var ls = db.Classrooms.Select(e => e.Name).ToList();

            cbbLopHoc.DataSource = ls;
            this.id = ID;
            if (this.id != null)
            {
                var ClassDefault = db.Classrooms.Where(e => e.ID == this.id).FirstOrDefault();
                cbbLopHoc.SelectedItem = ClassDefault.Name;
            }
        }
Exemplo n.º 20
0
        void LoadDanhSachLopHoc()
        {
            AppQLSVDBContext db = new AppQLSVDBContext();
            var ls = db.Classrooms.Select(e => new ClassRoomViewModel
            {
                ID           = e.ID,
                Name         = e.Name,
                Room         = e.Room,
                TotalStudent = e.Students.Count,
                TotalFemale  = e.Students.Where(t => t.Gender == 1).Count(),
                TotalMale    = e.Students.Where(t => t.Gender == 0).Count()
            }).OrderBy(e => e.Name).ToList();

            bdsLopHoc.DataSource  = ls;
            gridlophoc.DataSource = bdsLopHoc;
        }
Exemplo n.º 21
0
        private void LoadLopHoc()
        {
            var db = new AppQLSVDBContext();
            var ls = db.Classrooms.Select(e => e.Name).ToList();

            CBBoxLopHoc.DataSource = ls;
            if (student != null)
            {
                var ClassDefault = db.Classrooms.Where(e => e.ID == student.IDClassroom).FirstOrDefault();
                CBBoxLopHoc.SelectedItem = ClassDefault.Name;
            }
            if (this.IDClass != null)
            {
                var ClassDefault = db.Classrooms.Where(e => e.ID == this.IDClass).FirstOrDefault();
                CBBoxLopHoc.SelectedItem = ClassDefault.Name;
            }
        }
Exemplo n.º 22
0
        private void btnDongY_Click(object sender, EventArgs e)
        {
            var TenLop   = txtTenLop.Text;
            var PhongHoc = txtPhongHoc.Text;

            if (this.lopHoc == null)
            {
                //thêm lớp

                var lop = new Classroom
                {
                    ID   = Guid.NewGuid().ToString(),
                    Name = TenLop,
                    Room = PhongHoc
                };
                var db = new AppQLSVDBContext();
                try
                {
                    db.Classrooms.Add(lop);
                    db.SaveChanges();
                    //nếu thêm thành công thì
                    DialogResult = DialogResult.OK;
                }
                catch
                {
                    MessageBox.Show("Bạn phải nhập đầy đủ nội dung");
                }
            }
            else
            {
                var db  = new AppQLSVDBContext();
                var lop = db.Classrooms.Where(v => v.ID == lopHoc.ID).FirstOrDefault();
                try
                {
                    lop.Name = TenLop;
                    lop.Room = PhongHoc;
                    db.SaveChanges();
                    DialogResult = DialogResult.OK;
                }
                catch
                {
                    MessageBox.Show("Bạn chưa nhập đủ thông tin");
                }
            }
        }
Exemplo n.º 23
0
        private void btnXoaHocSinh_Click(object sender, EventArgs e)
        {
            var HocSinhDangChon = bdsSinhVien.Current as Student;

            if (HocSinhDangChon != null)
            {
                var rs = MessageBox.Show("Bạn có muốn xóa sinh viên này không ?", "Chú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (rs == DialogResult.OK)
                {
                    var db      = new AppQLSVDBContext();
                    var Student = db.Students.Where(t => t.ID == HocSinhDangChon.ID).FirstOrDefault();
                    if (Student != null)
                    {
                        db.Students.Remove(Student);
                    }
                    db.SaveChanges();
                    DanhSachLopHoc();
                }
            }
        }
Exemplo n.º 24
0
        private void btnXoaSV_Click(object sender, EventArgs e)
        {
            var sinhVienDangChon = bdsSinhVien.Current as Student;
            var ten = sinhVienDangChon.FirstName + sinhVienDangChon.LastName;

            if (sinhVienDangChon != null)
            {
                var rs = MessageBox.Show(string.Format("Bạn có thực sự muốn xóa sinh viên {0} không?", ten), "Chú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (rs == DialogResult.OK)
                {
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    var sv = db.Students.Where(a => a.ID == sinhVienDangChon.ID).FirstOrDefault();
                    if (sv != null)
                    {
                        db.Students.Remove(sv);
                        db.SaveChanges();
                        LoadDanhSachSinhVien();
                    }
                }
            }
        }
Exemplo n.º 25
0
        public frmChiTietHS(Student SinhVien)
        {
            AppQLSVDBContext db = new AppQLSVDBContext();
            var phong           = db.Classrooms.OrderBy(e => e.Name).ToList();

            InitializeComponent();
            this.Text         = "Chỉnh sửa sinh viên";
            this.SinhVien     = SinhVien;
            txtTen.Text       = this.SinhVien.FirstName;
            txtHo.Text        = this.SinhVien.LastName;
            dtpNgaySinh.Value = this.SinhVien.DateOfBirth;
            txtNoiSinh.Text   = this.SinhVien.PlaceOfBirth;
            if (this.SinhVien.Gender == 1)
            {
                rdbNam.Checked = true;
            }
            else
            {
                rdbNu.Checked = true;
            }
        }
Exemplo n.º 26
0
        private void btnxoasinhvien_Click_1(object sender, EventArgs e)
        {
            var sinhviendangchon = bdsSinhVien.Current as Student;

            if (sinhviendangchon != null)
            {
                var rs = MessageBox.Show("Bạn có thật sự muốn xóa không ?", "Chú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (rs == DialogResult.OK)
                {
                    //xóa lớp đang chọn
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    var sv = db.Students.Where(t => t.ID == sinhviendangchon.ID).FirstOrDefault();

                    if (sv != null)
                    {
                        db.Students.Remove(sv);
                        db.SaveChanges();
                        LoadDanhSachLopHoc();
                    }
                }
            }
        }
Exemplo n.º 27
0
        public frmSinhVienChiTiet(Student sinhVien)
        {
            InitializeComponent();
            LoadNameClassroom();
            this.Text     = "Chỉnh sửa nhân viên";
            this.sinhVien = sinhVien;
            txtMaSV.Text  = this.sinhVien.ID;
            txtHoDem.Text = this.sinhVien.FirstName;
            txtTen.Text   = this.sinhVien.LastName;
            //Parse DateTime to String
            DateTime dateOfBirth = (DateTime)this.sinhVien.DateOfBirth;
            String   DOBstr      = dateOfBirth.ToString();

            txtNgaySInh.Text = DOBstr;
            textBox1.Text    = this.sinhVien.PlaceOfBirth;
            txtGioiTinh.Text = this.sinhVien.Gender.ToString();

            var db     = new AppQLSVDBContext();
            var lopHoc = db.Classrooms.Where(d => d.ID == this.sinhVien.IDClassroom).FirstOrDefault();

            cboLop.Text = lopHoc.Name;
        }
Exemplo n.º 28
0
        private void btnDongY_Click(object sender, EventArgs e)
        {
            var TenSV           = txtTen.Text;
            var Ho              = txtHo.Text;
            var NgaySinh        = dtpNgaySinh.Value;
            var NoiSinh         = txtNoiSinh.Text;
            var Gender          = rdbNam.Checked == true ? 1 : 0;
            AppQLSVDBContext db = new AppQLSVDBContext();

            if (SinhVien == null)
            {
                var SV = new Student
                {
                    ID           = Guid.NewGuid().ToString(),
                    FirstName    = TenSV,
                    LastName     = Ho,
                    DateOfBirth  = NgaySinh,
                    PlaceOfBirth = NoiSinh,
                    Gender       = Gender,
                    IDClassroom  = MaLop
                };
                db.Students.Add(SV);
                db.SaveChanges();
                DialogResult = DialogResult.OK;
            }
            else
            {
                var sv = db.Students.Where(t => t.ID == SinhVien.ID).FirstOrDefault();
                sv.FirstName    = TenSV;
                sv.LastName     = Ho;
                sv.DateOfBirth  = NgaySinh;
                sv.PlaceOfBirth = NoiSinh;
                sv.Gender       = Gender;
                db.SaveChanges();
                DialogResult = DialogResult.OK;
            }
        }
Exemplo n.º 29
0
        private void btnImport_Click_1(object sender, EventArgs e)
        {
            //string namefile;
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter      = "Excel Files (.xls*)|*.xls*|All Files (*.*)|*.*";
            dlg.Multiselect = false;
            String       kn, filePath;
            DialogResult dlResult = dlg.ShowDialog();

            if (dlResult == DialogResult.OK)
            {
                filePath = dlg.FileName;
                if (filePath.Equals(""))
                {
                    var rs = MessageBox.Show(
                        "Bạn chưa chọn file",
                        "Chú ý",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning
                        );
                    return;
                }
                if (!File.Exists(filePath))
                {
                    var rs = MessageBox.Show(
                        "Không thể mở File",
                        "Chú ý",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning
                        );
                    return;
                }
                string excelcon;
                if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
                {
                    excelcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'";
                }
                if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
                {
                    kn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'";
                }
                else
                {
                    kn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
                }
                using (OleDbConnection conn = new OleDbConnection(kn))
                {
                    conn.Open();
                    OleDbDataAdapter objDA = new System.Data.OleDb.OleDbDataAdapter
                                                 ("select * from[Sheet2$]", conn);
                    DataSet excelDataSet = new DataSet();
                    objDA.Fill(excelDataSet, "Classrooms");
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    DataTable        dt = excelDataSet.Tables["Classrooms"];
                    // In kết quả ra Console
                    var i = 0;
                    foreach (DataRow row in dt.Rows)
                    {
                        if (i != 0)
                        {
                            var id    = row[0].ToString();
                            var kt_id = db.Classrooms.Where(t => t.ID == id).FirstOrDefault();
                            if (kt_id == null)
                            {
                                var room = new Classroom
                                {
                                    ID   = id = row[0].ToString(),
                                    Name = row[1].ToString(),
                                    Room = row[2].ToString()
                                };
                                db.Classrooms.Add(room);
                            }
                            else
                            {
                                var rs = MessageBox.Show(
                                    "Dữ liệu có sự trùng lặp khóa chính!",
                                    "Chú ý",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning
                                    );
                                return;
                            }
                        }
                        i++;
                    }
                    db.SaveChanges();
                    LoadDanhSachLopHoc();
                }
            }
        }
Exemplo n.º 30
0
        private void btnImportSV_Click(object sender, EventArgs e)
        {
            //string namefile;
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter      = "Excel Files (.xls*)|*.xls*|All Files (*.*)|*.*";
            dlg.Multiselect = false;
            String       kn, filePath;
            DialogResult dlResult = dlg.ShowDialog();

            if (dlResult == DialogResult.OK)
            {
                filePath = dlg.FileName;
                if (filePath.Equals(""))
                {
                    var rs = MessageBox.Show(
                        "Bạn chưa chọn file",
                        "Chú ý",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning
                        );
                    return;
                }
                if (!File.Exists(filePath))
                {
                    var rs = MessageBox.Show(
                        "Không thể mở File",
                        "Chú ý",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning
                        );
                    return;
                }
                string excelcon;
                if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
                {
                    excelcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'";
                }
                if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
                {
                    kn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'";
                }
                else
                {
                    kn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
                }
                using (OleDbConnection conn = new OleDbConnection(kn))
                {
                    conn.Open();
                    OleDbDataAdapter objDA = new System.Data.OleDb.OleDbDataAdapter
                                                 ("select * from[Sheet1$]", conn);
                    DataSet excelDataSet = new DataSet();
                    objDA.Fill(excelDataSet, "Classrooms");
                    AppQLSVDBContext db = new AppQLSVDBContext();
                    gridSinhVien.DataSource = excelDataSet;
                    // In kết quả ra Console

                    /* var i = 0;
                     * foreach (DataRow row in dt.Rows)
                     * {
                     *   if (i != 0)
                     *   {
                     *       var id = row[0].ToString();
                     *       var kt_id = db.Students.Where(t => t.ID == id).FirstOrDefault();
                     *       if (kt_id == null)
                     *       {
                     *           String day = row[3].ToString();
                     *           DateTime DoB;
                     *           if (DateTime.TryParse(day, out DoB)) {
                     *               var SV = new Student
                     *               {
                     *                   ID = id = row[0].ToString(),
                     *                   FirstName = row[1].ToString(),
                     *                   LastName = row[2].ToString(),
                     *                   DateOfBirth = DoB,
                     *                   PlaceOfBirth = row[4].ToString(),
                     *                   Gender = Convert.ToInt32(row[5].ToString()),
                     *                   IDClassroom = row[6].ToString()
                     *
                     *               };
                     *
                     *               db.Students.Add(SV);
                     *
                     *           }
                     *           else
                     *           {
                     *               var rs = MessageBox.Show(
                     *                       "Tồn tại phần tử có ngày sinh không hợp lệ!",
                     *                       "Chú ý",
                     *                       MessageBoxButtons.OK,
                     *                       MessageBoxIcon.Warning
                     *                       );
                     *               return;
                     *           }
                     *       }
                     *       else
                     *       {
                     *           var rs = MessageBox.Show(
                     *           "Dữ liệu có sự trùng lặp khóa chính!",
                     *           "Chú ý",
                     *           MessageBoxButtons.OK,
                     *           MessageBoxIcon.Warning
                     *           );
                     *           return;
                     *       }
                     *
                     *   }
                     *   i++;*/
                    /*   }
                     * db.SaveChanges();*/
                    LoadDanhSachLopHoc();
                }
            }
        }