728x90 AdSpace

Latest News
Saturday, June 9, 2018

C# Kết Nối CSDL - Bài 5: Danh Sách Món Ăn (Có ListBox)

Đề: Thiết Kế From Theo Mẫu Sau



Yêu Cầu
-Tao Cở sở dữ liệu gồm 2 bảng
+khachhang(makhach,tenkhach,ngaysinh,gioitinh,sdt,mamon)
+mon (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 khachhang
{
    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=khach;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 khachhang
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void load()
        {
            ketnoi.moketnoi();
            ds.DataSource = ketnoi.htbang("select * from mon");
            ds.DisplayMember = "tenmon";
            ds.ValueMember = "mamon";

            bang.DataSource = ketnoi.htbang("select * from khachhang");
            ketnoi.dongketnoi();

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            load();
        }
//xử lý Data GirdView
        private void bang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
     
            int t = bang.CurrentCell.RowIndex;
            makhach.Text = bang.Rows[t].Cells[0].Value.ToString();
            tenkhach.Text = bang.Rows[t].Cells[1].Value.ToString();
            ngaysinh.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();
            mmon.Text = bang.Rows[t].Cells[5].Value.ToString();
   
        }

//xử lý ListView
        private void ds_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ma = ds.SelectedValue.ToString();
            bang.DataSource = ketnoi.htbang("select * from khachhang where khachhang.mamon='" + ma + "'");
        }

//xử lý Data GirdView khi bấm vào mỗi ô
        private void bang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        
            int t = bang.CurrentCell.RowIndex;
            makhach.Text = bang.Rows[t].Cells[0].Value.ToString();
            tenkhach.Text = bang.Rows[t].Cells[1].Value.ToString();
            ngaysinh.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();
            mmon.Text = bang.Rows[t].Cells[5].Value.ToString();
         
        }

        private void them_Click(object sender, EventArgs e)
        {

//xử lý bắt lỗi khi để trống textbox
            if (makhach.Text == "")
            {
                MessageBox.Show("bạn chưa nhập mã", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            else if (tenkhach.Text == "")
            {
                MessageBox.Show("bạn chưa nhập tên", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tenkhach.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 số điện thoại", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (gt.Text == "")
            {
                MessageBox.Show("bạn chưa cho biết giới tính", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (mmon.Text == "")
            {
                MessageBox.Show("bạn chưa nhập mã món ăn", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

//xử lý nút thêm
            else
            {
                ketnoi.moketnoi();
                ketnoi.thuchien("insert into khachhang values('" + makhach.Text + "',N'" + tenkhach.Text + "','" + ngaysinh.Text.ToString() + "','" + int.Parse(sdt.Text) + "',N'" + gt.Text + "',N'" + mmon.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 khachhang set makhach='" + makhach.Text + "',tenkhach=N'" + tenkhach.Text.ToString() + "',ngaysinh='" + ngaysinh.Text.ToString() + "',sdt='" + int.Parse(sdt.Text) + "',gioitinh=N'"+gt.Text+"',mamon=N'" + mmon.Text + "' where makhach='" + 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 khachhang where makhach='"+bang.Rows[bang.CurrentCell.RowIndex].Cells[0].Value.ToString()+"'");
                ketnoi.dongketnoi();
                load();
                MessageBox.Show("Đã Xóa", "Bingo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

//xử lý bắt lỗi không cho nhập text vào textbox
        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 makhach_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 huy_Click(object sender, EventArgs e)
        {
            makhach.Text = "";
            tenkhach.Text = "";
            ngaysinh.ResetText();
            mmon.Text = "";
            gt.Text = "";
            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();
            }
        }
    }
}



  • Blogger Comments
  • Facebook Comments

1 comments:

Item Reviewed: C# Kết Nối CSDL - Bài 5: Danh Sách Món Ăn (Có ListBox) Rating: 5 Reviewed By: Genm