private void btnthem_Click(object sender, EventArgs e)
 {
     DKCHOMUON inf = new DKCHOMUON();
     inf.madocgia = cbxmdg.SelectedItem.ToString();
     inf.madausach = (cbxmadausach.SelectedItem as ComboboxItem).Value.ToString();
     inf.ngaygiodk = dtngaygiodk.Value.Date;
     inf.tinhtrang = false;
     dataDangKyCho.themDangKyCho(inf);
     OnSign();
     Close();
     MessageBox.Show("Hoàn thành!");
 }
        public void suaDangKyCho(DKCHOMUON bef, DKCHOMUON aft)
        {
            SqlCommand sql = new SqlCommand();
            sql.CommandText = "update DKCHOMUON set madocgia = @mdg, madausach = @sach, ngaygiodk = @ngay, tinhtrang = @tt where madocgia = @mdg1 and madausach = @sach1 and ngaygiodk = @ngay1";
            sql.CommandType = CommandType.Text;
            sql.Parameters.AddWithValue("@mdg", aft.madocgia);
            sql.Parameters.AddWithValue("@sach", aft.madausach);
            sql.Parameters.AddWithValue("@ngay", aft.ngaygiodk);
            sql.Parameters.AddWithValue("@tt", aft.tinhtrang);

            sql.Parameters.AddWithValue("@mdg1", bef.madocgia);
            sql.Parameters.AddWithValue("@sach1", bef.madausach);
            sql.Parameters.AddWithValue("@ngay1", bef.ngaygiodk);

            sql.Connection = DataProvider.con;
            sql.ExecuteNonQuery();
        }
 private void btnluu_Click(object sender, EventArgs e)
 {
     try
     {
         DKCHOMUON after = new DKCHOMUON();
         after.madocgia = cbxmdg.SelectedItem.ToString();
         after.madausach = (cbxmadausach.SelectedItem as ComboboxItem).Value.ToString();
         after.ngaygiodk = dtngaygiodk.Value.Date;
         after.tinhtrang = cbdamuon.Checked;
         dataDangKyCho.suaDangKyCho(inf, after);
         MessageBox.Show("Đã lưu!");
         Close();
         OnUpdate();
     }
     catch (SqlException)
     {
         MessageBox.Show("Dữ liệu không hợp lệ!");
     }
 }
 private void btndamuon_Click(object sender, EventArgs e)
 {
     DKCHOMUON inf = new DKCHOMUON();
     inf.madocgia = dgvchomuonsach.SelectedRows[0].Cells[0].Value.ToString();
     inf.madausach = dgvchomuonsach.SelectedRows[0].Cells[1].Value.ToString();
     inf.ngaygiodk = (DateTime)dgvchomuonsach.SelectedRows[0].Cells[2].Value;
     inf = dataDangKy.timDangKyCho(inf);
     if (inf.tinhtrang == true)
     {
         MessageBox.Show("Đã đánh dấu trước đó!");
     }
     else
     {
         inf.tinhtrang = true;
         dataDangKy.suaDangKyCho(inf, inf);
         dgvchomuonsach.DataSource = dataDangKy.loadDangKyCho();
         MessageBox.Show("Đã đánh dấu!");
     }
 }
 private void btntim_Click(object sender, EventArgs e)
 {
     if (rbmdg.Checked == true)
     {
         DKCHOMUON inf = new DKCHOMUON();
         inf.madocgia = txtmdg.Text;
         dgvdkcho.DataSource = data.timDangKyCho(inf, 1);
     }
     else if (rbmadausach.Checked == true)
     {
         DKCHOMUON inf = new DKCHOMUON();
         inf.madausach = txtmds.Text;
         dgvdkcho.DataSource = data.timDangKyCho(inf, 2);
     }
     else if (rbngaydk.Checked == true)
     {
         DKCHOMUON inf = new DKCHOMUON();
         inf.ngaygiodk = dtngaygiodk.Value.Date;
         dgvdkcho.DataSource = data.timDangKyCho(inf, 3);
     }
 }
 public DataTable timDangKyCho(DKCHOMUON inf, int type)
 {
     DataTable dt = new DataTable();
     if (type == 1)//tìm theo mã độc giả
     {
         SqlDataAdapter da = new SqlDataAdapter("select* from DKCHOMUON where madocgia = @mdg", DataProvider.con);
         da.SelectCommand.Parameters.AddWithValue("@mdg", inf.madocgia);
         da.Fill(dt);
     }
     else if (type == 2)//tìm theo mã cuốn sách
     {
         SqlDataAdapter da = new SqlDataAdapter("select* from DKCHOMUON where madausach = @sach", DataProvider.con);
         da.SelectCommand.Parameters.AddWithValue("@sach", inf.madausach);
         da.Fill(dt);
     }
     else if (type == 3)//tìm theo ngày
     {
         SqlDataAdapter da = new SqlDataAdapter("select* from DKCHOMUON where ngaygiodk = @ngay", DataProvider.con);
         da.SelectCommand.Parameters.AddWithValue("@ngay", inf.ngaygiodk.Date);
         da.Fill(dt);
     }
     return dt;
 }
 public DKCHOMUON timDangKyCho(DKCHOMUON inf)
 {
     return data.DKCHOMUONs.Find(inf.madocgia, inf.madausach, inf.ngaygiodk);
 }
 public void themDangKyCho(DKCHOMUON inf)
 {
     data.DKCHOMUONs.Add(inf);
     data.SaveChanges();
 }
 public DataTable timDangKyCho(DKCHOMUON inf, int type)
 {
     return data.timDangKyCho(inf, type);
 }
 public DKCHOMUON timDangKyCho(DKCHOMUON inf)
 {
     return data.timDangKyCho(inf);
 }
 public void themDangKyCho(DKCHOMUON inf)
 {
     data.themDangKyCho(inf);
 }
 public void suaDangKyCho(DKCHOMUON bef, DKCHOMUON aft)
 {
     data.suaDangKyCho(bef, aft);
 }
        public void NhapDuLieu()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "Excel file (*.xls; *.xlsx; *.xlsm; *.xlsb)|*.xls";
            openFile.ShowDialog();

            if (openFile.FileName == String.Empty)
                return;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Worksheet sheetDocGia, sheetHocSinh, sheetNhanVien, sheetViPham;
            Excel.Worksheet sheetTuaSach, sheetDauSach, sheetCuonSach;
            Excel.Worksheet sheetMuonSach;
            Excel.Worksheet sheetDkChoMuon;
            Excel.Range rangeDocGia;

            string str;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(openFile.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            //xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            sheetDocGia = xlWorkBook.Sheets["Độc giả"] as Excel.Worksheet;
            sheetHocSinh = xlWorkBook.Sheets["Học sinh"] as Excel.Worksheet;
            sheetNhanVien = xlWorkBook.Sheets["Nhân viên"] as Excel.Worksheet;
            sheetViPham = xlWorkBook.Sheets["Vi phạm"] as Excel.Worksheet;
            sheetTuaSach = xlWorkBook.Sheets["Tựa sách"] as Excel.Worksheet;
            sheetDauSach = xlWorkBook.Sheets["Đầu sách"] as Excel.Worksheet;
            sheetCuonSach = xlWorkBook.Sheets["Cuốn sách"] as Excel.Worksheet;
            sheetMuonSach = xlWorkBook.Sheets["Mượn sách"] as Excel.Worksheet;
            sheetDkChoMuon = xlWorkBook.Sheets["ĐK chờ mượn"] as Excel.Worksheet;
            //sheetDocGia = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            /*sheetHocSinh = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
            sheetNhanVien = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
            sheetViPham = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);
            sheetTuaSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(5);
            sheetDauSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(6);
            sheetCuonSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(7);
            sheetMuonSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(8);
            sheetDkChoMuon = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(9);*/

            //range = xlWorkSheet.UsedRange;

            //for (int rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            //{
            //    for (int cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            //    {
            //        str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
            //        MessageBox.Show(str);
            //    }
            //}

            //rangeDocGia = sheetDocGia.UsedRange;

            //if ((sheetDocGia.Cells[2, 6] as Excel.Range).Value == false)
            //    MessageBox.Show("false");
            //MessageBox.Show(Convert.ToString((sheetDocGia.Cells[2,6] as Excel.Range).Value));

            DangKyCho dataDkCho = new DangKyCho();
            MuonTraSach dataMuonSach = new MuonTraSach();
            Sach dataSach = new Sach();
            DocGia dataDocGia = new DocGia();
            ViPham dataViPham = new ViPham();
            dataDkCho.xoaTatCa();
            dataMuonSach.xoaTatCa();
            dataSach.xoaTatCa();
            dataViPham.xoaTatCa();
            dataDocGia.xoaTatCa();
            //Excel.Range rangeDkChoMuon = sheetDkChoMuon.UsedRange;

            DOCGIA dg;
            for (int i = 2; i <= sheetDocGia.UsedRange.Rows.Count; i++)
            {
                dg = new DOCGIA();
                dg.madocgia = Convert.ToString((sheetDocGia.Cells[i, 1] as Excel.Range).Value);
                dg.hoten = Convert.ToString((sheetDocGia.Cells[i, 2] as Excel.Range).Value);
                dg.gioitinh = Convert.ToString((sheetDocGia.Cells[i, 3] as Excel.Range).Value);
                String date = Convert.ToString((sheetDocGia.Cells[i, 4] as Excel.Range).Value);
                if (date != null)
                    dg.ngaysinh = DateTime.Parse(date);
                String date1 = Convert.ToString((sheetDocGia.Cells[i, 5] as Excel.Range).Value);
                if (date1 != null)
                    dg.ngaylap = DateTime.Parse(date1);
                dg.tinhtrang = (bool)(sheetDocGia.Cells[i, 6] as Excel.Range).Value;
                dataDocGia.themDocGia(dg);
            }

            HOCSINH hs;
            for (int i = 2; i <= sheetHocSinh.UsedRange.Rows.Count; i++)
            {
                hs = new HOCSINH();
                hs.madocgia = Convert.ToString((sheetHocSinh.Cells[i, 1] as Excel.Range).Value);
                hs.lop = Convert.ToString((sheetHocSinh.Cells[i, 2] as Excel.Range).Value);
                dataDocGia.themHocSinh(hs);
            }

            NHANVIEN nv;
            for (int i = 2; i <= sheetNhanVien.UsedRange.Rows.Count; i++)
            {
                nv = new NHANVIEN();
                nv.madocgia = Convert.ToString((sheetNhanVien.Cells[i, 1] as Excel.Range).Value);
                dataDocGia.themNhanVien(nv);
            }

            VIPHAM vp;
            for (int i = 2; i <= sheetViPham.UsedRange.Rows.Count; i++)
            {
                vp = new VIPHAM();
                vp.madocgia = Convert.ToString((sheetViPham.Cells[i, 1] as Excel.Range).Value);
                vp.vipham1 = Convert.ToInt32((sheetViPham.Cells[i, 2] as Excel.Range).Value);
                String date = Convert.ToString((sheetViPham.Cells[i, 3] as Excel.Range).Value);
                if (date != null)
                    vp.ngayhethan = DateTime.Parse(date);
                dataViPham.themViPham(vp);
            }

            TUASACH ts;
            for (int i = 2; i <= sheetTuaSach.UsedRange.Rows.Count; i++)
            {
                ts = new TUASACH();
                ts.matuasach = Convert.ToString((sheetTuaSach.Cells[i, 1] as Excel.Range).Value);
                ts.tentuasach = Convert.ToString((sheetTuaSach.Cells[i, 2] as Excel.Range).Value);
                ts.tacgia = Convert.ToString((sheetTuaSach.Cells[i, 3] as Excel.Range).Value);
                ts.gioithieu = Convert.ToString((sheetTuaSach.Cells[i, 4] as Excel.Range).Value);
                dataSach.themTuaSach(ts);
            }

            DAUSACH ds;
            for (int i = 2; i <= sheetDauSach.UsedRange.Rows.Count; i++)
            {
                ds = new DAUSACH();
                ds.madausach = Convert.ToString((sheetDauSach.Cells[i, 1] as Excel.Range).Value);
                ds.matuasach = Convert.ToString((sheetDauSach.Cells[i, 2] as Excel.Range).Value);
                ds.ngonngu = Convert.ToString((sheetDauSach.Cells[i, 3] as Excel.Range).Value);
                ds.tinhtrang = (bool)(sheetDauSach.Cells[i, 4] as Excel.Range).Value;
                dataSach.themDauSach(ds);
            }

            CUONSACH cs;
            for (int i = 2; i <= sheetCuonSach.UsedRange.Rows.Count; i++)
            {
                cs = new CUONSACH();
                cs.macuonsach = Convert.ToString((sheetCuonSach.Cells[i, 1] as Excel.Range).Value);
                cs.madausach = Convert.ToString((sheetCuonSach.Cells[i, 2] as Excel.Range).Value);
                cs.tinhtrang = (bool)(sheetCuonSach.Cells[i, 3] as Excel.Range).Value;
                dataSach.themCuonSach(cs);
            }

            DKCHOMUON dk;
            for (int i = 2; i <= sheetDkChoMuon.UsedRange.Rows.Count; i++)
            {
                dk = new DKCHOMUON();
                dk.madocgia = Convert.ToString((sheetDkChoMuon.Cells[i, 1] as Excel.Range).Value);
                dk.madausach = Convert.ToString((sheetDkChoMuon.Cells[i, 2] as Excel.Range).Value);
                String date = Convert.ToString((sheetDkChoMuon.Cells[i, 3] as Excel.Range).Value);
                if (date != null)
                    dk.ngaygiodk = DateTime.Parse(date);
                dk.tinhtrang = (bool)(sheetDkChoMuon.Cells[i, 4] as Excel.Range).Value;
                dataDkCho.themDangKyCho(dk);
            }

            MUONSACH ms;
            for (int i = 2; i <= sheetMuonSach.UsedRange.Rows.Count; i++)
            {
                ms = new MUONSACH();
                ms.madocgia = Convert.ToString((sheetMuonSach.Cells[i, 1] as Excel.Range).Value);
                ms.macuonsach = Convert.ToString((sheetMuonSach.Cells[i, 2] as Excel.Range).Value);
                String date = Convert.ToString((sheetMuonSach.Cells[i, 3] as Excel.Range).Value);
                if (date != null)
                    ms.ngaygiomuon = DateTime.Parse(date);
                String date1 = Convert.ToString((sheetMuonSach.Cells[i, 4] as Excel.Range).Value);
                if (date1 != null)
                    ms.ngayhethan = DateTime.Parse(date1);
                String date2 = Convert.ToString((sheetMuonSach.Cells[i, 5] as Excel.Range).Value);
                if (date2 != null)
                    ms.ngaygiotra = DateTime.Parse(date2);
                dataMuonSach.themMuonTraSach(ms);
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            MessageBox.Show("Hoàn thành!");
            noticeImport();

            //releaseObject(xlWorkSheet);
            //releaseObject(xlWorkBook);
            //releaseObject(xlApp);
        }