Đề: Thiết Kế From như hình sau
Yêu Cầu
-Tao Cở sở dữ liệu gồm 2 bảng
+Rider(marider,tenrider,namsinh,gioitinh,maloai)
+loai (maloai,tenloai)
-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
+Rider(marider,tenrider,namsinh,gioitinh,maloai)
+loai (maloai,tenloai)
-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 kamenrider
{
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=kamenrider;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 kamenrider
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void load()
{
ketnoi.moketnoi();
bang.DataSource=ketnoi.htbang("select marider,tenrider,namsinh,gioitinh,rider.maloai,loai.tenloai from rider,loai where rider.maloai=loai.maloai");
ma2.DataSource=ketnoi.htbang("select *from loai");
ma2.DisplayMember = "maloai";
ma2.ValueMember = "maloai";
ketnoi.dongketnoi();
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void them_Click(object sender, EventArgs e)
{
// xử lý bắt lỗi để trống Textbox
// xử lý bắt lỗi để trống Textbox
if (ma1.Text == "")
{
MessageBox.Show("bạn chưa nhập mã rider", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (ten.Text == "")
{
MessageBox.Show("bạn chưa nhập tên rider", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (ten.Text == "")
{
MessageBox.Show("bạn chưa nhập tên rider", "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);
}
// xử lý button thêm
// xử lý button thêm
else
{
ketnoi.moketnoi();
ketnoi.thuchien("insert into rider values('" + ma1.Text + "',N'" + ten.Text + "','" + time.Text.ToString() + "',N'" + gt.Text + "','" + ma2.Text + "')");
ketnoi.dongketnoi();
load();
MessageBox.Show("Đã Thêm", "Bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void sua_Click(object sender, EventArgs e)
{
ketnoi.moketnoi();
ketnoi.thuchien("update rider set marider='"+ma1.Text+"',tenrider=N'"+ten.Text+"',namsinh='"+time.Text.ToString()+"',gioitinh=N'"+gt.Text+"',maloai='"+ma2.Text.ToString()+"' where marider ='"+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 thoát", "chú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (DialogResult == DialogResult.OK)
{
ketnoi.moketnoi();
ketnoi.thuchien("delete from rider where marider='" + bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'");
ketnoi.dongketnoi();
load();
MessageBox.Show("Đã xó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();
time.Text = bang.Rows[t].Cells[2].Value.ToString();
gt.Text = bang.Rows[t].Cells[3].Value.ToString();
ma2.Text = bang.Rows[t].Cells[4].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();
time.Text = bang.Rows[t].Cells[2].Value.ToString();
gt.Text = bang.Rows[t].Cells[3].Value.ToString();
ma2.Text = bang.Rows[t].Cells[4].Value.ToString();
}
private void huy_Click(object sender, EventArgs e)
{
ma1.Text = "";
ten.Text = "";
gt.Text = "";
time.ResetText();
}
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();
}
}
}
}
0 comments:
Post a Comment