private void CopyDataFormExcel(HttpPostedFileBase file, int?idKyThi, string tenMonHoc) { var maKyThi = db.KyThis.Find(idKyThi).MaKyThi; var dtNow = DateTime.Now; string _FileName = "Le-" + maKyThi + "-" + dtNow.Year + dtNow.Month + dtNow.Day + dtNow.Hour + dtNow.Minute + dtNow.Second + ".xls"; string _path = Path.Combine(Server.MapPath("~/Uploads/Excels"), _FileName); file.SaveAs(_path); DataTable dt = excelPro.ReadDataFromExcelFile(_path); dt.Columns.Add("IdKyThi", typeof(int)); dt.Columns.Add("Active", typeof(bool)); dt.Columns.Add("f_tenmhvn", typeof(string)); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i][4] = idKyThi; dt.Rows[i][5] = false; dt.Rows[i][6] = tenMonHoc; } SqlBulkCopy bulkcopy = new SqlBulkCopy(con); bulkcopy.DestinationTableName = "TestNhapDiems"; bulkcopy.ColumnMappings.Add(0, "SoPhach"); bulkcopy.ColumnMappings.Add(1, "Diem1"); bulkcopy.ColumnMappings.Add(2, "Diem2"); bulkcopy.ColumnMappings.Add(3, "DiemTrungBinh"); bulkcopy.ColumnMappings.Add(4, "IdKyThi"); bulkcopy.ColumnMappings.Add(5, "Active"); bulkcopy.ColumnMappings.Add(5, "f_tenmhvn"); con.Open(); bulkcopy.WriteToServer(dt); con.Close(); }
private void CopyDataFormExcel(HttpPostedFileBase file, int?idKyThi) { var maKyThi = db.KyThis.Find(idKyThi).MaKyThi; var dtNow = DateTime.Now; string _FileName = maKyThi + "-" + dtNow.Year + dtNow.Month + dtNow.Day + dtNow.Hour + dtNow.Minute + dtNow.Second + ".xls"; string _path = Path.Combine(Server.MapPath("~/Uploads/Excels"), _FileName); file.SaveAs(_path); DataTable dt = excelPro.ReadDataFromExcelFile(_path); dt.Columns.Add("KyThi", typeof(int)); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i][12] = idKyThi; } SqlBulkCopy bulkcopy = new SqlBulkCopy(con); bulkcopy.DestinationTableName = "DanhSachThis"; bulkcopy.ColumnMappings.Add(0, "f_masv"); bulkcopy.ColumnMappings.Add(1, "f_mamh"); bulkcopy.ColumnMappings.Add(2, "f_holotvn"); bulkcopy.ColumnMappings.Add(3, "f_tenvn"); bulkcopy.ColumnMappings.Add(4, "f_ngaysinh"); bulkcopy.ColumnMappings.Add(5, "sobaodanh"); bulkcopy.ColumnMappings.Add(6, "f_tenlop"); bulkcopy.ColumnMappings.Add(7, "f_tenmhvn"); bulkcopy.ColumnMappings.Add(8, "ngaythi"); bulkcopy.ColumnMappings.Add(9, "phongthi"); bulkcopy.ColumnMappings.Add(10, "tietbatdau"); bulkcopy.ColumnMappings.Add(11, "sotiet"); bulkcopy.ColumnMappings.Add(12, "IdKyThi"); con.Open(); bulkcopy.WriteToServer(dt); con.Close(); }