728x90 AdSpace

Latest News
Saturday, June 9, 2018

C# Kết Nối CSDL - Bài 4:Danh Mục Sản Phẩm(Lấy Dữ liệu từ 2 bảng)

Đề: Thiết Kế From Theo hình sau:



Yêu Cầu:

-Tao Cở sở dữ liệu gồm 2 bảng
+sanpham(masp,tensp,ngaynhap,maloai)
+loaisp (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 sanpham
{
    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=danhmucsp;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ý 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 sanpham
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void load()
        {

            ketnoi.moketnoi();
            bang.DataSource = ketnoi.htbang("select masp,tensp,ngaynhap,sanpham.maloai,loaisp.tenloai from sanpham,loaisp where sanpham.maloai=loaisp.maloai");
            loaisp.DataSource = ketnoi.htbang("select * from loaisp");
            loaisp.DisplayMember = "maloai";
            loaisp.ValueMember = "maloai";
            
            
            

        }
        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
            if (masp.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập mã sp", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tensp.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên sp", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
// xử lý cho nút thêm
            else
            {
                ketnoi.moketnoi();
                ketnoi.thuchien("insert into sanpham values('" + masp.Text + "',N'" + tensp.Text + "','" + time.Value.ToString() + "','" + loaisp.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 sanpham set masp='" + masp.Text + "',tensp=N'" + tensp.Text + "',ngaynhap='" + time.Value.ToString() + "',maloai='"+loaisp.Text+"'  where masp='" + 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;
            masp.Text = bang.Rows[t].Cells[0].Value.ToString();
            tensp.Text = bang.Rows[t].Cells[1].Value.ToString();
            time.Text = bang.Rows[t].Cells[2].Value.ToString();
            loaisp.Text = bang.Rows[t].Cells[3].Value.ToString();
           
        
        }

        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 sanpham where masp='" + 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)
        {
            tensp.Text = "";
            masp.Text = "";
         time.ResetText();

        }

        private void thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
// xử lý bắt lỗi không cho nhập chữ vào Textbox
        private void masp_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
            {
                e.Handled = true;
                MessageBox.Show("nhập sai dữ liệu", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

      

        private void bang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int t = bang.CurrentCell.RowIndex;
            masp.Text = bang.Rows[t].Cells[0].Value.ToString();
            tensp.Text = bang.Rows[t].Cells[1].Value.ToString();
            time.Text = bang.Rows[t].Cells[2].Value.ToString();
            loaisp.Text = bang.Rows[t].Cells[3].Value.ToString();
        }


    
    }
}

  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: C# Kết Nối CSDL - Bài 4:Danh Mục Sản Phẩm(Lấy Dữ liệu từ 2 bảng) Rating: 5 Reviewed By: Genm