Tuần 2 - Phần 2 - Câu 2
Thứ Bảy, 4 tháng 1, 2020
/
No Comments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cau1
{
class Book
{
private string m_Title;
private string m_Code;
private int m_Page;
public string Title { get => m_Title; set => m_Title = value; }
public string Code { get => m_Code; set => m_Code = value; }
public int Page { get => m_Page; set => m_Page = value; }
public Book()
{
this.Title = "";
this.Page = 0;
this.Code = "";
}
public Book(string code)
{
this.Code = code;
}
public Book(string title, string code)
{
this.Title = title;
this.Code = code;
}
public Book(string title, string code, int page)
{
this.Title = title;
this.Code = code;
this.Page = page;
}
public Book(Book bk)
{
this.Title = bk.Title;
this.Code = bk.Code;
this.Page = bk.Page;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cau1
{
class Library
{
ArrayList m_BookShelf;
public Library()
{
m_BookShelf = new ArrayList();
}
public void AddBook(Book newBook)
{
m_BookShelf.Add(newBook);
}
public Book RemoveBook(string code)
{
int i = 0;
foreach (Book item in m_BookShelf)
{
if (item.Code == code)
m_BookShelf.RemoveAt(i);
i++;
}
return null;
}
public Book FindBook(string title)
{
foreach (Book item in m_BookShelf)
{
if (item.Title == title)
return item;
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cau1
{
class Program
{
static void Main(string[] args)
{
int lua_chon = 0;
Library lb = new Library();
do
{
Console.WriteLine("===== MENU =====");
Console.WriteLine("1. Nhap Sach");
Console.WriteLine("2. Tim Sach");
Console.WriteLine("3. Xoa Sach");
Console.WriteLine("0. Exit");
Console.Write("Ban muon chon?: ");
lua_chon = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
switch (lua_chon)
{
case 1:
Console.WriteLine("----- THEM SACH -----");
Console.Write("Nhap so luong sach: ");
int sl = int.Parse(Console.ReadLine());
for (int i = 0; i < sl; i++)
{
Book bk = new Book();
Console.Write("Nhap ten sach: ");
bk.Title = Console.ReadLine();
Console.Write("Nhap ma sach: ");
bk.Code = Console.ReadLine();
Console.Write("Nhap trang sach: ");
bk.Page = int.Parse(Console.ReadLine());
lb.AddBook(bk);
}
break;
case 2:
Console.WriteLine("----- TIM SACH -----");
Console.Write("Nhap ten sach can tim: ");
string ten = Console.ReadLine();
Book bk1 = new Book();
bk1 = lb.FindBook(ten);
if (bk1 != null)
{
Console.WriteLine("Da tim thay ["+ten+"]");
Console.WriteLine("Ten sach: "+bk1.Title);
Console.WriteLine("Ma sach: " + bk1.Code);
Console.WriteLine("Page sach: " + bk1.Page);
}
else
Console.WriteLine("Khong tim thay [" + ten + "] trong thu vien!");
break;
case 3:
Console.WriteLine("----- XOA SACH -----");
Console.Write("Nhap ma sach can xoa: ");
string ma_sach = Console.ReadLine();
//Book temp = new Book();
lb.RemoveBook(ma_sach);
break;
case 0:
Console.WriteLine("Ban da thoat chuong trinh!");
break;
default:
Console.WriteLine("Ban da chon sai! Hay chon lai!\n\n");
break;
}
} while (lua_chon != 0);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Cau2
{
public class Database
{
SqlConnection Connection;
SqlCommand cmd;
public Database()
{
string strConnect = "";
Connection = new SqlConnection(strConnect);
}
public DataTable ExecuteQuery(string query)
{
Connection.Open();
cmd = new SqlCommand(query, Connection);
DataTable data = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(data);
Connection.Close();
return data;
}
public int ExecuteNonQuery(string query)
{
int data = 0;
Connection.Open();
SqlCommand command = new SqlCommand(query, Connection);
data = command.ExecuteNonQuery();
Connection.Close();
return data;
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace Cau2
{
public class DocGia
{
public DataTable LoadData()
{
string query = "select *from DocGia";
DataTable result = new Database().ExecuteQuery(query);
return result;
}
public bool ThemDocGia(string ten, DateTime ngaysinh, string dia_chi, string email, DateTime ngay_lap, DateTime ngay_het_han, float tien_no)
{
string query = "insert into DocGia values(N'"+ten+"', '"+ngaysinh+"', N'"+dia_chi+"', '"+email+"', '"+ngay_lap+"', '"+ngay_het_han+"', "+tien_no+")";
int result = new Database().ExecuteNonQuery(query);
return result > 0;
}
public bool XoaDocGia(int id)
{
string query = "delete from DocGia where MaDocGia = '"+id+"'";
int result = new Database().ExecuteNonQuery(query);
return result > 0;
}
public bool SuaDocGia(string id, string ten, DateTime ngaysinh, string dia_chi, string email, DateTime ngay_lap, DateTime ngay_het_han, float tien_no)
{
string query = "Update DocGia set HoTenDocGia = N'"+ten+"', NgaySinh = '"+ngaysinh+"', DiaChi = N'"+dia_chi+"', Email = '"+email+ "', NgayLapThe = '"+ngay_lap+ "', NgayHetHan = '"+ngay_het_han+ "', TienNo = '"+tien_no+"' where MaDocGia = '" + id+"'";
int result = new Database().ExecuteNonQuery(query);
return result > 0;
}
}
}
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 Cau2
{
public partial class fQuanLyDocGia : Form
{
public fQuanLyDocGia()
{
InitializeComponent();
}
private void fQuanLyDocGia_Load(object sender, EventArgs e)
{
HienThi();
}
void HienThi()
{
lsvViewDocGia.Items.Clear();
DataTable data = new DocGia().LoadData();
int i = 0;
foreach (DataRow row in data.Rows)
{
lsvViewDocGia.Items.Add(row["MaDocGia"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["HoTenDocGia"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["NgaySinh"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["DiaChi"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["Email"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["NgayLapThe"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["NgayHetHan"].ToString());
lsvViewDocGia.Items[i].SubItems.Add(row["TienNo"].ToString());
i++;
}
}
private void btnView_Click(object sender, EventArgs e)
{
HienThi();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (new DocGia().ThemDocGia(txbTen.Text, dtpkNgaySinh.Value, txbAdress.Text, txbEmail.Text, dtpkNgayLap.Value, dtpkHetHan.Value, float.Parse(txbTienNo.Text)))
{
MessageBox.Show("Thêm thành công!");
HienThi();
}
else
MessageBox.Show("Thêm thất bại!");
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (new DocGia().XoaDocGia(int.Parse(txbID.Text)))
{
MessageBox.Show("Xóa thành công!");
HienThi();
}
else
MessageBox.Show("Xóa thất bại!");
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (new DocGia().SuaDocGia(txbID.Text,txbTen.Text, dtpkNgaySinh.Value, txbAdress.Text, txbEmail.Text, dtpkNgayLap.Value, dtpkHetHan.Value, float.Parse(txbTienNo.Text)))
{
MessageBox.Show("Sửa thành công!");
HienThi();
}
else
MessageBox.Show("Sửa thất bại!");
}
}
}
CREATE DATABASE qltv_db GO USE qltv_db GO CREATE TABLE DOCGia ( MaDocGia INT IDENTITY(1,1) PRIMARY KEY, HoTenDocGia NVARCHAR(30) NULL, NgaySinh DATE NULL, DiaChi NVARCHAR(30) NULL, Email CHAR(30) NULL, NgayLapThe DATE NULL, NgayHetHan DATE NULL, TienNo FLOAT NULL ) GO