728x90 AdSpace

Latest News
Tuesday, June 12, 2018

C# Kết Nối CSDL - Bài 8: Thông Tin Học Sinh

Đề: Thiết Kế from theo mẫu sau



Yêu Cầu
-Tao Cở sở dữ liệu gồm 2 bảng
+hocsinh(mahs,tenhs,ngaysinh,diachi,dtb,malop)
+lop (malop,tenlop,siso)

-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 hocsinh
{
    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=hocsinh6;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 hocsinh
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void load()
        {

            ketnoi.moketnoi();
            ma2.DataSource = ketnoi.htbang("select *from lop");
            ma2.DisplayMember = "malop";
            ma2.ValueMember = "malop";
          

            bang.DataSource = ketnoi.htbang("select * from hocsinh");
            ketnoi.dongketnoi();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            load();
            
        }
//Xử Lý DataGirdView
        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();
            dc.Text = bang.Rows[t].Cells[3].Value.ToString();
            dtb.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();
            dc.Text = bang.Rows[t].Cells[3].Value.ToString();
            dtb.Text = bang.Rows[t].Cells[4].Value.ToString();
            ma2.Text = bang.Rows[t].Cells[5].Value.ToString();
         
           
        }
//khi chọn vào mã lóp bất kỳ sẽ hiện thông tin học sinh trên datagird view
private void ma2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ma = ma2.SelectedValue.ToString();
            bang.DataSource = ketnoi.htbang("select *from hocsinh where hocsinh.malop='"+ma+"'");

            

        }
//Xử Lý bắt lỗi khi để TextBox Trống
        private void button1_Click(object sender, EventArgs e)
        {
            if (ma1.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Mã Thưa Ngài", "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 (dc.Text == "")
            {
                MessageBox.Show("Bạn Chưa nhập địa chỉ", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            else if (dtb.Text == "")
            {
                MessageBox.Show("Bạn Chưa nhập Điểm", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            
//Xử Lý nút thêm
            else
            {
                ketnoi.moketnoi();
                ketnoi.thuchien("insert into hocsinh values('" + ma1.Text + "',N'" + ten.Text + "','" + DateTime.Parse(ns.Text) + "',N'" + dc.Text + "','" + int.Parse(dtb.Text) + "','" + ma2.SelectedValue + "')");
                ketnoi.dongketnoi();
                load();
                ma1.Text = "";
                ten.Text = "";
                ma2.ResetText();
                ns.ResetText();
                dc.Text = "";
                dtb.Text = "";
                MessageBox.Show("Đã Thêm Dữ Liệu", "Bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
//Xử Lý bắt lỗi khi dữa kiệu trống
            if (ma1.Text == "" && ten.Text == "" && dc.Text == "" && dtb.Text == "")
            {
                MessageBox.Show("Bạn Chưa Chọn Dữ Liệu Để Sửa", "Chú Ý", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
//Xử Lý nút sửa
            else
            {
                ketnoi.moketnoi();
                ketnoi.thuchien("update hocsinh set mahs='" + ma1.Text + "',tenhs=N'" + ten.Text.ToString() + "',ngaysinh='" + DateTime.Parse(ns.Text) + "',diachi=N'" + dc.Text + "',dtb='" + int.Parse(dtb.Text) + "',malop='" + ma2.SelectedValue + "' where mahs='" + bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'");
                MessageBox.Show("Đã Sửa", "Bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                load();
                ma1.Text = "";
                ten.Text = "";
                ma2.ResetText();
                ns.ResetText();
                dc.Text = "";
                dtb.Text = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult = MessageBox.Show("bạn có muốn xóa", "cú ý", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (DialogResult == DialogResult.OK)
            {
                if (ma1.Text == "" && ten.Text == "" && dc.Text == "" && dtb.Text == "")
                {
                    MessageBox.Show("Bạn Chưa Chọn Dữ Liệu Để Sửa", "Chú Ý", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else
                {
                    ketnoi.moketnoi();
                    ketnoi.thuchien("delete from hocsinh where mahs='" + bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString() + "'");
                    ketnoi.dongketnoi();
                    load();
                    MessageBox.Show("Đã xóa", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }
//Xử Lý bắt lỗi không cho nhập Text vào Textbox
        private void dtb_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);
            }
        }
//Xử Lý bắt lỗi không cho nhập Text vào Textbox
        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);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ma1.Text = "";
            ma2.ResetText();
            ns.ResetText();
            dc.Text = "";
            dtb.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult thongbao = (MessageBox.Show("bạn có Muốn Thoát", "chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question));
            if (thongbao == DialogResult)
            {
                Application.Exit();
            }
        }
    }
}


  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: C# Kết Nối CSDL - Bài 8: Thông Tin Học Sinh Rating: 5 Reviewed By: Genm