Mục Lục
Mục Lục
Giáo Trình Học EBook - Tài Liệu Học Lập Trình C Sharp Kiến Thức Căn Bản - Khai Báo Biến, Toán Tử, Mệnh Đề Điều Kiện, Lặp Video Giới thiệu về ngôn ngữ lập trình C# & .NET Framework + Khai báo biến trong C# Video Hướng dẫn tìm hiểu biến + toán tử trong lập trình C# Video Hướng dẫn tìm hiểu mệnh đề điều kiện if - else - switch trong C# In dãy Fibonacci trong C# - Tìm dãy số Fibonacci - Lập trình C# - Loop trong C# Giải phương trình bậc 2 - Lập Trình C# - Mệnh đề điều kiên if else trong # Viêt chương trình cho FlowChart - Lập trình C# - Tìm hiểu if else trong C# Hiển thị hình tam giác - Lập trình C# - Loop trong C# Viết chương trình ax + b = 0 bằng C#, C Sharp Xây dựng dự án Hello World trong C# - C Sharp Nhập và hiển thị thông tin sinh viên - Console trong C# Tìm dãy số nguyên tố - Lập trình C# - Loop trong C# Video Tìm dãy số nguyên tố - Lập trình C# - Loop trong C# In dãy số chẵn & lẻ từ mảng số nguyên - Lập trình C# - Loop trong C# Video In dãy số chẵn & lẻ từ mảng số nguyên - Lập trình C# - Loop trong C# Array trong C Sharp Video Array C# - Hướng dẫn tìm hiểu Array trong C#. C Sharp, Lập Trình C#, Lập Trình C Sharp Tính tổng số chắc & Tổng số lẻ trong chuỗi - Lập Trình C# - Array Trong C# OOP - Tính Chất Đóng Gói & Kế Thừa Video Học lập trình OOP C# - Tính chất đóng gói C# - Qua ví dụ quản lý sinh viên - Lập trình C# Bài 1: Chương trình quản lý sản phẩm - Lập Trình C# - Lập Trình C Sharp - Làm quen OOP Bài 2: Chương trình quản lý sản phẩm - Product - Lập Trình C# - Lập Trình C Sharp Chương trình quản lý công nhân - Lập Trình C# - Lập Trình C Sharp - Làm quen OOP Chương trình quản lý sách - Develop Book Project - Lập Trình C# - OOP trong C# - C Sharp Video Chương trình quản lý sách - Develop Book Project - Lập Trình C# - OOP trong C# - C Sharp - C2010G OOP - Tính Chất Đa Hình & Trừu Tượng & Interface Ôn tập OOP - Table - Trong C# - Lập Trình C# - Lập Trình C Sharp Chường trình quản lý sở thú - Lập trình C# - Lập trình C Sharp Bài tập ôn luyện OOP + Interface trong C# - Lập Trình C# - Lập Trình C Sharp Kiểm tra 30 phút - Chương trình quản lý thiết bị phòng lab cho trường học - Lập trình C# - Khoá học lập trình C Sharp Chương trình quản lý khách sạn - Develop Hotel Project - Lập Trình C# - Lập Trình C Sharp Video Chương trình quản lý khách sạn - Develop Hotel Project - Lập Trình C# - Lập Trình C Sharp Namespace & Delegate & Event Viết chương trình quản lý sinh viên + delegate & event trong C# - Lập Trình C# - Lập Trình C Sharp Examination & Test - Lập trình C# - Lập trình C Sharp Bài tập - Xây dựng chương trình quản lý sức khoẻ - Lập trình C# Viết chương trình quản lý tiêm chủng Vacxin COVID-19 - Lập trình C Sharp nâng cao. JSON & Extension - Partial - Type Anonymous Chương trình quản lý sinh viên C# + Import/Export JSON + File - Lập Trình C# - Lập Trình C Sharp Video Chương trình quản lý sinh viên C# + Import/Export JSON + File - Lập Trình C# - Lập Trình C Sharp Chương trình nhập xuất dữ liệu điểm thi - Lập Trình C# - Lập Trình C Sharp - Làm quen OOP Bài tập C Sharp - Chương trình quản lý cán bộ C# - C Sharp Kết Nối CSDL - C Sharp Video Quản lý sinh viên C# - Kết nối CSDL C# - Kết nối database C# Chương trình quản lý sách C# - Book project C# - CSDL Tools Import/Export JSON into Database C# - Nhập xuất JSON File vào CSDL C# - Quản lý sinh viên C# Bài tập - 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 Ôn Tập Tổng Quát Bài tập - Chương trình quản lý Mèo C# (C Sharp) | Chương trình quản lý Cat C# (C Sharp) Bài tập - Quản lý điểm thi Aptech bằng C# - Lập trình C Sharp [Examination] C# - Chương trình quản lý đội thi TechWiz toàn cầu Aptech Ấn Độ - C# Bài Tập - Quản lý nhà - Lập trình C# | C Sharp Bài tập - Quản lý rạp chiều phim C# - Lập trình C# Viết chương trình quản lý xe car bằng C # - C Sharp | Khoá học lập trình C# [Assignment] Bài tập ôn luyên tổng quát C# - Lập Trình C# - Lập Trình C Sharp 1000 Bài tập lập trình C# - Lập trình C# - Lập Trình C Sharp Ôn tập OOP - Table - Trong C# - Lập Trình C# - Lập Trình C Sharp
C Sharp Tutorial

[Video] Quản lý sinh viên C# - Kết nối CSDL C# - Kết nối database C#



Nội dung kiến thức:
	Kết nối CSDL MySQL <-> C# (C Sharp)

Mini Project:
	Viết 1 chương trình quản lý sinh
	Menu chương trình:
		1. Xem danh sách sinh viên
		2. Thêm sinh viên vào CSDL
		3. Sửa thông tin sinh viên
		4. Xoá
		5. Thoát


B1. Chuẩn bị database + tables
B2. Tạo dự án + add thư viện hỗ trợ kết CSDL MySQL <-> C#
B3. Mapping Models <-> Tables (CSDL)





#Models/Students.cs


using System;
using Lesson08.Utils;

namespace Lesson08.Models
{
    public class Students
    {
        public int ID { get; set; }
        public string Fullname { get; set; }
        public string Gender { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public int Age { get; set; }

        public Students()
        {
        }

        public Students(int id, string fullname, string gender, string email, string phoneNumber, int age)
        {
            ID = id;
            Fullname = fullname;
            Gender = gender;
            Email = email;
            PhoneNumber = phoneNumber;
            Age = age;
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten: ");
            Fullname = Console.ReadLine();

            Console.WriteLine("Nhap gioi tinh: ");
            Gender = Console.ReadLine();

            Console.WriteLine("Nhap email: ");
            Email = Console.ReadLine();

            Console.WriteLine("Nhap SDT: ");
            PhoneNumber = Console.ReadLine();

            Console.WriteLine("Nhap tuoi: ");
            Age = Utility.ReadInt();
        }

        public void Display()
        {
            Console.WriteLine("Ten: {0}, gioi tinh: {1}, email: {2}, sdt: {3}, " +
                " tuoi: {4}", Fullname, Gender, Email, PhoneNumber, Age);
        }
    }
}


#Utils/Utility.cs


using System;
namespace Lesson08.Utils
{
    public class Utility
    {
        public static int ReadInt()
        {
            int value;
            while(true)
            {
                try
                {
                    value = int.Parse(Console.ReadLine());
                    return value;
                } catch(Exception e)
                {
                    Console.WriteLine("Nhap lai!!!");
                }
            }
        }
    }
}


#DataBase/StudentDAO.cs


using System;
using System.Collections.Generic;
using Lesson08.Models;
using MySql.Data.MySqlClient;

namespace Lesson08.DataBase
{
    public class StudentDAO
    {
        public static List<Students> GetStudentList()
        {
            List<Students> dataList = new List<Students>();

            //Open connection
            MySqlConnection conn = new MySqlConnection(Config.getConnectionString());
            conn.Open();

            //Query
            string sql = "select * from students";
            MySqlCommand command = new MySqlCommand(sql, conn);
            MySqlDataReader reader = command.ExecuteReader();

            while(reader.Read())
            {
                int id = int.Parse(reader["id"].ToString());
                string fullname = reader["fullname"].ToString();
                string gender = reader["gender"].ToString();
                int age = int.Parse(reader["age"].ToString());
                string email = reader["email"].ToString();
                string phoneNumber = reader["phone_number"].ToString();

                Students std = new Students(id, fullname, gender, email, phoneNumber, age);
                dataList.Add(std);
            }

            //Close connection
            conn.Close();

            return dataList;
        }

        public static Students FindByID(int id)
        {
            Students std = null;

            //Open connection
            MySqlConnection conn = new MySqlConnection(Config.getConnectionString());
            conn.Open();

            //Query
            string sql = "select * from students where id = @id";
            MySqlCommand command = new MySqlCommand(sql, conn);
            command.Parameters.AddWithValue("@id", id);

            MySqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                string fullname = reader["fullname"].ToString();
                string gender = reader["gender"].ToString();
                int age = int.Parse(reader["age"].ToString());
                string email = reader["email"].ToString();
                string phoneNumber = reader["phone_number"].ToString();

                std = new Students(id, fullname, gender, email, phoneNumber, age);
            }

            //Close connection
            conn.Close();

            return std;
        }

        public static void Insert(Students std)
        {
            //Open connection
            MySqlConnection conn = new MySqlConnection(Config.getConnectionString());
            conn.Open();

            //Query
            string sql = "insert into students(fullname, email, age, gender, phone_number) " +
                "values(@fullname, @email, @age, @gender, @phone_number)";
            MySqlCommand command = new MySqlCommand(sql, conn);
            command.Parameters.AddWithValue("@fullname", std.Fullname);
            command.Parameters.AddWithValue("@email", std.Email);
            command.Parameters.AddWithValue("@age", std.Age);
            command.Parameters.AddWithValue("@gender", std.Gender);
            command.Parameters.AddWithValue("@phone_number", std.PhoneNumber);

            command.ExecuteNonQuery();
            //Close connection
            conn.Close();
        }

        public static void Update(Students std)
        {
            //Open connection
            MySqlConnection conn = new MySqlConnection(Config.getConnectionString());
            conn.Open();

            //Query
            string sql = "update students set fullname = @fullname, email = @email, age = @age, gender = @gender, phone_number = @phone_number where id = @id";
            MySqlCommand command = new MySqlCommand(sql, conn);
            command.Parameters.AddWithValue("@fullname", std.Fullname);
            command.Parameters.AddWithValue("@email", std.Email);
            command.Parameters.AddWithValue("@age", std.Age);
            command.Parameters.AddWithValue("@gender", std.Gender);
            command.Parameters.AddWithValue("@phone_number", std.PhoneNumber);
            command.Parameters.AddWithValue("@id", std.ID);

            command.ExecuteNonQuery();
            //Close connection
            conn.Close();
        }

        public static void Delete(int id)
        {
            //Open connection
            MySqlConnection conn = new MySqlConnection(Config.getConnectionString());
            conn.Open();

            //Query
            string sql = "delete from students where id = @id";
            MySqlCommand command = new MySqlCommand(sql, conn);
            command.Parameters.AddWithValue("@id", id);

            command.ExecuteNonQuery();
            //Close connection
            conn.Close();
        }
    }
}


#DataBase/Config.cs


using System;
namespace Lesson08.DataBase
{
    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;
        }
    }
}


#Program.cs


using System;
using System.Collections.Generic;
using Lesson08.Utils;
using Lesson08.DataBase;
using Lesson08.Models;

namespace Lesson08
{
    class Program
    {
        static void Main(string[] args)
        {
            int choose;

            do
            {
                ShowMenu();
                choose = Utility.ReadInt();

                switch(choose)
                {
                    case 1:
                        Display();
                        break;
                    case 2:
                        Input();
                        break;
                    case 3:
                        Update();
                        break;
                    case 4:
                        Delete();
                        break;
                    case 5:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 5);
        }

        private static void Delete()
        {
            Console.WriteLine("Nhap ID can sua thong tin: ");
            int id = Utility.ReadInt();

            Students stdFind = StudentDAO.FindByID(id);

            if (stdFind != null)
            {
                StudentDAO.Delete(id);
            }
            else
            {
                Console.WriteLine("Khong tim thay sinh vien phu hop");
            }
        }

        private static void Update()
        {
            Console.WriteLine("Nhap ID can sua thong tin: ");
            int id = Utility.ReadInt();

            Students stdFind = StudentDAO.FindByID(id);

            if(stdFind != null)
            {
                stdFind.Input();
                StudentDAO.Update(stdFind);
            } else
            {
                Console.WriteLine("Khong tim thay sinh vien phu hop");
            }
        }

        private static void Input()
        {
            Console.WriteLine("Nhap thong tin sinh vien: ");
            Students std = new Students();
            std.Input();

            StudentDAO.Insert(std);

            Console.WriteLine("Them thanh cong!!!");
        }

        private static void Display()
        {
            List<Students> studentList = StudentDAO.GetStudentList();

            Console.WriteLine("====== Danh sach sinh vien");
            foreach(Students std in studentList)
            {
                std.Display();
            }
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Hien thi danh sach sinh vien");
            Console.WriteLine("2. Them sinh vien");
            Console.WriteLine("3. Sua thong tin sinh vien");
            Console.WriteLine("4. Xoa thong tin sinh vien");
            Console.WriteLine("5. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}




Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó

×