public static void Save(HandmadeData Data) { DKMHEntities db = new DKMHEntities(); var ClassList = ""; foreach (String cl in Data.Class) { ClassList += (ClassList.Length > 0 ? ", " : "") + "'" + cl + "'"; } String IgnoreStudents = InputHelper.IgnoreStudents.ContainsKey(Data.SubjectData) ? JsonConvert.SerializeObject(InputHelper.IgnoreStudents[Data.SubjectData]) : "[]"; IgnoreStudents = IgnoreStudents.Substring(1, IgnoreStudents.Length - 2).Replace("\"", "'"); var StudentList = db.Database.SqlQuery<StudentInfo>("select pdkmh.MaSinhVien, pdkmh.Nhom from pdkmh, sinhvien " + "where pdkmh.MaSinhVien = sinhvien.MaSinhVien and MaMonHoc = '" + Data.SubjectData + "' and Nhom in (" + ClassList + ") " + (IgnoreStudents.Length > 0 ? "and not(sinhvien.MaSinhVien in (" + IgnoreStudents + ")) " : "") + "order by (sinhvien.Ten + sinhvien.ho)").ToList(); DateTime FirstShiftTime = InputHelper.Options.StartDate.AddHours(InputHelper.Options.Times[0].Hour) .AddMinutes(InputHelper.Options.Times[0].Minute); String ShiftID = "";//InputHelper.Options.StartDate.Year + "" + InputHelper.Options.StartDate.Month + "" + InputHelper.Options.StartDate.Day; ShiftID += RoomArrangement.CalcShift(FirstShiftTime, Data.Date).ToString(); if ((from ct in db.CaThis where ct.MaCa == ShiftID select ct).Count() == 0) { var pa = new SqlParameter[] { new SqlParameter("@MaCa", SqlDbType.NVarChar) { Value = ShiftID }, new SqlParameter("@GioThi", SqlDbType.DateTime) { Value = Data.Date }, }; db.Database.ExecuteSqlCommand("INSERT INTO CaThi (MaCa, GioThi) VALUES (@MaCa, @GioThi)", pa); } Thi aRecord = new Thi(); aRecord.MaMonHoc = Data.SubjectData; aRecord.MaCa = ShiftID; var ClassGroup = Data.SubjectData; foreach (var cl in Data.Class) { ClassGroup += "_" + cl; } aRecord.Nhom = ClassGroup; String SQLQuery = ""; int StudentIndex = 0; for (int Index = 0; Index < Data.Room.Count; Index++) { aRecord.MaPhong = Data.Room[Index]; for (int i = 0; i < Data.Num[Index]; i++) { aRecord.MaSinhVien = StudentList[StudentIndex].MaSinhVien; SQLQuery += String.Format("INSERT INTO Thi (MaCa, MaMonHoc, Nhom, MaPhong, MaSinhVien) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')\r\n", aRecord.MaCa, aRecord.MaMonHoc, aRecord.Nhom, aRecord.MaPhong, aRecord.MaSinhVien ); StudentIndex++; } } db.Database.ExecuteSqlCommand(SQLQuery); }
public static void Delete(HandmadeData Data) { String ClassList = ""; foreach (String cl in Data.Class) ClassList += cl + ","; ClassList = ClassList.Remove(ClassList.Length - 1, 1); var SubjectID = Data.SubjectID; DKMHEntities db = new DKMHEntities(); var MaCaQry = (from thi in InputHelper.db.This where thi.MaMonHoc == SubjectID && thi.Nhom == ClassList select new { MaCa = thi.MaCa, MSMH = thi.MaMonHoc, Nhom = thi.Nhom }).FirstOrDefault(); try { InitRooms(SubjectID, MaCaQry.MaCa, false); db.Database.ExecuteSqlCommand("DELETE FROM Thi WHERE MaCa='" + MaCaQry.MaCa + "' and MaMonHoc='" + MaCaQry.MSMH + "' and Nhom='" + MaCaQry.Nhom + "'"); var MC = InputHelper.db.This.Where(m => m.MaCa == MaCaQry.MaCa).FirstOrDefault(); if (MC == null) db.Database.ExecuteSqlCommand("DELETE FROM CaThi WHERE MaCa='" + MaCaQry.MaCa + "'"); var DbName = Regex.Match(db.Database.Connection.ConnectionString, "initial\\scatalog=([^;]+)").Groups[1].Value; db.Database.ExecuteSqlCommand("DBCC SHRINKFILE (" + DbName + ", 1) "); db.Database.ExecuteSqlCommand("DBCC SHRINKFILE (" + DbName + "_log, 1) "); } catch { AlgorithmRunner.SaveOBJ("Status", "err Lỗi Pri trong khi xoá CSDL, hãy thử chạy lại lần nữa!"); InitRooms(SubjectID, MaCaQry.MaCa, true); AlgorithmRunner.IsBusy = false; Thread.CurrentThread.Abort(); } }
public static void Save(HandmadeData Data) { DKMHEntities db = new DKMHEntities(); var ClassList = ""; foreach (String cl in Data.Class) { ClassList += (ClassList.Length > 0 ? ", " : "") + "'" + cl + "'"; } var StudentList = db.Database.SqlQuery<StudentInfo>("select pdkmh.MaSinhVien, pdkmh.Nhom from pdkmh, sinhvien " + "where pdkmh.MaSinhVien = sinhvien.MaSinhVien and MaMonHoc = '" + Data.SubjectID + "' and Nhom in (" + ClassList + ") " ).ToList(); var DotQry = (from m in InputHelper.db.This select m.Dot).Max(); int dot = 1; if (DotQry != null) { dot = int.Parse(DotQry[0].ToString()); } DateTime FirstShiftTime = InputHelper.Options.StartDate.AddHours(InputHelper.Options.Times[0].Hour) .AddMinutes(InputHelper.Options.Times[0].Minute); String ShiftID = dot.ToString();//InputHelper.Options.StartDate.Year + "" + InputHelper.Options.StartDate.Month + "" + InputHelper.Options.StartDate.Day; ShiftID += "_" + RoomArrangement.CalcShift(FirstShiftTime, Data.Date).ToString(); if ((from ct in db.CaThis where ct.MaCa == ShiftID select ct).Count() == 0) { var pa = new SqlParameter[] { new SqlParameter("@MaCa", SqlDbType.NVarChar) { Value = ShiftID }, new SqlParameter("@GioThi", SqlDbType.DateTime) { Value = Data.Date }, }; db.Database.ExecuteSqlCommand("INSERT INTO CaThi (MaCa, GioThi) VALUES (@MaCa, @GioThi)", pa); } Thi aRecord = new Thi(); aRecord.MaMonHoc = Data.SubjectID; aRecord.MaCa = ShiftID; ClassList = ""; foreach (String cl in Data.Class) ClassList += cl + ","; ClassList = ClassList.Remove(ClassList.Length - 1, 1); aRecord.Nhom = ClassList; String SQLQuery = ""; int StudentIndex = 0; for (int Index = 0; Index < Data.Room.Count; Index++) { aRecord.MaPhong = Data.Room[Index]; for (int i = 0; i < Data.Num[Index]; i++) { aRecord.MaSinhVien = StudentList[StudentIndex].MaSinhVien; SQLQuery += String.Format("INSERT INTO Thi (MaCa, MaMonHoc, Nhom, MaPhong, MaSinhVien, Dot) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}','{5}')\r\n", aRecord.MaCa, aRecord.MaMonHoc, aRecord.Nhom, aRecord.MaPhong, aRecord.MaSinhVien, dot ); StudentIndex++; } int RoomIndex = InputHelper.Rooms.Find(m => m.Time == Data.Date).Rooms.FindIndex(m => m.RoomID == aRecord.MaPhong); InputHelper.Rooms.Find(m => m.Time == Data.Date).Rooms[RoomIndex].IsBusy = true; } db.Database.ExecuteSqlCommand(SQLQuery); AlgorithmRunner.SaveOBJ("Rooms", InputHelper.Rooms); }