TRIỂN KHAI LẬP TRÌNH

Một phần của tài liệu Báo cáo môn lập trình hướng đối tượng xây dựng chương trinh quản lí sinh viên (Trang 23 - 30)

1.Lớp Khoa 1.1 Thêm

public void Them(clsDuLieu db, Khoa objKhoa) {

// nếu Thông tin đã đầy đủ thì thực hiện lưu

String stringSQL = "INSERT INTO " + TABLE_KHOA + " (";

stringSQL += " makhoa,tenkhoa";

stringSQL += ") VALUES (";

stringSQL += "'" + objKhoa.makhoa + "',";

stringSQL += "'" + objKhoa.tenkhoa + "'";

stringSQL += ")";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

1.2 Sửa

public void Sua(clsDuLieu db, Khoa objKhoa) {

String stringSQL = "UPDATE " + TABLE_KHOA + " SET ";

stringSQL += "tenkhoa='" + objKhoa.tenkhoa + "'";

stringSQL += " WHERE (makhoa='" + objKhoa.makhoa + "');";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

1.3 Xóa

public void Xoa(clsDuLieu db, Khoa objKhoa) {

DialogResult TraLoi;

TraLoi = MessageBox.Show("Bạn có muốn xóa khoa này " + objKhoa.makhoa + "

không ?", "Cảnh báo xóa dữ liệu ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (TraLoi == DialogResult.Yes) {

String stringSQL = "DELETE * FROM " + TABLE_KHOA + " WHERE ";

stringSQL += "makhoa='" + objKhoa.makhoa + "';";

db.RunSQL(stringSQL);

MessageBox.Show("Đã xóa");

} }

2 Lớp Lớp học 2.1 Thêm

public void Them(clsDuLieu db, Lop objLop)

{

// nếu Thông tin đã đầy đủ thì thực hiện lưu

String stringSQL = "INSERT INTO " + TABLE_LOP + " (";

stringSQL += " malop,tenlop,khoa,namnhaphoc,siso,makhoa";

stringSQL += ") VALUES (";

stringSQL += "'" + objLop.malop + "',";

stringSQL += "'" + objLop.tenlop + "',";

stringSQL += "'" + objLop.khoa + "',";

stringSQL += "'" + objLop.namnhap + "',";

stringSQL += "'" + objLop.siso + "',";

stringSQL += "'" + objLop.makhoa + "'";

stringSQL += ")";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

2.2 Sửa

public void Sua(clsDuLieu db, Lop objLop) {

String stringSQL = "UPDATE " + TABLE_LOP + " SET ";

stringSQL += "tenlop='" + objLop.tenlop + "',";

stringSQL += "khoa='" + objLop.khoa + "',";

stringSQL += "namnhaphoc='" + objLop.namnhap + "',";

stringSQL += "siso='" + objLop.siso + "',";

stringSQL += "makhoa='" + objLop.makhoa + "'";

stringSQL += " WHERE (malop='" + objLop.malop + "');";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

2.3 Xóa

public void Xoa(clsDuLieu db, Lop objLop) {

DialogResult TraLoi;

TraLoi = MessageBox.Show("Bạn có muốn xóa lớp này " + objLop.malop + " không ?", "Cảnh báo xóa dữ liệu ",

MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (TraLoi == DialogResult.Yes) {

String stringSQL = "DELETE * FROM " + TABLE_LOP + " WHERE ";

stringSQL += "malop='" + objLop.malop + "';";

db.RunSQL(stringSQL);

MessageBox.Show("Đã xóa");

} }

3 Lớp Sinh viên 3.1 Thêm

public void Them(clsDuLieu db, SinhVien sv) {

// nếu Thông tin đã đầy đủ thì thực hiện lưu String stringSQL = "INSERT INTO SINHVIEN (";

stringSQL += "

masv,ho,ten,ngaysinh,gioitinh,noisinh,hokhau,dantoc,sodienthoai,malop";

stringSQL += ") VALUES (";

stringSQL += "'" + sv.ma + "',";

stringSQL += "'" + sv.ho + "',";

stringSQL += "'" + sv.ten + "',";

stringSQL += "'" + sv.ngaysinh + "',";

if (sv.gioitinh == "True") stringSQL += "'1',";

else stringSQL += "'0',";

stringSQL += "'" + sv.noisinh + "',";

stringSQL += "'" + sv.hokhau + "',";

stringSQL += "'" + sv.dantoc + "',";

stringSQL += "'" + sv.sodienthoai + "',";

stringSQL += "'" + sv.malop + "'";

stringSQL += ")";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

3.2 Sửa

public void Sua(clsDuLieu db, SinhVien objsv) {

String stringSQL = "UPDATE SINHVIEN SET ";

stringSQL += "ho='" + objsv.ho + "',";

stringSQL += "ten='" + objsv.ten + "',";

stringSQL += "ngaysinh='" + objsv.ngaysinh + "',";

if (objsv.gioitinh == "True") stringSQL += "gioitinh='1',";

else stringSQL += "gioitinh='0',";

stringSQL += "noisinh='" + objsv.noisinh + "',";

stringSQL += "hokhau='" + objsv.hokhau + "',";

stringSQL += "dantoc='" + objsv.dantoc + "',";

stringSQL += "sodienthoai='" + objsv.sodienthoai + "',";

stringSQL += "malop='" + objsv.malop + "'";

stringSQL += " WHERE (masv='" + objsv.ma + "');";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

3.3 Xóa

public void Xoa(clsDuLieu db, SinhVien objsv) {

DialogResult TraLoi;

TraLoi = MessageBox.Show("Bạn có muốn xóa sinh viên "

+ objsv.ma + " không ?", "Cảnh báo xóa dữ liệu ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (TraLoi == DialogResult.Yes) {

String stringSQL = "DELETE * FROM SINHVIEN WHERE ";

stringSQL += "masv='" + objsv.ma + "';";

db.RunSQL(stringSQL);

MessageBox.Show("Đã xóa");

} }

4 Lớp Điểm thi 4.1 Thêm

public void Them(clsDuLieu db, DiemThi objDiemThi) {

// nếu Thông tin đã đầy đủ thì thực hiện lưu

String stringSQL = "INSERT INTO " + TABLE_DIEM + " (";

stringSQL += " mamonhoc,masv,diemthilan1,diemthilan2";

stringSQL += ") VALUES (";

stringSQL += "'" + objDiemThi.mamonhoc + "',";

stringSQL += "'" + objDiemThi.masv + "',";

stringSQL += "'" + objDiemThi.diemthilan1 + "',";

stringSQL += "'" + objDiemThi.diemthilan2 + "'";

stringSQL += ")";

if ( db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu !");

else MessageBox.Show("Lỗi ! Chưa lưu");

}

4.2 Sửa

public void Sua(clsDuLieu db, DiemThi objDiemThi) {

String stringSQL = "UPDATE " + TABLE_DIEM + " SET ";

stringSQL += "diemthilan1='" + objDiemThi.diemthilan1 + "',";

stringSQL += "diemthilan2='" + objDiemThi.diemthilan2 + "'";

stringSQL += " WHERE (mamonhoc='" + objDiemThi.mamonhoc + "') and (masv='" + objDiemThi.masv+ "');";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

4.3 Xóa

public void Xoa(clsDuLieu db, DiemThi objDiemThi) {

DialogResult TraLoi;

TraLoi = MessageBox.Show("Bạn có muốn xóa điểm sinh viên "

+objDiemThi.masv +"không ?", "Cảnh báo xóa dữ liệu ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (TraLoi == DialogResult.Yes) {

String stringSQL = "DELETE * FROM " + TABLE_DIEM + " WHERE ";

stringSQL += "mamonhoc='" + objDiemThi.mamonhoc + "' and masv='"

+objDiemThi.masv+"';";

db.RunSQL(stringSQL);

MessageBox.Show("Đã xóa");

} }

5 Môn học 5.1 Thêm

public void Them(clsDuLieu db, MonHoc objMH) {

// nếu Thông tin đã đầy đủ thì thực hiện lưu

String stringSQL = "INSERT INTO " + TABLE_MON_HOC + " (";

stringSQL += " mamonhoc,tenmonhoc,sodv";

stringSQL += ") VALUES (";

stringSQL += "'" + objMH.mamonhoc + "',";

stringSQL += "'" + objMH.tenmonhoc + "',";

stringSQL += "'" + objMH.sodonvihocphan + "'";

stringSQL += ")";

if (db.RunSQL(stringSQL))

MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

5.2 Sửa

public void Sua(clsDuLieu db, MonHoc objMH) {

String stringSQL = "UPDATE " + TABLE_MON_HOC + " SET ";

stringSQL += "tenmonhoc='" + objMH.tenmonhoc + "',";

stringSQL += "sodv='" + objMH.sodonvihocphan + "'";

stringSQL += " WHERE (mamonhoc='" + objMH.mamonhoc + "');";

if (db.RunSQL(stringSQL)) MessageBox.Show("Đã lưu");

else MessageBox.Show("Chưa lưu");

}

5.3 Xóa

public void Xoa(clsDuLieu db, MonHoc objMH) {

DialogResult TraLoi;

TraLoi = MessageBox.Show("Bạn có muốn xóa môn học " + objMH.mamonhoc + " không ?", "Cảnh báo xóa dữ liệu ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (TraLoi == DialogResult.Yes) {

String stringSQL = "DELETE * FROM " + TABLE_MON_HOC + " WHERE ";

stringSQL += "mamonhoc='" + objMH.mamonhoc + "';";

db.RunSQL(stringSQL);

MessageBox.Show("Đã xóa");

} }

6 Lớp clsDuLieu 6.1 Kết nối

public void KetNoi() {

try {

if (Cnn.State == ConnectionState.Closed || Cnn.State ==

ConnectionState.Broken) {

Cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +

Application.StartupPath + DUONGDANKETNOI;

Cnn.Open();

} } catch {

// Hiện ra hộp thoại thông báo

MessageBox.Show("Kết nối không thành công ");

} }

6.2 Lấy GetDataSet

public DataSet GetDataSet(string TenBang) {

DataSet ds = new DataSet();

string strSQL = "Select * From " + TenBang;

da = new OleDbDataAdapter(strSQL, Cnn);

CmdBD = new OleDbCommandBuilder(da);

da.Fill(ds, TenBang);

return ds;

}

6.3 Lấy DataSet gồm nhiều trường

// // lấy dataset gồm nhiều trường //

public DataSet GetDataSet(string cactruong, string TenBang) {

DataSet ds = new DataSet();

string strSQL = "Select " + cactruong + " From " + TenBang;

da = new OleDbDataAdapter(strSQL, Cnn);

CmdBD = new OleDbCommandBuilder(da);

da.Fill(ds, TenBang);

return ds;

}

6.4 Lấy DataSet theolệnh SQL

public DataSet GetDataSetSQLCommand(string strSQL) {

DataSet ds = new DataSet();

da = new OleDbDataAdapter(strSQL, Cnn);

CmdBD = new OleDbCommandBuilder(da);

da.Fill(ds);

return ds;

}

6.5 Lấy DataSet theo điều kiện

//

// lấy dataset có điều kiện kèm theo //

public DataSet GetDataSetDieuKien(string TenBang, string DieuKien) {

DataSet ds = new DataSet();

string strSQL = " Select * From " + TenBang + " WHERE " + DieuKien;

da = new OleDbDataAdapter(strSQL, Cnn);

da.Fill(ds, TenBang);

return ds;

}

6.6 Kiểm tra mã có trùng không

//

// Phương thức kiểm tra mã số có trùng không //

public bool KiemTraMa(string TenBang, string DieuKien) {

DataTable table = new DataTable();

string strSQL = " Select * From " + TenBang;

if ( DieuKien != "") {

strSQL += " Where "+ DieuKien;

}

da = new OleDbDataAdapter(strSQL, Cnn);

da.Fill(table);

int dem = 0;

foreach (DataRow row in table.Rows) {

dem++;

}

if (dem >= 1) return true;

else return false;

}

6.7 Chạy một lệnh SQL

public bool RunSQL(string strSQL) {

try {

OleDbCommand Cmd = new OleDbCommand(strSQL, Cnn);

Cmd.ExecuteNonQuery();

return true;

}

catch (Exception ex) {

return false;

} }

Một phần của tài liệu Báo cáo môn lập trình hướng đối tượng xây dựng chương trinh quản lí sinh viên (Trang 23 - 30)

Tải bản đầy đủ (DOCX)

(35 trang)
w