Đề: Thiết Kế From Như Hình Sau
Yêu cầu:
-Tao Cở sở dữ liệu gồm 2 bảng
+nhanvien (manv,tennv,ngaysinh,sodt,gioitinh,macv)
+chucvu (macv,tencv)
-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 From 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 chucvu
{
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=nhanvien;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();
}
}
}
-Tao Cở sở dữ liệu gồm 2 bảng
+nhanvien (manv,tennv,ngaysinh,sodt,gioitinh,macv)
+chucvu (macv,tencv)
-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 From 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 chucvu
{
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=nhanvien;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();
}
}
}
Code xử lý Trong 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 chucvu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void load()
{
ketnoi.moketnoi();
ds.DataSource = ketnoi.htbang("select * from chucvu");
ds.DisplayMember = "tencv";
ds.ValueMember = "macv";
bang.DataSource = ketnoi.htbang("select *from nhanvien");
ketnoi.dongketnoi();
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void ds_SelectedIndexChanged(object sender, EventArgs e)
{
string ma = ds.SelectedValue.ToString();
bang.DataSource = ketnoi.htbang("select * from nhanvien where nhanvien.macv='" + ma + "'");
}
private void button1_Click(object sender, EventArgs e)
{
if (ma1.Text == "")
{
MessageBox.Show("bạn chưa nhập mã nv", "chú Ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (ten.Text == "")
{
MessageBox.Show("bạn chưa nhập tê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 if (gt.Text == "")
{
MessageBox.Show("bạn chưa nhập gt", "chú Ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (ma2.Text == "")
{
MessageBox.Show("bạn chưa nhập mã chức vụ", "chú Ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
ketnoi.moketnoi();
ketnoi.thuchien("insert into nhanvien values('" + ma1.Text + "',N'" + ten.Text + "','" + DateTime.Parse(ns.Text) + "','" + int.Parse(sdt.Text) + "',N'" + gt.Text + "','" + ma2.Text + "')");
ketnoi.dongketnoi();
load();
MessageBox.Show("Đã Thêm", "Bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (ma1.Text == "" && ma2.Text == "" && ten.Text == "" && sdt.Text == "" && gt.Text == "")
{
MessageBox.Show("Chưa Chọn Ô để sửa", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
ketnoi.moketnoi();
ketnoi.thuchien("update nhanvien set manv='" + ma1.Text + "',tennv=N'" + ten.Text.ToString() + "',ngaysinh='" + DateTime.Parse(ns.Text) + "',sdt='" + int.Parse(sdt.Text) + "',gioitinh=N'" + gt.Text + "',macv='" + ma2.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 bang_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int t = bang.CurrentCell.RowIndex;
ma1.Text = bang.Rows[t].Cells[0].Value.ToString();
ten.Text = bang.Rows[t].Cells[1].Value.ToString();
ns.Text = bang.Rows[t].Cells[2].Value.ToString();
sdt.Text = bang.Rows[t].Cells[3].Value.ToString();
gt.Text = bang.Rows[t].Cells[4].Value.ToString();
ma2.Text = bang.Rows[t].Cells[5].Value.ToString();
}
private void bang_CellClick(object sender, DataGridViewCellEventArgs e)
{
int t = bang.CurrentCell.RowIndex;
ma1.Text = bang.Rows[t].Cells[0].Value.ToString();
ten.Text = bang.Rows[t].Cells[1].Value.ToString();
ns.Text = bang.Rows[t].Cells[2].Value.ToString();
sdt.Text = bang.Rows[t].Cells[3].Value.ToString();
gt.Text = bang.Rows[t].Cells[4].Value.ToString();
ma2.Text = bang.Rows[t].Cells[5].Value.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
DialogResult = MessageBox.Show("bạn có muosn xóa", "chú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (DialogResult == DialogResult.OK)
{
if (ma1.Text == "" && ma2.Text == "" && ten.Text == "" && sdt.Text == "" && gt.Text == "")
{
MessageBox.Show("Chưa chọn dữ liệu để xóa", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
ketnoi.moketnoi();
ketnoi.thuchien("delete from nhanvien 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 button4_Click(object sender, EventArgs e)
{
ma1.Text = "";
ten.Text = "";
ma2.Text = "";
ns.ResetText();
sdt.Text = "";
gt.Text = "";
}
private void button5_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;
MessageBox.Show("ai cho bạn nhập text vào đây", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void ma1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
MessageBox.Show("ai cho bạn nhập text vào đây", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
0 comments:
Post a Comment