Thi CSDL

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

SQL

CREATE DATABASE QuanLyNhanVien


USE QuanLyNhanVien

CREATE TABLE ChungChi(


MaChungChi INT IDENTITY PRIMARY KEY,
TenChungChi NVARCHAR(30) NOT NULL UNIQUE,
ThoiHan INT CHECK (ThoiHan>=0)
)

CREATE TABLE NghiepVu(


MaNghiepVu INT IDENTITY PRIMARY KEY,
TenNghiepVu NVARCHAR(30) NOT NULL UNIQUE
)

CREATE TABLE NhanVien(


MaNhanVien INT IDENTITY PRIMARY KEY,
HoTen NVARCHAR(30) NOT NULL,
DienThoai VARCHAR(10) NOT NULL
)

CREATE TABLE NghiepVuNhanVien


(
MaNhanVien INT REFERENCES NhanVien(MaNhanVien),
MaNghiepVu INT REFERENCES NghiepVu(MaNghiepVu),
MaChungChi INT REFERENCES ChungChi(MaChungChi),
PRIMARY KEY(MaNhanVien,MaNghiepVu, MaChungChi),
NgayCap DATE
)

CREATE VIEW vChiTietNghiepVuNhanVien


AS SELECT
NhanVien.MaNhanVien,HoTen, NghiepVu.TenNghiepVu,
ChungChi.TenChungChi, NghiepVuNhanVien.NgayCap
FROM NghiepVuNhanVien
INNER JOIN NhanVien ON NghiepVuNhanVien.MaNhanVien =
NhanVien.MaNhanVien
INNER JOIN NghiepVu ON NghiepVuNhanVien.MaNghiepVu =
NghiepVu.MaNghiepVu
INNER JOIN ChungChi ON NghiepVuNhanVien.MaChungChi =
ChungChi.MaChungChi

CREATE PROCEDURE spThemNghiepVuNhanVien


@MaNhanVien INT, @TenNghiepVu NVARCHAR(30), @TenChungChi NVARCHAR(30),
@NgayCap DATE
AS BEGIN
DECLARE @Dem INT
SET @Dem = (SELECT COUNT(*)FROM NhanVien WHERE MaNhanVien =
@MaNhanVien)
IF (@Dem = 0)
RAISERROR (N' Ma nhan vien nay khong ton tai', 16, 1)
ELSE BEGIN
SET @Dem = (SELECT COUNT(*)FROM NghiepVu WHERE TenNghiepVu =
@TenNghiepVu)
IF (@Dem = 0)
RAISERROR (N' Ten nghiep vu nay khong ton tai', 16, 1)
ELSE BEGIN
SET @Dem = (SELECT COUNT(*)FROM ChungChi WHERE
TenChungChi = @TenChungChi)
IF (@Dem = 0)
RAISERROR (N' ten chung chi nay khong ton tai', 16,
1)
ELSE BEGIN
DECLARE @MaNghiepVu INT, @MaChungChi INT
SET @MaNghiepVu = (SELECT MaNghiepVu FROM NghiepVu
WHERE TenNghiepVu = @TenNghiepVu)
SET @MaChungChi = (SELECT MaChungChi FROM ChungChi
WHERE TenChungChi = @TenChungChi)
SET @Dem = (SELECT COUNT(*)FROM NghiepVuNhanVien
WHERE MaNhanVien = @MaNhanVien AND MaNghiepVu = @MaNghiepVu AND MaChungChi
= @MaChungChi)
IF (@Dem = 0)
INSERT NghiepVuNhanVien VALUES (@MaNhanVien,
@MaNghiepVu, @MaChungChi, @NgayCap)
ELSE
RAISERROR (N' nhan vien nghiep vu chung chi
nay da duoc luu tru', 16, 1)
END
END
END
END

CREATE FUNCTION TimKiemNghiepVu (@MaNghiepVu INT = NULL, @TenNghiepVu


NVARCHAR(30) = NULL)
RETURNS TABLE
AS RETURN(
SELECT*FROM NghiepVu
WHERE @MaNghiepVu IS NULL OR MaNghiepVu = @MaNghiepVu
AND @TenNghiepVu IS NULL OR TenNghiepVu LIKE '%' + @TenNghiepVu + '%'
)

CREATE PROCEDURE XoaNhanVien @MaNhanVien INT


AS BEGIN
DELETE FROM NhanVien WHERE MaNhanVien = @MaNhanVien
DELETE FROM NghiepVuNhanVien WHERE MaNhanVien = @MaNhanVien
END

Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PhamThiMinhPhuong_94685
{
public partial class FrmQuanLyNhanVien : Form
{
public FrmQuanLyNhanVien()
{
InitializeComponent();
}
private void loadDgvNhanVien()
{
dgvNhanVien.DataSource = Database.Query("SELECT * FROM NhanVien");
//cbbLoaiPhim.DataSource = Database.Query("SELECT * FROM LoaiPhim");
- trong trường hợp dùng cbb
}
private void FrmQuanLyNhanVien_Load(object sender, EventArgs e)
{
loadDgvNhanVien();
}
private bool check()
{
bool kt = true;
if (tboHoTen.Text.Trim() == "")
{
loi.SetError(tboHoTen, "Chưa nhập họ tên");
kt = false;
}
if (tboHoTen.Text.Trim().Length > 30)
{
loi.SetError(tboHoTen, "Họ tên vượt quá 30 ký tự");
kt = false;
}
if (tboDienThoai.Text.Trim() == "")
{
loi.SetError(tboDienThoai, "Chưa nhập số điện thoại");
kt = false;
}
return kt;
}

private void dgvNhanVien_RowEnter(object sender, DataGridViewCellEventArgs e)


{
tboMaNhanVien.Text =
dgvNhanVien.Rows[e.RowIndex].Cells["colMaNhanVien"].Value.ToString();
tboHoTen.Text = dgvNhanVien.Rows[e.RowIndex].Cells["colHoTen"].Value.ToString();
//cbbLoaiPhim.Text =
dgvPhimDienAnh.Rows[e.RowIndex].Cells["colTenLoaiPhim"].Value.ToString();
}

private void btnThem_Click(object sender, EventArgs e)


{
if (check() != true)
return;
string sql = "INSERT into NhanVien(HoTen,DienThoai) values(@HoTen,@DienThoai)";
Dictionary<string, object> para = new Dictionary<string, object>();
para.Add("@HoTen", tboHoTen.Text);
para.Add("@DienThoai", tboDienThoai.Text);
//para.Add("@MaLoaiPhim", cbbLoaiPhim.SelectedValue);
try
{
Database.Execute(sql, para);
loadDgvNhanVien();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void btnSua_Click(object sender, EventArgs e)


{
if (check() != true)
return;
if (tboMaNhanVien.Text == "")
{
loi.SetError(tboMaNhanVien, "Chưa chọn mã");
return;
}
string sql = "UPDATE NhanVien SET HoTen=@HoTen,DienThoai=@DienThoai where
MaNhanVien=@MaNhanVien";
Dictionary<string, object> para = new Dictionary<string, object>();
para.Add("@MaNhanVien", tboMaNhanVien.Text);
para.Add("@HoTen", tboHoTen.Text);
para.Add("@DienThoai", tboDienThoai.Text);
//para.Add("@MaLoaiPhim", cbbLoaiPhim.SelectedValue);
try
{
Database.Execute(sql, para);
loadDgvNhanVien();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void btnXoa_Click(object sender, EventArgs e)


{
if (tboMaNhanVien.Text == "")
{
loi.SetError(tboMaNhanVien, "Chưa chọn mã");
return;
}
string sql = "EXEC XoaNhanVien @MaNhanVien";
Dictionary<string, object> para = new Dictionary<string, object>();
para.Add("@MaNhanVien", dgvNhanVien.CurrentRow.Cells["colMaNhanVien"].Value);
try
{
Database.Execute(sql, para);
loadDgvNhanVien();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void btnTimKiem_Click(object sender, EventArgs e)


{
string sql = "SELECT * FROM TimKiemNhanVien(@MaNhanVien,@HoTen)";
Dictionary<string, object> para = new Dictionary<string, object>();
if (chbTimMa.Checked)
{
para.Add("@MaNhanVien", tboTimMa.Text);
}
else
{
para.Add("@MaNhanVien", DBNull.Value);
}
if (chbTimTen.Checked)
{
para.Add("@HoTen", tboTimTen.Text);
}
else
{
para.Add("@HoTen", DBNull.Value);
}
try
{
dgvNhanVien.DataSource = Database.Query(sql, para);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void chbTimMa_CheckedChanged(object sender, EventArgs e)


{
if (chbTimMa.Checked == true)
tboTimMa.Enabled = true;
else
{
tboTimMa.Clear();
tboTimMa.Enabled = false;
}
}

private void chbTimTen_CheckedChanged(object sender, EventArgs e)


{
if (chbTimTen.Checked == true)
tboTimTen.Enabled = true;
else
{
tboTimTen.Clear();
tboTimTen.Enabled = false;
}
}
}
}

Database.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhamThiMinhPhuong_94685
{
class Database
{
private static SqlConnection conn = new SqlConnection(@"Data Source=LAPTOP-
4A3VP30J;Initial Catalog=BaiKiemTra3;Integrated Security=True;Encrypt=False");
public static void Execute(string sql, Dictionary<string, object> parameters = null)
{
conn.Open();
SqlCommand command = new SqlCommand(sql, conn);
if (parameters != null)
foreach (string key in parameters.Keys)
command.Parameters.Add(new SqlParameter(key, parameters[key]));
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}

public static DataTable Query(string sql, Dictionary<string, object> parameters = null)


{
conn.Open();
SqlCommand command = new SqlCommand(sql, conn);
if (parameters != null)
foreach (string key in parameters.Keys)
command.Parameters.Add(new SqlParameter(key, parameters[key]));
try
{
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
conn.Close();
return table;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy