Đề: thiết kế from theo mẫu sau
Yêu Cầu
-Tao Cở sở dữ liệu gồm 2 bảng
+sinhvien(masv,tensv,ngaysinh,gioitinh,sodt,diachi,mamon)
+monhoc (mamon,tenmon)
-viết thuộc tính cho các nút:thêm,sửa,xóa,hủy,thoát
-xự kiện xử lý bắt lỗi
-kết nối 2 bảng dữ liệu hiển thị lên Data GridView
+sinhvien(masv,tensv,ngaysinh,gioitinh,sodt,diachi,mamon)
+monhoc (mamon,tenmon)
-viết thuộc tính cho các nút:thêm,sửa,xóa,hủy,thoát
-xự kiện xử lý 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 sinhvien1
{
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=sinhvien1;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 sinhvien1
{
public partial class xoa : Form
{
public xoa()
{
InitializeComponent();
}
public void load()
{
ketnoi.moketnoi();
ds.DataSource = ketnoi.htbang("select * from monhoc");
ds.DisplayMember = "tenmon";
ds.ValueMember = "mamon";
bang.DataSource = ketnoi.htbang("select * from sinhvien");
ketnoi.dongketnoi();
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void bang_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int t = bang.CurrentCell.RowIndex;
masinhvien.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();
gt.Text = bang.Rows[t].Cells[3].Value.ToString();
sdt.Text = bang.Rows[t].Cells[4].Value.ToString();
dc.Text = bang.Rows[t].Cells[5].Value.ToString();
mamonhoc.Text = bang.Rows[t].Cells[6].Value.ToString();
}
private void bang_CellClick(object sender, DataGridViewCellEventArgs e)
{
int t = bang.CurrentCell.RowIndex;
masinhvien.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();
gt.Text = bang.Rows[t].Cells[3].Value.ToString();
sdt.Text = bang.Rows[t].Cells[4].Value.ToString();
dc.Text = bang.Rows[t].Cells[5].Value.ToString();
mamonhoc.Text = bang.Rows[t].Cells[6].Value.ToString();
}
private void ds_SelectedIndexChanged(object sender, EventArgs e)
{
string ma = ds.SelectedValue.ToString();
bang.DataSource = ketnoi.htbang("select * from sinhvien where sinhvien.mamon='" + ma + "'");
}
private void them_Click(object sender, EventArgs e)
{
//bắt lỗi khi để textbox trống
if (masinhvien.Text == "")
{
MessageBox.Show("bạn Chưa Nhập Mã", "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 (gt.Text == "")
{
MessageBox.Show("bạn Chưa Nhập giới tính", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (sdt.Text == "")
{
MessageBox.Show("bạn Chưa Nhập số điện thoại", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (dc.Text == "")
{
MessageBox.Show("bạn Chưa Nhập địa chỉ", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (mamonhoc.Text == "")
{
MessageBox.Show("bạn Chưa Nhập mã môn học", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//xử lý nút thêm
else
{
ketnoi.moketnoi();
ketnoi.thuchien("insert into sinhvien values('" + masinhvien.Text + "',N'" + ten.Text + "','" + DateTime.Parse(ns.Text) + "',N'" + gt.Text + "','" + int.Parse(sdt.Text) + "',N'" + dc.Text + "','" + mamonhoc.Text + "')");
ketnoi.dongketnoi();
load();
MessageBox.Show("Đã Thêm", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void sua_Click(object sender, EventArgs e)
{
//bắt lỗi khi chưa chọn dòng để sửa
if (masinhvien.Text == "" && ten.Text == "" && gt.Text == "" && dc.Text == "" && sdt.Text == "")
{
MessageBox.Show("Chưa Chọn Thông Tin Để Sửa", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
ketnoi.moketnoi();
ketnoi.thuchien("update sinhvien set masv='" + masinhvien.Text + "',tensv=N'" + ten.Text.ToString() + "',ngaysinh='" + DateTime.Parse(ns.Text) + "',gioitinh=N'" + gt.Text + "',sodt='" + int.Parse(sdt.Text) + "',diachi=N'" + dc.Text + "',mamon='" + mamonhoc.Text.ToString() + "' where masv='" + bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'");
ketnoi.dongketnoi();
MessageBox.Show("Đã sửa", "Bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
load();
}
}
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 sinhvien where masv='" + bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'");
ketnoi.dongketnoi();
load();
MessageBox.Show("Đã xóa", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void huy_Click(object sender, EventArgs e)
{
masinhvien.Text = "";
ten.Text = "";
dc.Text = "";
mamonhoc.Text = "";
gt.Text = "";
ns.ResetText();
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)
{
//bắt lỗi không cho nhập chữ vào textbox
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