By GokiSoft.com|
09:30 20/10/2021|
C Sharp
[Video] Viết chương trình quản lý nhập kho máy tính cho thegioididong.com C Sharp - Lập trình C Sharp - C2010G
B1. Xây dựng database
create table computer (
id int primary key auto_increment,
name varchar(150) not null,
distribute_name varchar(150),
producer varchar(150),
produce_year int,
imported_date datetime,
buy_price float,
sell_price float
)
B2. Tạo project + add thư viện
B3. Models (Mapping Tables <-> Class Object)
#Models/Program.cs
using System;
using BT2493.DAO;
using BT2493.Models;
using System.Collections.Generic;
namespace BT2493
{
class Program
{
static void Main(string[] args)
{
int choose;
do
{
ShowMenu();
choose = int.Parse(Console.ReadLine());
switch(choose)
{
case 1:
Input();
break;
case 2:
Display();
break;
case 3:
Search();
break;
case 4:
Report();
break;
case 5:
Console.WriteLine("Thoat!!!");
break;
default:
Console.WriteLine("Nhap sai!!!");
break;
}
} while (choose != 5);
}
private static void Report()
{
int count = ComputerDAO.Count();
Console.WriteLine("So san pham trong kho: " + count);
}
private static void Search()
{
Console.WriteLine("Nhap san pham can tim kiem: ");
string name = Console.ReadLine();
List<Computer> computerList = ComputerDAO.GetComputerList("%" + name + "%");
foreach (Computer computer in computerList)
{
computer.Display();
}
}
private static void Display()
{
List<Computer> computerList = ComputerDAO.GetComputerList(null);
foreach(Computer computer in computerList)
{
computer.Display();
}
}
private static void Input()
{
string option;
do
{
Console.WriteLine("=== Nhap thong tin may tinh ===");
Computer computer = new Computer();
computer.Input();
ComputerDAO.Insert(computer);
Console.WriteLine("Ban co tiep tuc nhap hay khong Y/n?");
option = Console.ReadLine().ToUpper();
} while (option != "N");
}
static void ShowMenu()
{
Console.WriteLine("1. Nhap san pham");
Console.WriteLine("2. Hien thi thong tin san pham");
Console.WriteLine("3. Tim kiem");
Console.WriteLine("4. Bao cao");
Console.WriteLine("5. Thoat");
Console.WriteLine("Chon: ");
}
}
}
#DAO/Computer.cs
using System;
using MySql.Data.MySqlClient;
namespace BT2493.Models
{
public class Computer
{
public int ID { get; set; }
public string Name { get; set; }
public string DistributeName { get; set; }
public string Producer { get; set; }
public string ProduceYear { get; set; }
public string ImportedDate { get; set; }
public float BuyPrice { get; set; }
public float SellPrice { get; set; }
public Computer()
{
}
public Computer(int id, string name, string distributeName,
string producer, string produceYear, string importedDate,
float buyPrice, float sellPrice)
{
ID = id;
Name = name;
DistributeName = distributeName;
Producer = producer;
ProduceYear = produceYear;
ImportedDate = importedDate;
BuyPrice = buyPrice;
SellPrice = sellPrice;
}
public void Parse(MySqlDataReader reader)
{
Name = reader["name"].ToString();
DistributeName = reader["distribute_name"].ToString();
Producer = reader["producer"].ToString();
ProduceYear = reader["produce_year"].ToString();
ImportedDate = reader["imported_date"].ToString();
BuyPrice = float.Parse(reader["buy_price"].ToString());
SellPrice = float.Parse(reader["sell_price"].ToString());
}
public void Input()
{
Console.WriteLine("Nhap ten san pham: ");
Name = Console.ReadLine();
Console.WriteLine("Nhap nha phan phoi: ");
DistributeName = Console.ReadLine();
Console.WriteLine("Nhap nha san xuat: ");
Producer = Console.ReadLine();
Console.WriteLine("Nhap ngay nhap kho (yyyy-MM-dd HH:ii:ss): ");
ImportedDate = Console.ReadLine();
Console.WriteLine("Nhap nam san xuat: ");
ProduceYear = Console.ReadLine();
Console.WriteLine("Nhap gia mua: ");
BuyPrice = float.Parse(Console.ReadLine());
Console.WriteLine("Nhap gia ban: ");
SellPrice = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("Ten: {0}, nha phan phoi: {1}, nha san xuat: {2}, " +
"ngay nhap kho: {3}, gia mua: {4}, gia ban: {5}", Name, DistributeName,
Producer, ImportedDate, BuyPrice, SellPrice);
}
}
}
#DAO/Config.cs
using System;
namespace BT2493.DAO
{
public class Config
{
static string HOST = "localhost";
static string PORT = "3306";
static string DATABASE = "C2010G";
static string USERNAME = "root";
static string PASSWORD = "";
public static string GetConnectionString()
{
string connString = String.Format("SERVER={0};PORT={1};DATABASE={2};UID={3};PASSWORD={4};SSL Mode=None",
HOST, PORT, DATABASE, USERNAME, PASSWORD);
return connString;
}
}
}
#ComputerDAO.cs
using System;
using BT2493.Models;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
namespace BT2493.DAO
{
public class ComputerDAO
{
public static void Insert(Computer computer)
{
//Open Connection
MySqlConnection conn = new MySqlConnection(Config.GetConnectionString());
conn.Open();
//Query
string sql = "insert into computer(name, distribute_name, producer, produce_year, " +
"imported_date, buy_price, sell_price) values (@name, @distribute_name, @producer, @produce_year, " +
"@imported_date, @buy_price, @sell_price)";
MySqlCommand command = new MySqlCommand(sql, conn);
command.Parameters.AddWithValue("@name", computer.Name);
command.Parameters.AddWithValue("@distribute_name", computer.DistributeName);
command.Parameters.AddWithValue("@producer", computer.Producer);
command.Parameters.AddWithValue("@produce_year", computer.ProduceYear);
command.Parameters.AddWithValue("@imported_date", computer.ImportedDate);
command.Parameters.AddWithValue("@buy_price", computer.BuyPrice);
command.Parameters.AddWithValue("@sell_price", computer.SellPrice);
command.ExecuteNonQuery();
//Close Connection
conn.Close();
}
public static List<Computer> GetComputerList(string name)
{
List<Computer> dataList = new List<Computer>();
//Open Connection
MySqlConnection conn = new MySqlConnection(Config.GetConnectionString());
conn.Open();
//Query
string sql = "select * from computer";
if(name != null)
{
sql += " where name like @name";
}
MySqlCommand command = new MySqlCommand(sql, conn);
if(name != null)
{
command.Parameters.AddWithValue("@name", name);
}
MySqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
Computer computer = new Computer();
computer.Parse(reader);
dataList.Add(computer);
}
//Close Connection
conn.Close();
return dataList;
}
public static int Count()
{
int count = 0;
List<Computer> dataList = new List<Computer>();
//Open Connection
MySqlConnection conn = new MySqlConnection(Config.GetConnectionString());
conn.Open();
//Query
string sql = "select count(*) as count from computer";
MySqlCommand command = new MySqlCommand(sql, conn);
MySqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
count = int.Parse(reader["count"].ToString());
}
//Close Connection
conn.Close();
return count;
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)