Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

THỰC HÀNH WEB

THỰC HÀNH SỐ 05

Kiến thức đạt được


- Kết nối CSDL bằng MS SQL Server
- Truy vấn dữ liệu: hiển thị dữ liệu lên trang, thêm, xóa, sửa, xem thông tin chi tiết

Bài 1: Kết nối dữ liệu


Sinh viên tiếp tục sử dụng Project các bài Lab trước đó hoặc tạo Project mới tùy ý.
Sinh viên có 2 cách tạo CSDL: cả 2 cách này yêu cầu máy tính bạn phải cài đặt
SQL trước đó.
- Tạo từ phần mềm MS SQL
- Tạo từ Visual Studio
 Tên database được đặt trong bài thực hành: DBQLBanHang
Cơ sở dữ liệu sử dụng trong bài thực hành:
==
create table tblUser(
userID int identity(1,1) primary key,
userName nvarchar(100),
userPass nvarchar(max),
userRole nvarchar(50)
)
--
create table tblSupplierProduct(
idSup int identity (1,1) primary key,
supplierName nvarchar(150)
)
--
create table tblStaff(
IDStaff int identity(1,1) primary key,
StaffName nvarchar(150)
)

--
create table tblStyle(
ID int identity(1,1) primary key,
nameStyle nvarchar(150)
)
--
THỰC HÀNH WEB

create table tblCustumer(


ID int identity(1,1) ,
codeCus char(10) primary key,
nameCus nvarchar(150),
addressCus nvarchar(150),
phoneCus char(11),
emailCus nvarchar(150)
)

--
--
create table tblProduct(
ID int identity(1,1),
codeProduct char(20) primary key,
nameProduct nvarchar(100),
descriptionProduct nvarchar(max),
idSup int,
quantityProductInput int,
priceProductInput float,
colorPro varchar(50),
sizePro varchar(10),
material nvarchar(50),
genderUse nvarchar(5),
IDStyle int,
imageProduct1 nvarchar(max),
imageProduct2 nvarchar(max),
imageProduct3 nvarchar(max),
foreign key (IDSup) references tblSupplierProduct(IDSup),
foreign key(IDStyle) references tblStyle(ID),
)
--

create table tblBill(


IDBill int identity(1,1) primary key,
BillDate date,
IDStaff int,
UserID int,
TotalPay float,
codeCus char(10),
foreign key (IDStaff) references tblStaff(IDStaff),
foreign key (codeCus) references tblCustumer(codeCus)
THỰC HÀNH WEB

create table tblDetailBill(


ID int identity(1,1),
IDBill int,
CodeProduct char(20),
QuantityProduct int,
PriceProductBuying float,
TotalMoney float,
primary key(IDBill, CodeProduct),
foreign key (IDBill) references tblBill(IDBill),
foreign key (CodeProduct) references tblProduct(CodeProduct)
)==
 Bước 1: Tạo CSDL bằng Visual Studio
 Mở cửa sổ SQL Server Object Explorer

 Trên Visual Studio > menu View > chọn SQL Server Object Explorerr
 Kết nối với SQL:

 Chọn tên Server Name mà máy bạn đã cài đặt


THỰC HÀNH WEB

 Kết quả sau khi connect:

 Nhấp phải chuột vào thư mục Databases > Add New Database

 Đặt tên là: DBQLBanHang


THỰC HÀNH WEB

 Nhấp chuột phải vào tên database DBQLBanHang -> New Query ->
Copy paste các lệnh tạo table và chạy từng bảng.

 Bước 2: Cài đặt Entity Framework


THỰC HÀNH WEB

 Bước 3: Kết nối database vào Model

Nhấp phải chuột vào thư mục Model -> Add -> New Item
THỰC HÀNH WEB
THỰC HÀNH WEB
THỰC HÀNH WEB

Server name

Chọn: Database

 Bạn muốn biết tên datasese dùng để truy vấn các lớp trong Model thì có 2
cách:
Cách 1:

Cách 2:
THỰC HÀNH WEB

 Bước 4: Tạo mới Controller

 Nhấp phải chuột vào thư mục Controller -> New -> Controller
 Đặt tên là StaffController.cs

 Kết nối với thư mục Models trong StaffController


DemoLTWeb là tên của
Project được đặt trogn Lab
này
THỰC HÀNH WEB

Khởi tạo kết nối

 Chức năng hiển thị danh sách nhân viên:

 Add Index() -> View


THỰC HÀNH WEB

 Chức năng Create: thêm mới

Hàm Create này


có vai trò Add >
View vạo giao
diện Create

Hàm Create này


có vai trò xử lý
lưu dòng dữ liệu
mới vào bảng

 Add Create () -> View


THỰC HÀNH WEB

Xóa đoạn IDStaff đi, vì trong vì dụ


này IDStaff có thuộc tính là
identity, tự động tăng, không cần
nhập

 Chức năng Details: xem thông tin chi tiết

 Add Details () -> View


THỰC HÀNH WEB

 Chức năng Edit: cập nhật dòng dữ liệu

Chú ý: chỉ cập nhật các thuộc tính không phải là ID

 Add Edit () -> View


THỰC HÀNH WEB

 Chức năng Deletes: xóa dòng dữ liệu

 Add Delete () -> View


THỰC HÀNH WEB

You might also like