public void importStudent(Excel.Range range) { dbQLThiTracNghiemDataContext dbQLThiTracNghiemDataContext = new dbQLThiTracNghiemDataContext(); int rows = range.Rows.Count; int cols = range.Columns.Count; // dữ liệu for (int i = 3; i <= rows; i++) { Student a = new Student(); a.nameStudent = range.Cells[i, 1].Value.ToString(); a.gender = range.Cells[i, 2].Value.ToString(); Account ac = new Account(); ac.username = range.Cells[i, 3].Value.ToString(); ac.password = MyTool.getHashString(ac.username + range.Cells[i, 4].Value.ToString()); a.@class = range.Cells[i, 6].Value.ToString(); var q = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.password == ac.password && t.username == ac.username)).SingleOrDefault(); if (q != null) { MessageBox.Show( String.Format("Trong danh sách có 1 account tồn tại : Name {0} - Username {1}", a.nameStudent, q.username)); return; } else { dbQLThiTracNghiemDataContext.Accounts.InsertOnSubmit(ac); dbQLThiTracNghiemDataContext.SubmitChanges(); var temp = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.password == MyTool.getHashString(ac.password) && t.username == ac.username)).SingleOrDefault(); a.idAccount = temp.idAccount; dbQLThiTracNghiemDataContext.Students.InsertOnSubmit(a); dbQLThiTracNghiemDataContext.SubmitChanges(); } } MessageBox.Show("Import Student successfully"); }
private void btnUpdate_Click(object sender, EventArgs e) { dbQLThiTracNghiemDataContext dbQLThiTracNghiemDataContext = new dbQLThiTracNghiemDataContext(); if (cbUser.Text == "Admin") { var q = dbQLThiTracNghiemDataContext.Admins.Where((t) => (t.idAdmin == int.Parse(txtID.Text))).SingleOrDefault(); var ac = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.idAccount == q.idAccount)).SingleOrDefault(); q.nameAdmin = txtName.Text; q.gender = txtGender.Text; ac.username = txtUsername.Text; ac.password = MyTool.getHashString(txtUsername.Text); dbQLThiTracNghiemDataContext.SubmitChanges(); LoadAdmin(); } else if (cbUser.Text == "Teacher") { var q = dbQLThiTracNghiemDataContext.Teachers.Where((t) => (t.idTeacher == int.Parse(txtID.Text))).SingleOrDefault(); var ac = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.idAccount == q.idAccount)).SingleOrDefault(); q.nameTeacher = txtName.Text; q.gender = txtGender.Text; ac.username = txtUsername.Text; ac.password = MyTool.getHashString(txtUsername.Text); dbQLThiTracNghiemDataContext.SubmitChanges(); LoadTeacher(); } else if (cbUser.Text == "Student") { var q = dbQLThiTracNghiemDataContext.Students.Where((t) => (t.idStudent == int.Parse(txtID.Text))).SingleOrDefault(); var ac = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.idAccount == q.idAccount)).SingleOrDefault(); q.nameStudent = txtName.Text; q.gender = txtGender.Text; q.@class = txtClass.Text; ac.username = txtUsername.Text; ac.password = MyTool.getHashString(txtUsername.Text); dbQLThiTracNghiemDataContext.SubmitChanges(); LoadStudent(); } else if (cbUser.Text == "Disapprove") { DataGridViewRow a = null; try { a = dgvUser?.SelectedRows[0]; } catch (Exception) { MessageBox.Show("please, select a row"); return; } if (a != null) { int id = int.Parse(a.Cells["id"].Value.ToString()); int role = int.Parse(a.Cells["role"].Value.ToString()); var accountrequest = dbQLThiTracNghiemDataContext.AccountRequests.Where((i) => (i.id == id)).SingleOrDefault(); if (role == 2) { var teacher = dbQLThiTracNghiemDataContext.Teachers.Where((i) => i.idTeacher == id).SingleOrDefault(); Account account = new Account(); account.username = accountrequest.username; account.password = MyTool.getHashString(accountrequest.password); account.role = accountrequest.role; try { var temp = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.username == account.username && t.password == account.password)).SingleOrDefault(); if (temp != null) { MessageBox.Show("username and password invalid"); return; } dbQLThiTracNghiemDataContext.Accounts.InsertOnSubmit(account); dbQLThiTracNghiemDataContext.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } var temp1 = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.username == account.username && t.password == account.password)).SingleOrDefault(); if (temp1 != null) { teacher.idAccount = temp1.idAccount; dbQLThiTracNghiemDataContext.SubmitChanges(); } } else if (role == 3) { var teacher = dbQLThiTracNghiemDataContext.Students.Where((i) => i.idStudent == id).SingleOrDefault(); Account account = new Account(); account.username = accountrequest.username; account.password = MyTool.getHashString(accountrequest.password); account.role = accountrequest.role; try { var temp = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.username == account.username && t.password == account.password)).SingleOrDefault(); if (temp == null) { MessageBox.Show("username and password invalid"); return; } dbQLThiTracNghiemDataContext.Accounts.InsertOnSubmit(account); dbQLThiTracNghiemDataContext.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } var temp1 = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.username == account.username && t.password == account.password)).SingleOrDefault(); if (temp1 != null) { teacher.idAccount = temp1.idAccount; dbQLThiTracNghiemDataContext.SubmitChanges(); } } dbQLThiTracNghiemDataContext.AccountRequests.DeleteOnSubmit(accountrequest); dbQLThiTracNghiemDataContext.SubmitChanges(); LoadListDisApprove(); } } MessageBox.Show("Update successfull"); }
private void btnInsert_Click(object sender, EventArgs e) { dbQLThiTracNghiemDataContext dbQLThiTracNghiemDataContext = new dbQLThiTracNghiemDataContext(); string username = ""; string password = ""; try { username = txtUsername.Text; password = MyTool.getHashString(username + txtPassword.Text); var q = dbQLThiTracNghiemDataContext.Accounts.Where((i) => (i.username == username && i.password == password)).SingleOrDefault(); if (q != null) { MessageBox.Show("Username and password invalid"); return; } } catch (Exception ex) { MessageBox.Show(ex.Message); } if (cbUser.Text == "Admin") { try { Admin a = new Admin(); a.nameAdmin = txtName.Text; a.gender = txtGender.Text; Account ac = new Account(); ac.username = username; ac.password = password; ac.role = 1; // role admin dbQLThiTracNghiemDataContext.Accounts.InsertOnSubmit(ac); dbQLThiTracNghiemDataContext.SubmitChanges(); var q1 = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.password == password && t.username == username)).SingleOrDefault(); a.idAccount = q1.idAccount; dbQLThiTracNghiemDataContext.Admins.InsertOnSubmit(a); dbQLThiTracNghiemDataContext.SubmitChanges(); if (MessageBox.Show("insert successfull") == DialogResult.OK) { LoadAdmin(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else if (cbUser.Text == "Teacher") { try { Teacher a = new Teacher(); a.nameTeacher = txtName.Text; a.gender = txtGender.Text; Account ac = new Account(); ac.username = username; ac.password = password; ac.role = 2; // role teacher var qtest = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.password == ac.password && t.username == ac.username)).SingleOrDefault(); if (qtest != null) { MessageBox.Show("username and pasword invalid"); return; } dbQLThiTracNghiemDataContext.Accounts.InsertOnSubmit(ac); dbQLThiTracNghiemDataContext.SubmitChanges(); var q1 = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.password == password && t.username == username)).SingleOrDefault(); a.idAccount = q1.idAccount; dbQLThiTracNghiemDataContext.Teachers.InsertOnSubmit(a); dbQLThiTracNghiemDataContext.SubmitChanges(); if (MessageBox.Show("insert successfull") == DialogResult.OK) { LoadTeacher(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else if (cbUser.Text == "Student") { try { Student a = new Student(); a.nameStudent = txtName.Text; a.gender = txtGender.Text; a.@class = txtClass.Text; Account ac = new Account(); ac.username = username; ac.password = MyTool.getHashString(password); ac.role = 2; // role teacher var qtest = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.password == ac.password && t.username == ac.username)).SingleOrDefault(); if (qtest != null) { MessageBox.Show("username and pasword invalid"); return; } dbQLThiTracNghiemDataContext.Accounts.InsertOnSubmit(ac); dbQLThiTracNghiemDataContext.SubmitChanges(); var q1 = dbQLThiTracNghiemDataContext.Accounts.Where((t) => (t.password == password && t.username == username)).SingleOrDefault(); a.idAccount = q1.idAccount; dbQLThiTracNghiemDataContext.Students.InsertOnSubmit(a); dbQLThiTracNghiemDataContext.SubmitChanges(); if (MessageBox.Show("insert successfull") == DialogResult.OK) { LoadStudent(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }