IT SHARE EDU
Code Phần mềm quản lý khách hàng thực hành học c# ôn lại kiến thức đã học
- Tổng hợp các kiến thức đã học về Control nâng cao và kết nối cơ sở dữ liệu sql server để viết Phần mềm Quản Lý Khách Hàng
- Khách hàng: Mã, tên, giới tính, Điện Thoại, loại khách (VIP, Thường), địa chỉ, năm sinh,..
- Dùng ListView: Hiển thị theo Group VIP và thường, có icon giới tính nam hay nữ.
- Tính năng Phần mềm: Cho phép: Thêm, sửa, xóa, tìm kiếm, in ấn
Hướng dẫn Code Phần mềm quản lý khách hàng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace binding
{
public partial class frmQuanlyKhachHang : Form
{
public frmQuanlyKhachHang()
{
InitializeComponent();
}
SqlConnection conec = null;
string strconec = @”Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database= QLKH ; User ID=sa; pwd =123″;
private void MoKetnoi()
{
if (conec==null)
{
conec = new SqlConnection(strconec);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
}
private void DongKetNoi()
{
if (conec!=null && conec.State==ConnectionState.Open)
{
conec.Close();
}
}
private void frmQuanlyKhachHang_Load(object sender, EventArgs e)
{
HienThiSanPham();
}
private void HienThiSanPham()
{
try
{
MoKetnoi();
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = “Select * from KhachHang”;
command.Connection = conec;
SqlDataReader reader = command.ExecuteReader();
lvSanPham.Items.Clear();
lvSanPham.Groups.Clear();
ListViewGroup lvVip = new ListViewGroup(“Khách Hàng VIP”);// Hiện lên trên listview
lvSanPham.Groups.Add(lvVip);
ListViewGroup lvThuong = new ListViewGroup(“Khách Hàng Thường”);
lvSanPham.Groups.Add(lvThuong);
while (reader.Read())
{
int ma = reader.GetInt32(0);
string ten = reader.GetString(1);
int gioitinh = reader.GetInt32(2);
string dienthoai = reader.GetString(3);
string diachi = reader.GetString(4);
string loaikh = reader.GetString(5);
DateTime time = reader.GetDateTime(7);
ListViewItem lvi = new ListViewItem((lvSanPham.Items.Count+1)+””);
lvi.SubItems.Add(ma+””);
lvi.SubItems.Add(ten);
lvi.SubItems.Add(gioitinh==1 ?”Nam”:”Nữ”);
lvi.SubItems.Add(dienthoai + “”);
lvi.SubItems.Add(loaikh + “”);
lvi.SubItems.Add(time + “dd/MM/yyyy”);
lvi.SubItems.Add(diachi + “”);
lvSanPham.Items.Add(lvi);
if (string.Compare(loaikh, “Vip”, true)==0 )// true không phân biêt hô thường
{
lvi.Group = lvVip;
}
else
{
lvi.Group = lvThuong;
}
lvi.Tag = ma;// lấy mã khách hàng
if (gioitinh==0)
{
lvi.ImageIndex = 0;// hiện hình icon nữ
}
else if(gioitinh==1)
{
lvi.ImageIndex = 1;// hiện hình icon nam
}
}
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void lvSanPham_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvSanPham.SelectedItems.Count==0)
{
return;
}
ListViewItem lvi = lvSanPham.SelectedItems[0];// lấy 1 dòng dữ liệu
int ma = (int)lvi.Tag;//lấy mã khách hàng
hienthichitiet(ma);
}
private void hienthichitiet(int ma)
{
MoKetnoi();
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = “select * from KhachHang where Ma=” + ma;
command.Connection = conec;
SqlParameter parama = new SqlParameter(“ma”, SqlDbType.Int);
parama.Value = ma;
command.Parameters.Add(parama);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
string ten = reader.GetString(1);
int gioitinh = reader.GetInt32(2);
string dienthoai = reader.GetString(3);
string diachi = reader.GetString(4);
string loaikh = reader.GetString(5);
DateTime time = reader.GetDateTime(7);
txtMaKH.Text = ma + “”;
txtTenKH.Text = ten;
txtDiaChi.Text = diachi;
txtDienThoai.Text = dienthoai;
cboKH.Text = loaikh;
dtKh.Value = time;
if (gioitinh==1)// hiện nam
{
radNam.Checked = true;
}
else
{
radNu.Checked = true;
}
}
reader.Close();
}
}
}
Video hướng dẫn Code Phần mềm quản lý khách hàng