NGUYỄN PHÚC MINH
PHẦN 1
DataProvider
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace QuanLyNhanVien_SQLServer
{
class DataProvider
{
static SqlConnection conn = new SqlConnection();
//Open
public static void TaoKN()
{
conn.ConnectionString = "Data Source=DESKTOP-109H3CI;Initial Catalog=QLNV;Integrated Security=True";
try
{
conn.Open();
}
catch(Exception)
{
}
}
//hàm đóng
public static void DongKN()
{
conn.Close();
}
//hàm load dữ liệu
public static DataTable getdata(string query)
{
TaoKN();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(dt);
DongKN();
return dt;
}
//hàm thêm xóa sửa
public void ExeCuteNonQuery(string sql)
{
TaoKN();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
DongKN();
}
}
}
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;
using System.Data.SqlClient;
namespace QuanLyNhanVien_SQLServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void LoadDS()
{
DataSet ds;
SqlDataAdapter da;
string sConn = "Data Source=DESKTOP-8FP6BR2;Initial Catalog=QLNV;Integrated Security=True";
string sQuery = "select * from nhanvien";
da = new SqlDataAdapter(sQuery, sConn);
ds = new DataSet("dsQLNV");
da.Fill(ds, "NhanVien");
dgvDSNV.DataSource = ds.Tables["NhanVien"];
}
void LoadTenCV()
{
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-109H3CI;Initial Catalog=QLNV;Integrated Security=True");
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from chucvu", conn);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
cboChucVu.Items.Add(dr["TenCV"].ToString());
}
conn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadDS();
LoadTenCV();
}
private void dgvDSNV_CellClick(object sender, DataGridViewCellEventArgs e)
{
//txtMaSo.Enabled = false;
int i;
i = dgvDSNV.CurrentRow.Index;
txtMaSo.Text = dgvDSNV.Rows[i].Cells[0].Value.ToString();
txtHoLot.Text = dgvDSNV.Rows[i].Cells[1].Value.ToString();
txtTen.Text = dgvDSNV.Rows[i].Cells[2].Value.ToString();
if (dgvDSNV.Rows[i].Cells[3].Value.ToString() == "Nam")
radNam.Checked = true;
else
radNu.Checked = true;
dtpNgaySinh.Text = dgvDSNV.Rows[i].Cells[4].Value.ToString();
if (dgvDSNV.Rows[i].Cells[5].Value.ToString() == "CV")
cboChucVu.Text = "Nhân viên chuyên trách";
else if (dgvDSNV.Rows[i].Cells[5].Value.ToString() == "KT")
cboChucVu.Text = "Kế toán";
else if (dgvDSNV.Rows[i].Cells[5].Value.ToString() == "LX")
cboChucVu.Text = "Lái xe cơ quan";
else if (dgvDSNV.Rows[i].Cells[5].Value.ToString() == "PP")
cboChucVu.Text = "Phó trưởng phòng";
else if (dgvDSNV.Rows[i].Cells[5].Value.ToString() == "TP")
cboChucVu.Text = "Trưởng phòng";
}
string cv = "";
string phai = "";
private void btnThem_Click(object sender, EventArgs e)
{
if (cboChucVu.Text == "Nhân viên chuyên trách")
cv = "CV";
else if (cboChucVu.Text == "Kế toán")
cv = "KT";
else if (cboChucVu.Text == "Lái xe cơ quan")
cv = "LX";
else if (cboChucVu.Text == "Phó trưởng phòng")
cv = "PP";
else if (cboChucVu.Text == "Trưởng phòng")
cv = "TP";
if (radNam.Checked == true)
{
phai = "Nam";
}
else
{
phai = "Nữ";
}
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-8FP6BR2;Initial Catalog=QLNV;Integrated Security=True");
SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[nhanvien]
([manv]
,[holot]
,[tennv]
,[phai]
,[ngaysinh]
,[macv])
VALUES
('" + txtMaSo.Text + "', N'" +txtHoLot.Text + "', N'" + txtTen.Text + "', N'" + phai + "', N'" + dtpNgaySinh.Text + "', '" + cv + "')", conn);//insert vào database
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
LoadDS();
}
private void btnSua_Click(object sender, EventArgs e)
{
if (radNam.Checked == true)
{
phai = "Nam";
}
else
{
phai = "Nữ";
}
if (cboChucVu.Text == "Nhân viên chuyên trách")
cv = "CV";
else if (cboChucVu.Text == "Kế toán")
cv = "KT";
else if (cboChucVu.Text == "Lái xe cơ quan")
cv = "LX";
else if (cboChucVu.Text == "Phó trưởng phòng")
cv = "PP";
else if (cboChucVu.Text == "Trưởng phòng")
cv = "TP";
SqlConnection connect = new SqlConnection(@"Data Source=DESKTOP-8FP6BR2;Initial Catalog=QLNV;Integrated Security=True");
SqlDataAdapter da1 = new SqlDataAdapter("update nhanvien set holot =N'"+txtHoLot.Text+"', tennv = N'" + txtTen.Text + "', phai = N'" + phai + "', ngaysinh = '" + dtpNgaySinh.Text + "', macv = '" + cv + "' where manv='" + txtMaSo.Text + "'", connect);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
MessageBox.Show("Sửa thông tin thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
LoadDS();
}
private void btnXoa_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection(@"Data Source=DESKTOP-8FP6BR2;Initial Catalog=QLNV;Integrated Security=True");
SqlCommand cmd = new SqlCommand("delete from nhanvien where manv= '" + txtMaSo.Text + "'", connect);
connect.Open();
cmd.ExecuteNonQuery();
connect.Close();
MessageBox.Show("Xóa thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
LoadDS();
}
}
}
Chia sẻ với bạn bè của bạn: |