Exemplo n.º 1
0
        private void GroupsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            dgvGroup[0, 0].Selected = true;

            // ----------------------------------------------------------------------------------------------
            //
            var db = new LINQDataContext();

            for (int rowCounter = 0; rowCounter < dgvGroup.RowCount - 1; rowCounter++)
            {
                try
                {
                    //
                    // search dgvGroup.rows in db.Groups
                    //
                    int ID_No = 0;
                    int.TryParse(dgvGroup[0, rowCounter].Value.ToString(), out ID_No);
                    // Define the query expression.
                    var query = (from groups in db.Groups
                                 where groups.ID == ID_No
                                 select groups).SingleOrDefault();

                    if (query != null) // EDIT
                    {
                        try
                        {
                            int year = 0;
                            query.Semester_Entry_Year = (int.TryParse(dgvGroup[1, rowCounter].Value.ToString(), out year)) ? year : 0;

                            query.Semester_Entry_FS = (dgvGroup[2, rowCounter].Value != null) ?
                                                      ((dgvGroup[2, rowCounter].Value.ToString() == colSemesterEntry.Items[0].ToString()) ? true : false) : true;
                            //
                            // find branch id in string ComboBox Select Academic field
                            //
                            int    Branch_ID = 0;
                            string bIND      = (dgvGroup[3, rowCounter].Value != null) ? dgvGroup[3, rowCounter].Value.ToString() : "";
                            if (bIND == "")
                            {
                                throw new Exception("Pleas Select a Branch for the Group by Group's Id (" + ID_No + ")");
                            }
                            string branch_id = "";
                            foreach (char ch in bIND.ToCharArray())
                            {
                                if (char.IsDigit(ch))
                                {
                                    branch_id += ch.ToString();
                                }
                                else
                                {
                                    Branch_ID = int.Parse(branch_id);
                                    break;
                                }
                            }

                            query.Branch_Selection = Branch_ID;
                            //
                            int size = 0;
                            query.Size_No = (dgvGroup[4, rowCounter].Value != null) ? (int.TryParse(dgvGroup[4, rowCounter].Value.ToString(), out size)) ? size : 0 : 0;

                            db.SubmitChanges();
                        }
                        catch { }
                    }
                    else
                    {
                        //
                        // find branch id in string ComboBox Select Academic field
                        //
                        int    Branch_ID = 0;
                        string bIND      = (dgvGroup[3, rowCounter].Value != null) ? dgvGroup[3, rowCounter].Value.ToString() : "";
                        if (bIND == "")
                        {
                            throw new Exception("Pleas Select a Branch for the Group by Group's Id (" + ID_No + ")");
                        }
                        string branch_id = "";
                        foreach (char ch in bIND.ToCharArray())
                        {
                            if (char.IsDigit(ch))
                            {
                                branch_id += ch.ToString();
                            }
                            else
                            {
                                Branch_ID = int.Parse(branch_id);
                                break;
                            }
                        }
                        //
                        int size = 0;
                        int year = 0;
                        db.GroupsSave(ID_No, (int.TryParse(dgvGroup[1, rowCounter].Value.ToString(), out year)) ? year : 0,
                                      (dgvGroup[2, rowCounter].Value != null) ?
                                      ((dgvGroup[2, rowCounter].Value.ToString() == colSemesterEntry.Items[0].ToString()) ? true : false) : true,
                                      Branch_ID,
                                      (dgvGroup[4, rowCounter].Value != null) ? (int.TryParse(dgvGroup[4, rowCounter].Value.ToString(), out size)) ? size : 0 : 0);
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }
        private void pbtnGroupImport_Click(object sender, EventArgs e)
        {
            // If using Professional version, put your serial key below. Otherwise, keep following
            // line commented out as Free version doesn't have SetLicense method.
            // SpreadsheetInfo.SetLicense("YOUR-SERIAL-KEY-HERE");
            var db = new LINQDataContext(); // for save to OLEDB  db.Group.mdf database file's
            ExcelFile ef = new ExcelFile();
            OpenFileDialog openFromExcel = new OpenFileDialog();
            openFromExcel.Filter = @"Excel files|*.xls";
            if (openFromExcel.ShowDialog() == DialogResult.OK)
            {
                ExcelWorksheet sheet;
                try
                {
                    ef.LoadXls(openFromExcel.FileName);

                    sheet = ef.Worksheets["Group"];
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); return; }
                bool FirstRow = true;
                foreach (ExcelRow row in sheet.Rows)
                {
                    if (!FirstRow)
                    {
                        try
                        {
                            int size = 0;
                            int CourseID = 0;
                            db.GroupsSave(Convert.ToInt32(row.Cells[0].Value.ToString()),
                                (row.Cells[1].Value != null) ? int.Parse(row.Cells[1].Value.ToString()) : 0,
                                (row.Cells[2].Value != null) ? bool.Parse(row.Cells[2].Value.ToString()) : true,
                                (int.TryParse(row.Cells[3].Value.ToString(), out CourseID)) ? CourseID : 0,
                                (int.TryParse(row.Cells[6].Value.ToString(), out size)) ? size : 0);
                        }
                        catch (Exception ex) { MessageBox.Show(ex.Message); return; }
                    }
                    else FirstRow = false;
                }
            }
        }
        private void ImportMDF()
        {
            try
            {
                Set_Cursor_Value("WaitCursor");
                var originDB = new LINQDataContext();
                var newDB = new LINQDataContext(FileName);
                Set_prbMain_Value(0);

                #region 1.  Professor DATA
                //
                // Delete old Professor data and Fill it by newDB data
                //
                var aryProfessor = newDB.Professors.ToArray();
                if (aryProfessor.Length > 0)
                {
                    string query = @"DELETE FROM dbo.New_GroupsPerClass " + @"DELETE FROM dbo.Classroom_time "
                        + @"DELETE FROM dbo.Priority_Professor " + @"DELETE FROM dbo.Professor";
                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryProfessor)
                    {
                        originDB.ProfessorSave(row.ID, row.Name_Professor, row.Branch, row.Email, row.EducationDegree, row.Schedule);
                    }
                }
                #endregion
                Set_prbMain_Value(10);

                #region 2.  Branch DATA
                //
                // Delete old Branch data and Fill it by newDB data
                //
                var aryBranch = newDB.Branches.ToArray();
                if (aryBranch.Length > 0)
                {
                    string query = @"DELETE FROM dbo.New_GroupsPerClass " + @"DELETE FROM dbo.Group_ID_List " + @"DELETE FROM dbo.Classroom_Time "
                        + @"DELETE FROM dbo.Class " + @"DELETE FROM dbo.Course "
                        + @"DELETE FROM dbo.Groups " + @"DELETE FROM dbo.Branch";

                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryBranch)
                    {
                        originDB.BranchSave(row.ID_Branch, row.Branch_Name, row.Degree);
                    }
                }
                #endregion
                Set_prbMain_Value(20);

                #region 3.  Room_Type DATA
                //
                // Delete old Room_Type data and Fill it by newDB data
                //
                var aryRoom_Type = newDB.Room_Types.ToArray();
                if (aryRoom_Type.Length > 0)
                {
                    string query = @"DELETE FROM dbo.New_GroupsPerClass " + @"DELETE FROM dbo.Classroom_time "
                        + @"DELETE FROM dbo.Room " + @"DELETE FROM dbo.Priority_Professor "
                        + @"DELETE FROM dbo.Group_ID_List "
                        + @"DELETE FROM dbo.Class " + @"DELETE FROM dbo.Room_Type";

                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryRoom_Type)
                    {
                        originDB.Room_TypeSave(row.Type_Name);
                    }
                }
                #endregion
                Set_prbMain_Value(30);

                #region 4.  Course DATA
                //
                // Delete old Course data and Fill it by newDB data
                //
                var aryCourse = newDB.Courses.ToArray();
                if (aryCourse.Length > 0)
                {
                    string query = @"DELETE FROM dbo.New_GroupsPerClass " + @"DELETE FROM dbo.Classroom_time "
                        + @"DELETE FROM dbo.Group_ID_List " + @"DELETE FROM dbo.Priority_Professor "
                        + @"DELETE FROM dbo.Class " + @"DELETE FROM dbo.Course";
                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryCourse)
                    {
                        originDB.CourseSave(row.Course_ID, row.Branch_ID,
                                            row.TermNo, row.Name_Course,
                                            row.Type_Course, row.CourseCode,
                                            row.TheoryUnitNo, row.PracticalUnitNo,
                                            row.InRequisite_CourseID, row.PreRequisite_CourseID);
                    }
                }
                #endregion
                Set_prbMain_Value(40);

                #region 5.  Groups DATA
                //
                // Delete old Group data and Fill it by newDB data
                //
                var aryGroup = newDB.Groups.ToArray();
                if (aryGroup.Length > 0)
                {
                    string query = @"DELETE FROM dbo.New_GroupsPerClass " +
                                   @"DELETE FROM dbo.Group_ID_List " +
                                   @"DELETE FROM dbo.Groups";

                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryGroup)
                    {
                        originDB.GroupsSave(row.ID, row.Semester_Entry_Year, row.Semester_Entry_FS, row.Branch_Selection, row.Size_No);
                    }
                }
                #endregion
                Set_prbMain_Value(50);

                #region 6.  Room DATA
                //
                // Delete old Room data and Fill it by newDB data
                //
                var aryRoom = newDB.Rooms.ToArray();
                if (aryRoom.Length > 0)
                {
                    string query = @"DELETE FROM dbo.New_GroupsPerClass " + @"DELETE FROM dbo.Classroom_time " + @"DELETE FROM dbo.Room";

                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryRoom)
                    {
                        originDB.RoomSave(row.Room_ID, row.Name_Room, row.Type_Room, row.Size_No);
                    }
                }
                #endregion
                Set_prbMain_Value(60);

                #region 7.  Class DATA
                //
                // Delete old Class data and Fill it by newDB data
                //
                var aryClass = newDB.Classes.ToArray();
                if (aryClass.Length > 0)
                {
                    string query = @"DELETE FROM dbo.New_GroupsPerClass " + @"DELETE FROM dbo.Group_ID_List " + @"DELETE FROM dbo.Classroom_Time "
                        + @"DELETE FROM dbo.Priority_Professor " + @"DELETE FROM dbo.Class";

                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryClass)
                    {
                        originDB.ClassSave(row.Class_ID, row.Course_ID, row.Practical_unit, row.Theory_unit, row.RoomType);
                    }
                }
                #endregion
                Set_prbMain_Value(70);

                #region 8.  Group_ID_List DATA
                //
                // Delete old Group_ID_List data and Fill it by newDB data
                //
                var aryGroup_ID_List = newDB.Group_ID_Lists.ToArray();
                if (aryGroup_ID_List.Length > 0)
                {
                    string query = @"DELETE FROM dbo.Group_ID_List";

                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryGroup_ID_List)
                    {
                        originDB.Group_ID_ListSave(row.Group_ID, row.Class_ID);
                    }
                }
                #endregion
                Set_prbMain_Value(80);

                #region 9.  Priority_Professor DATA
                //
                // Delete old Classroom_Time data and Fill it by newDB data
                //
                var aryPriority_Professor = newDB.Priority_Professors.ToArray();
                if (aryPriority_Professor.Length > 0)
                {
                    string query = @"DELETE FROM dbo.Priority_Professor";

                    originDB.ExecuteQuery<object>(query);
                    //
                    // read and save new data
                    //
                    foreach (var row in aryPriority_Professor)
                    {
                        originDB.Priority_ProfessorSave(row.Professor_ID, row.Class_ID, row.Priority);
                    }
                }
                #endregion
                Set_prbMain_Value(90);

                #region 10.  Classroom_Time DATA
                //
                // Delete old Classroom_Time data and Fill it by newDB data
                //
                var aryClassroom_Time = newDB.Classroom_Times.ToArray();
                var aryNew_GroupsPerClass = newDB.New_GroupsPerClasses.ToArray();
                if (aryClassroom_Time.Length > 0)
                {
                    originDB.Classroom_TimeDeleteAll();
                    //
                    // read and save new data about Classroom_Time
                    //
                    foreach (var row in aryClassroom_Time)
                    {
                        originDB.Classroom_TimeSave(row.Room_ID, row.Class_ID,
                            row.Professor_ID, row.StartTime, row.Duration, row.Day_No);
                    }
                    //
                    // read and save new data about New_GroupsPerClass
                    //
                    foreach (var row in aryNew_GroupsPerClass)
                    {
                        originDB.New_GroupsPerClassSave(row.Room_ID, row.Class_ID, row.StartTime, row.Day_No, row.Group_ID);
                    }
                }
                #endregion
                Set_prbMain_Value(98);

                originDB.Connection.Close();
                originDB.Dispose();
                newDB.Connection.Close();
                newDB.Dispose();
                Set_prbMain_Value(100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "اشکال در بازیابی اطلاعات");
                thImport.Abort();
                return;
            }
            finally
            {
                Thread.CurrentThread.Join(1000);
                Set_prbMain_Value(0);
                Set_Cursor_Value("Default");
                bwExportBtnCheck.RunWorkerAsync();
                Thread.CurrentThread.Abort();
            }
        }
        private void GroupsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            dgvGroup[0, 0].Selected = true;

            // ----------------------------------------------------------------------------------------------
            //
            var db = new LINQDataContext();

            for (int rowCounter = 0; rowCounter < dgvGroup.RowCount - 1; rowCounter++)
            {
                try
                {
                    //
                    // search dgvGroup.rows in db.Groups
                    //
                    int ID_No = 0;
                    int.TryParse(dgvGroup[0, rowCounter].Value.ToString(), out ID_No);
                    // Define the query expression.
                    var query = (from groups in db.Groups
                                 where groups.ID == ID_No
                                 select groups).SingleOrDefault();

                    if (query != null) // EDIT
                    {
                        try
                        {
                            int year = 0;
                            query.Semester_Entry_Year = (int.TryParse(dgvGroup[1, rowCounter].Value.ToString(), out year)) ? year : 0;

                            query.Semester_Entry_FS = (dgvGroup[2, rowCounter].Value != null) ?
                                ((dgvGroup[2, rowCounter].Value.ToString() == colSemesterEntry.Items[0].ToString()) ? true : false) : true;
                            //
                            // find branch id in string ComboBox Select Academic field
                            //
                            int Branch_ID = 0;
                            string bIND = (dgvGroup[3, rowCounter].Value != null) ? dgvGroup[3, rowCounter].Value.ToString() : "";
                            if (bIND == "")
                            {
                                throw new Exception("Pleas Select a Branch for the Group by Group's Id (" + ID_No + ")");
                            }
                            string branch_id = "";
                            foreach (char ch in bIND.ToCharArray())
                            {
                                if (char.IsDigit(ch))
                                {
                                    branch_id += ch.ToString();
                                }
                                else
                                {
                                    Branch_ID = int.Parse(branch_id);
                                    break;
                                }
                            }

                            query.Branch_Selection = Branch_ID;
                            //
                            int size = 0;
                            query.Size_No = (dgvGroup[4, rowCounter].Value != null) ? (int.TryParse(dgvGroup[4, rowCounter].Value.ToString(), out size)) ? size : 0 : 0;

                            db.SubmitChanges();
                        }
                        catch { }
                    }
                    else
                    {
                        //
                        // find branch id in string ComboBox Select Academic field
                        //
                        int Branch_ID = 0;
                        string bIND = (dgvGroup[3, rowCounter].Value != null) ? dgvGroup[3, rowCounter].Value.ToString() : "";
                        if (bIND == "")
                        {
                            throw new Exception("Pleas Select a Branch for the Group by Group's Id (" + ID_No + ")");
                        }
                        string branch_id = "";
                        foreach (char ch in bIND.ToCharArray())
                        {
                            if (char.IsDigit(ch))
                            {
                                branch_id += ch.ToString();
                            }
                            else
                            {
                                Branch_ID = int.Parse(branch_id);
                                break;
                            }
                        }
                        //
                        int size = 0;
                        int year = 0;
                        db.GroupsSave(ID_No, (int.TryParse(dgvGroup[1, rowCounter].Value.ToString(), out year)) ? year : 0,
                            (dgvGroup[2, rowCounter].Value != null) ?
                            ((dgvGroup[2, rowCounter].Value.ToString() == colSemesterEntry.Items[0].ToString()) ? true : false) : true,
                             Branch_ID,
                            (dgvGroup[4, rowCounter].Value != null) ? (int.TryParse(dgvGroup[4, rowCounter].Value.ToString(), out size)) ? size : 0 : 0);
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }