public DataTable GetTable(string query) { My_Database db = new My_Database(); try { db.Openconnection(); SqlCommand command = new SqlCommand(query, db.GetConnection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); db.Closeconnection(); return(table); } catch (Exception) { throw; } finally { db.Closeconnection(); } }
public DataTable FindByHint(string hint) { My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand( "SELECT * FROM Add_Student WHERE CONCAT(firstname, lastname, address) LIKE '%" + hint + "%'", dataBase.GetConnection); dataBase.Openconnection(); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); // table = StudentsTableNaming(table); dataBase.Closeconnection(); return(table); } catch (Exception) { dataBase.Closeconnection(); throw; } finally { dataBase.Closeconnection(); } }
public DataTable GetAllBriefInfo() { My_Database db = new My_Database(); try { db.Openconnection(); SqlCommand command = new SqlCommand() { Connection = db.GetConnection, CommandText = "SELECT ID, firstname, lastname FROM Add_Student" }; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); db.Closeconnection(); return(table); } catch (Exception) { throw; } finally { db.Closeconnection(); } }
public DataTable GetAvgScoreByCourse() { My_Database db = new My_Database(); try { db.Openconnection(); SqlCommand command = new SqlCommand() { Connection = db.GetConnection, CommandText = "SELECT Course.label, AVG(score.student_score) As AverageGrade " + "FROM Course, Score " + "WHERE Course.Id = Score.Course_id " + "GROUP BY Course.label" }; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); db.Closeconnection(); return(table); } catch (Exception) { throw; } finally { db.Closeconnection(); } }
public DataTable AllLabel_IdOrder() { My_Database db = new My_Database(); try { db.Openconnection(); SqlCommand command = new SqlCommand() { Connection = db.GetConnection, CommandText = "select Course.Id, Course.label " + "from Course " + "order by Course.Id" }; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); db.Closeconnection(); return(table); } catch (Exception) { throw; } finally { db.Closeconnection(); } }
public bool RemoveThisCourse() { My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand( "DELETE FROM Course" + " WHERE Id = @id", dataBase.GetConnection); command.Parameters.Add("@ID", SqlDbType.Int).Value = Id; dataBase.Openconnection(); if (command.ExecuteNonQuery() == 1) { dataBase.Closeconnection(); return(true); } else { dataBase.Closeconnection(); return(false); } } catch (Exception) { throw; } finally { dataBase.Closeconnection(); } }
public DataTable GetAllScore() { My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand("Select * From Score", dataBase.GetConnection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); dataBase.Openconnection(); adapter.Fill(table); dataBase.Closeconnection(); return(table); } catch (Exception) { throw; } finally { dataBase.Closeconnection(); } }
public DataTable GetAvg_byStudent() { My_Database db = new My_Database(); try { db.Openconnection(); SqlCommand command = new SqlCommand() { Connection = db.GetConnection, CommandText = "SELECT Score.Student_id, AVG(Score.Student_score) " + "FROM Add_Student inner join Score on Add_Student.ID = Score.Student_id " + "Group by Score.Student_id " + "order by Score.Student_id " }; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); db.Closeconnection(); return(table); } catch (Exception) { throw; } finally { db.Closeconnection(); } }
public bool DeleteCourse(int id) { My_Database db = new My_Database(); try { SqlCommand command = new SqlCommand("DELETE FROM Course " + "where id=@id", db.GetConnection); command.Parameters.Add("@id", SqlDbType.Int).Value = id; db.Openconnection(); if (command.ExecuteNonQuery() == 1) { db.Closeconnection(); return(true); } else { db.Closeconnection(); return(false); } } catch (Exception) { return(false); throw; } finally { db.Closeconnection(); } }
public bool InsertSelectedCourse(string id, int course) { My_Database db = new My_Database(); try { SqlCommand command = new SqlCommand("Insert into Course (Student_id, Course_id) " + "values (@stdId, @courseId)", db.GetConnection); command.Parameters.Add("@stdId", SqlDbType.Int).Value = id; command.Parameters.Add("@courseId", SqlDbType.Int).Value = course; db.Openconnection(); if (command.ExecuteNonQuery() == 1) { db.Closeconnection(); return(true); } else { db.Closeconnection(); return(false); } } catch (Exception) { throw; } finally { db.Closeconnection(); } }
public bool AddStudent(int id, string firstname, string lastname, DateTime birthday, string gender, string phone, string address, MemoryStream picture) { SqlCommand command = new SqlCommand("INSERT INTO Add_Student (id, firstname, lastname, birthday, gender, phone, address, picture)" + "VALUES (@id, @fn, @ln, @bday, @gen, @add, @ph, @pic)", data.Getcon); command.Parameters.Add("@id", SqlDbType.Int).Value = id; command.Parameters.Add("@fn", SqlDbType.NChar).Value = firstname; command.Parameters.Add("@ln", SqlDbType.NVarChar).Value = lastname; command.Parameters.Add("@bday", SqlDbType.DateTime).Value = birthday; command.Parameters.Add("@gen", SqlDbType.VarChar).Value = gender; command.Parameters.Add("@add", SqlDbType.VarChar).Value = address; command.Parameters.Add("@ph", SqlDbType.VarChar).Value = phone; command.Parameters.Add("@pic", SqlDbType.Image).Value = picture.ToArray(); data.Openconnection(); if (command.ExecuteNonQuery() == 1) { data.Closeconnection(); return(true); } else { data.Closeconnection(); return(false); } }
public DataTable GetCourseBySemester(string semester) { semester = semester.Trim(); My_Database db = new My_Database(); try { db.Openconnection(); SqlCommand command = new SqlCommand() { Connection = db.GetConnection, CommandText = "SELECT * FROM Courses WHERE semester = @semester" }; command.Parameters.Add("@semester", SqlDbType.NVarChar).Value = semester; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); db.Closeconnection(); return(table); } catch (Exception) { throw; } finally { db.Closeconnection(); } }
public bool DeleteContacts(int uid) { try { My_Database data = new My_Database(); SqlCommand commnad = new SqlCommand("DELETE FROM Contact WHERE ID = @id", data.GetConnection); commnad.Parameters.Add("@id", SqlDbType.Int).Value = uid; data.Openconnection(); if (commnad.ExecuteNonQuery() == 1) { data.Closeconnection(); return(true); } else { data.Closeconnection(); return(false); } } catch { XtraMessageBox.Show("The format is incorrect, please check again!"); return(false); } }
public bool EditStudent(int id, string firstname, string lastname, DateTime birthday, string gender, string address, string phone, MemoryStream picture) { SqlCommand command = new SqlCommand("UPDATE Add_Student" + " SET " + "firstname = @Fname," + "lastname = @Lname," + "birthday = @Bdate," + "gender = @Gender," + "address = @Address," + "phone = @Phone," + "picture = @Picture" + " WHERE id = @ID", data.GetConnection); command.Parameters.Add("@ID", SqlDbType.Int).Value = id; command.Parameters.Add("@Fname", SqlDbType.NChar).Value = firstname; command.Parameters.Add("@Lname", SqlDbType.NVarChar).Value = lastname; command.Parameters.Add("@Ddate", SqlDbType.DateTime).Value = birthday; command.Parameters.Add("@Gender", SqlDbType.VarChar).Value = gender; command.Parameters.Add("@Phone", SqlDbType.VarChar).Value = phone; command.Parameters.Add("@Address", SqlDbType.VarChar).Value = address; command.Parameters.Add("@Picture", SqlDbType.Image).Value = picture.ToArray(); data.Openconnection(); if (command.ExecuteNonQuery() == 1) { data.Closeconnection(); return(true); } else { data.Closeconnection(); return(false); } }
public DataTable GetAll_ID_label() { My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand("(Select Group_id, Group_name From Group1)", dataBase.GetConnection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); dataBase.Openconnection(); adapter.Fill(table); return(table); } catch (Exception) { throw; } finally { dataBase.Closeconnection(); } }
public bool CheckCourseName(string courseName, int courseID = 0) { My_Database db = new My_Database(); try { SqlCommand command = new SqlCommand("SELECT * FROM Course WHERE label=@cName and Id<>@cID", db.GetConnection); command.Parameters.Add("@cName", SqlDbType.VarChar).Value = courseName; command.Parameters.Add("@cID", SqlDbType.Int).Value = courseID; SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { return(false); } else { return(true); } } catch (Exception) { return(false); throw; } finally { db.Closeconnection(); } }
public bool ShowList() { try { My_Database dataBase = new My_Database(); dataBase.Openconnection(); SqlCommand command = new SqlCommand("SELECT * FROM Add_Student", dataBase.GetConnection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "Add_Student"); dataBase.Closeconnection(); DataSource_Student.RowTemplate.Height = 40; DataTable table = dataSet.Tables["Add_Student"]; DataSource_Student.DataSource = table; DataSource_Student.ReadOnly = true; DataGridViewImageColumn imageColumn = new DataGridViewImageColumn(); imageColumn = (DataGridViewImageColumn)DataSource_Student.Columns[7]; imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch; DataSource_Student.AllowUserToAddRows = false; return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); throw; } }
private void Remove_bt_Click(object sender, EventArgs e) { try { DialogResult delete = MessageBox.Show("Are you sure delete student with id: " + Id_Student_TB.Text, "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (delete == DialogResult.Yes) { My_Database dataBase = new My_Database(); SqlCommand command = new SqlCommand( "DELETE FROM Add_Student" + " WHERE ID = @id", dataBase.Getcon); command.Parameters.Add("@ID", SqlDbType.Int).Value = Id_Student_TB.Text; dataBase.Openconnection(); if (command.ExecuteNonQuery() == 1) { MessageBox.Show("Complete", "Delete complete", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("ERROR", "Delete fails", MessageBoxButtons.OK, MessageBoxIcon.Error); } dataBase.Closeconnection(); } } catch (Exception E) { Console.WriteLine(E.Message); throw; } }
public int IsExistId(int id) { My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand( "SELECT id, firstname, lastname, birthday, gender, phone, address, picture FROM Add_Student WHERE id = @ID", dataBase.GetConnection); command.Parameters.Add("@ID", SqlDbType.NVarChar).Value = id; dataBase.Openconnection(); DataTable table = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter() { SelectCommand = command }; adapter.Fill(table); return(table.Rows.Count); } catch (Exception) { return(0); throw; } finally { dataBase.Closeconnection(); } }
private bool ShowList() { try { My_Database dataBase = new My_Database(); dataBase.Openconnection(); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Course", dataBase.Getcon); DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "Course"); dataGridView_Course.RowTemplate.Height = 80; DataTable table = dataSet.Tables["Course"]; dataGridView_Course.DataSource = table; dataGridView_Course.ReadOnly = true; dataGridView_Course.AllowUserToAddRows = false; dataBase.Closeconnection(); return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); throw; } }
public List <int> GetAllId() { List <int> result = new List <int>(); My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand("Select Id From Course", dataBase.GetConnection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); dataBase.Openconnection(); adapter.Fill(table); foreach (DataRow row in table.Rows) { result.Add(Convert.ToInt32(row[0].ToString())); } return(result); } catch (Exception) { throw; } finally { dataBase.Closeconnection(); } }
public void LoadImageManager() { Contacts contact = new Contacts(); Users user = new Users(); My_Database data = new My_Database(); SqlCommand command = new SqlCommand("SELECT Id, firstname, lastname, username, password, picture, role FROM New_User WHERE username = @name AND password = @pass", data.GetConnection); command.Parameters.Add("@name", SqlDbType.NVarChar).Value = User_tb.Text; command.Parameters.Add("@pass", SqlDbType.NVarChar).Value = Pass_tb.Text; data.Openconnection(); DataTable table = user.GetUser(command); if (table.Rows.Count > 0) { SqlDataAdapter SDA = new SqlDataAdapter(); SDA.SelectCommand = command; DataSet set = new DataSet(); SDA.Fill(set, "New_User"); data.Closeconnection(); byte[] pic1 = (byte[])table.Rows[0]["picture"]; MemoryStream picture1 = new MemoryStream(pic1); contact.Picture_User.Image = Image.FromStream(picture1); contact.Wellcome_LabelControl.Text = "Wellcome back " + table.Rows[0]["firstname"].ToString().Trim() + " with username: "******"username"].ToString(); contact.LabelControl_ID.Text = table.Rows[0]["Id"].ToString(); contact.Show(); } }
public string Role() { string role = ""; Users user = new Users(); My_Database data = new My_Database(); SqlCommand command = new SqlCommand("SELECT Id, firstname, lastname, username, password, picture, role FROM New_User WHERE username = @name AND password = @pass", data.GetConnection); command.Parameters.Add("@name", SqlDbType.NVarChar).Value = User_tb.Text; command.Parameters.Add("@pass", SqlDbType.NVarChar).Value = Pass_tb.Text; data.Openconnection(); DataTable table = user.GetUser(command); if (table.Rows.Count > 0) { SqlDataAdapter SDA = new SqlDataAdapter(); SDA.SelectCommand = command; DataSet set = new DataSet(); SDA.Fill(set, "New_User"); data.Closeconnection(); role = table.Rows[0]["role"].ToString().Trim(); return(role); } else { return(role = ""); } }
public bool UpdateCourse(int id, string label, int period, string description, int se) { My_Database db = new My_Database(); try { SqlCommand command = new SqlCommand( "UPDATE Course" + " SET " + "label = @label," + "period = @period," + "description = @description" + "se = @semester" + " WHERE Id = @ID" , db.GetConnection); command.Parameters.Add("@label", SqlDbType.NVarChar).Value = label; command.Parameters.Add("@period", SqlDbType.Int).Value = period; command.Parameters.Add("@description", SqlDbType.Text).Value = description; command.Parameters.Add("@ID", SqlDbType.Int).Value = id; command.Parameters.Add("se", SqlDbType.Int).Value = se; db.Openconnection(); if (command.ExecuteNonQuery() == 1) { db.Closeconnection(); return(true); } else { db.Closeconnection(); return(false); } } catch (Exception) { return(false); throw; } finally { db.Closeconnection(); } }
private void Find() { if (Id_Student_TB.Text.Trim() == "") { MessageBox.Show("Error", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { My_Database dataBase = new My_Database(); SqlCommand command = new SqlCommand( "SELECT id, firstname, lastname, birthday, gender, phone, address, picture FROM Add_Student WHERE id = @address or firstname = @address or lastname = @address", dataBase.Getcon); command.Parameters.Add("@address", SqlDbType.NVarChar).Value = Id_Student_TB.Text; dataBase.Openconnection(); Student student = new Student(); DataTable table = student.getStudents(command); if (table.Rows.Count > 0) { SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "Add_Student"); dataBase.Closeconnection(); //dataGridView1.RowTemplate.Height = 80; //DataTable table1 = dataSet.Tables["Add_Student"]; //dataGridView1.DataSource = table1; //dataGridView1.ReadOnly = true; //DataGridViewImageColumn imageColumn = new DataGridViewImageColumn(); //imageColumn = (DataGridViewImageColumn)dataGridView1.Columns[7]; //imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch; //dataGridView1.AllowUserToAddRows = false; Id_Student_TB.Text = table.Rows[0]["id"].ToString(); Firstname_TB.Text = table.Rows[0]["firstname"].ToString(); Lastname_TB.Text = table.Rows[0]["lastname"].ToString(); dateTime.Value = (DateTime)table.Rows[0]["birthday"]; if (table.Rows[0]["gender"].ToString() == "Female") { radioMale.Checked = true; } else if (table.Rows[0]["gender"].ToString() == "Male") { radioFemale.Checked = true; } Phone_TB.Text = table.Rows[0]["phone"].ToString(); Address_TB.Text = table.Rows[0]["address"].ToString(); byte[] pic = (byte[])table.Rows[0]["picture"]; MemoryStream picture = new MemoryStream(pic); pictureBox.Image = Image.FromStream(picture); } else { MessageBox.Show("not found", "Find Student", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
private void Edit_bt_Click(object sender, EventArgs e) { try { My_Database data = new My_Database(); SqlCommand command = new SqlCommand("UPDATE Add_Student" + " SET " + "firstname = @Fname," + "lastname = @Lname," + "birthday = @Bdate," + "gender = @Gender," + "phone = @Phone," + "address = @Adress," + "picture = @Picture" + " WHERE id = @ID", data.Getcon); command.Parameters.Add("@Fname", SqlDbType.NVarChar).Value = Firstname_TB.Text; command.Parameters.Add("@Lname", SqlDbType.NVarChar).Value = Lastname_TB.Text; command.Parameters.Add("@Bdate", SqlDbType.DateTime).Value = dateTime.Value; command.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = Phone_TB.Text; command.Parameters.Add("@Adress", SqlDbType.NVarChar).Value = Address_TB.Text; ImageConverter converter = new ImageConverter(); byte[] image = (byte[])converter.ConvertTo(pictureBox.Image, typeof(byte[])); command.Parameters.Add("@Picture", SqlDbType.Image).Value = image; command.Parameters.Add("@ID", SqlDbType.Int).Value = Id_Student_TB.Text; if (radioMale.Checked) { command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = "Male"; } else if (radioFemale.Checked) { command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = "Female"; } else { command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = "Male"; } data.Openconnection(); if (command.ExecuteNonQuery() == 1) { MessageBox.Show("Complete", "Edit completed", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("ERROR", "Edit failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } data.Closeconnection(); } catch (Exception E) { Console.WriteLine(E.Message); throw; } }
public bool UpdateThisCourse() { My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand( "UPDATE Courses" + " SET " + "label = @label," + "period = @period," + "description = @description" + "semesrer = @se" + " WHERE id = @ID" , dataBase.GetConnection); command.Parameters.Add("@label", SqlDbType.NVarChar).Value = Label; command.Parameters.Add("@period", SqlDbType.Int).Value = Period; command.Parameters.Add("@description", SqlDbType.Text).Value = Description; command.Parameters.Add("@ID", SqlDbType.Int).Value = Id; command.Parameters.Add("@se", SqlDbType.Int).Value = Semester; dataBase.Openconnection(); if (command.ExecuteNonQuery() == 1) { dataBase.Closeconnection(); return(true); } else { dataBase.Closeconnection(); return(false); } } catch (Exception) { throw; } finally { dataBase.Closeconnection(); } }
public Courses GetByID(int Id) { My_Database dataBase = new My_Database(); try { SqlCommand command = new SqlCommand("Select * From Course Where Id = @id", dataBase.GetConnection); command.Parameters.Add("@id", SqlDbType.Int).Value = Id; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); dataBase.Openconnection(); adapter.Fill(table); dataBase.Closeconnection(); Courses course = new Courses() { Id = Convert.ToInt32(table.Rows[0][0].ToString()), Label = table.Rows[0][1].ToString(), Period = Convert.ToInt32(table.Rows[0][2].ToString()), Description = table.Rows[0][3].ToString() }; this.Id = Convert.ToInt32(table.Rows[0][0].ToString()); this.Label = table.Rows[0][1].ToString(); this.Period = Convert.ToInt32(table.Rows[0][2].ToString()); this.Description = table.Rows[0][3].ToString(); return(course); } catch (Exception) { throw; } finally { dataBase.Closeconnection(); } }
public bool AddCoure(int id, string label, int period, string decription) { SqlCommand command = new SqlCommand("INSERT INTO Course (Id, lable, period, description)" + "VALUES (@ID, @Label, @Period, @Dec)", data.Getcon); command.Parameters.Add("@ID", SqlDbType.Int).Value = id; command.Parameters.Add("@Label", SqlDbType.NVarChar).Value = label; command.Parameters.Add("@Period", SqlDbType.Int).Value = period; command.Parameters.Add("@Dec", SqlDbType.NText).Value = decription; data.Openconnection(); if (command.ExecuteNonQuery() == 1) { data.Closeconnection(); return(true); } else { data.Closeconnection(); return(false); } }
public int GetFailNumber() { My_Database db = new My_Database(); try { db.Openconnection(); SqlCommand command = new SqlCommand() { Connection = db.GetConnection, CommandText = "select count(A.Student_id) " + "from (SELECT Score.Student_id, AVG(Score.student_score) as AvgScore " + "FROM Add_Student inner join Score on Add_Student.ID = Score.Student_id " + "Group by Score.Student_id " + "HAVING AVG(Score.Student_score) < 5 and AVG(Score.Student_score) >= 0) as A" }; int result = 0; try { result = (int)command.ExecuteScalar(); } catch (Exception) { return(0); } db.Closeconnection(); return(result); } catch (Exception) { throw; } finally { db.Closeconnection(); } }