Đề:Tạo From như hình sau
Yêu cầu:
-Tao Cở sở dữ liệu gồm 2 bảng
+nhanvien1 (manv,tennv,ngaysinh,sodt,mapb)
+phongban1 (mapb,tebpb)
-viết thuộc tính cho các nút:thêm,sửa,xóa,lưu,hủy,thoát
-có bắt lỗi
-kết nối 2 bảng dữ liệu hiển thị lên Data GridView
Demo Chương Trình:
-Tao Cở sở dữ liệu gồm 2 bảng
+nhanvien1 (manv,tennv,ngaysinh,sodt,mapb)
+phongban1 (mapb,tebpb)
-viết thuộc tính cho các nút:thêm,sửa,xóa,lưu,hủy,thoát
-có bắt lỗi
-kết nối 2 bảng dữ liệu hiển thị lên Data GridView
Demo Chương Trình:
Code Xử Lý:
*tạo class kết nối:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace qlnhansu
{
class ketnoi
{
public static SqlConnection conn = null;
public static SqlCommand cmd = null;
public static SqlDataAdapter ad = null;
public static string chuoi = @"Data Source=DESKTOP-0HGGLRP\SQLEXPRESS01;Initial Catalog=nhansu1;Integrated Security=True";
public static void moketnoi()
{
conn = new SqlConnection(chuoi);
conn.Open();
}
public static void dongketnoi()
{
conn.Close();
}
public static DataTable htbang(string sql)
{
cmd = new SqlCommand(sql, conn);
ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
return dt;
}
public static void thuchien(string sql)
{
cmd=new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
}
}
}
*Xử Lý From:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace qlnhansu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void load()
{
// phần lấy dữ liệu từ 2 bảng
// phần lấy dữ liệu từ 2 bảng
ketnoi.moketnoi();
bang.DataSource=ketnoi.htbang("select manv,tennv,ngaysinh,sodt,nhanvien1.mapb,phongban1.tenpb from nhanvien1,phongban1 where nhanvien1.mapb=phongban1.mapb");
ten2.DataSource=ketnoi.htbang("select * from phongban1");
ten2.DisplayMember="mapb";
ten2.ValueMember="mapb";
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void bang_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ketnoi.moketnoi();
int t = bang.CurrentCell.RowIndex;
ma1.Text = bang.Rows[t].Cells[0].Value.ToString();
ten1.Text = bang.Rows[t].Cells[1].Value.ToString();
time.Text = bang.Rows[t].Cells[2].Value.ToString();
sdt.Text = bang.Rows[t].Cells[3].Value.ToString();
ten2.Text = bang.Rows[t].Cells[4].Value.ToString();
ketnoi.dongketnoi();
load();
}
private void them_Click(object sender, EventArgs e)
{
if (ma1.Text == "")
{
MessageBox.Show("bạn chưa nhập mã nhân viên", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (ten1.Text == "")
{
MessageBox.Show("bạn chưa nhập tên nhân viên", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (sdt.Text == "")
{
MessageBox.Show("bạn chưa nhập sdt", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
ketnoi.moketnoi();
ketnoi.thuchien("insert into nhanvien1 values('" + ma1.Text + "',N'" + ten1.Text + "','" + time.Text.ToString() + "','" + int.Parse(sdt.Text) + "','" + ten2.Text + "')");
ketnoi.dongketnoi();
load();
MessageBox.Show("đã thêm vào", "bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void sua_Click(object sender, EventArgs e)
{
ketnoi.moketnoi();
ketnoi.thuchien("update nhanvien1 set manv='" + ma1.Text + "',tennv=N'" + ten1.Text + "',ngaysinh='" + time.Text.ToString() + "',sodt='" + int.Parse(sdt.Text) + "',mapb='"+ten2.Text.ToString()+"' where manv='" + bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'");
ketnoi.dongketnoi();
load();
MessageBox.Show("đã sửa", "bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void xoa_Click(object sender, EventArgs e)
{
DialogResult = MessageBox.Show("bạn có muốn xóa", "chú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (DialogResult == DialogResult.OK)
{
ketnoi.moketnoi();
ketnoi.thuchien("delete from nhanvien1 where manv='" + bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'");
ketnoi.dongketnoi();
load();
MessageBox.Show("đã xóa", "bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void huy_Click(object sender, EventArgs e)
{
ma1.Text = "";
ten1.Text = "";
sdt.Text = "";
}
private void thoat_Click(object sender, EventArgs e)
{
DialogResult thongbao;
thongbao = (MessageBox.Show("bạn có muốn thoát", "chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Warning));
if(thongbao==DialogResult.Yes)
{
Application.Exit();
}
}
private void sdt_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
}
private void sdt_TextChanged(object sender, EventArgs e)
{
}
private void ten1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled=!((e.KeyChar >=65 && e.KeyChar <=122 || e.KeyChar==8));
}
}
}
0 comments:
Post a Comment