News Ticker

Menu

Giải Bài Kiểm Tra .NET

 Link Download Project: Click Download

Câu 1: Lập trình Console



Class Book:
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;
        }

    }
}

Class Library:
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;
        }
    }
}

Class Main:
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();
        }
    }
}
Câu 2: Lập trình winform, mô hình 3 lớp


Class DataBase (Lớp thao tác CSDL ):
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;
        }
    }
}


Class DocGia (Lớp xử lý nghiệp vụ ):
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;
        }
    }
}

Class fQuanLyDocGia(Lớp xử lý giao diện ):
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!");
        }
    }
}

Code SQL:
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

Share This:

Post Tags:

Welcome To Task Marks

I'm Task Marks. Tôi là chủ trang web này, trang này tôi dùng để chia sẽ tài liệu và những thứ linh tinh khác. Cảm ơn mọi người đã ghé thăm trang web của chúng tôi.Nếu có thắt mắt xin vui lòng liên hệ
Mail: devnguhoc@gmail.com

No Comment to " Giải Bài Kiểm Tra .NET "

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM