Tải bản đầy đủ (.doc) (8 trang)

Tài liệu Bài thực hành số 4: ADO pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (222.41 KB, 8 trang )

Bài thực hành số 4: ADO
1. Tạo CSDL HOSO.MDB và thiết kế bảng SINHVIEN có cấu trúc như sau:
2. Thiết kế trang DEFAULT.
ASP cho phép hiển thị danh
sách SV như sau:
3. Nhấn thêm cho phép thêm một SV mới(Hình trái)
4. Nhấn nút SỬA cho phép sửa thông tin của SV đã chọn (Hình phải)
GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên
1
Bài thực hành số 4: ADO
5. Nhấn nút XOÁ cho phép xoá những SV đã được đánh dấu
Yêu cầu SV nắm vững các kiến thức sau:
- Sử dụng và truy xuất các điều khiển trên FORM
- Phần lập trình ADO và truy vấn dữ liệu bằng Recordset
GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên
2
Bài thực hành số 4: ADO
HƯỚNG DẪN CHI TIẾT
Mô hình liên kết các trang
Connection.inc
<%
StrConn="Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & server.mappath("Hoso.mdb")
Set conn=Server.CreateObject("ADODB.Connection")
Conn.open StrConn
Set RS=server.CreateObject("ADODB.Recordset")
%>
Default.asp
<!--#include file=connection.inc-->
<%
StrSQL="Select * from Sinhvien order by Hoten"
rs.open strSQL,Conn,1,3


%>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>
<body>
<p><b>DANH SACH SINH VIEN</b></p>
<form method="POST" action="Xuly_Default.asp">
<table border="1" width="105%">
<tr>
<td width="6%" bgcolor="#C0C0C0">XOA</td>
<td width="5%" align="center" bgcolor="#C0C0C0"><b>SUA</b></td>
<td width="7%" align="center" bgcolor="#C0C0C0"><b>MA SV</b></td>
<td width="20%" align="center" bgcolor="#C0C0C0"><b>HO VA TEN</b></td>
<td width="7%" align="center" bgcolor="#C0C0C0"><b>GIOI TINH</b></td>
<td width="9%" align="center" bgcolor="#C0C0C0"><b>NGAY SINH</b></td>
<td width="20%" align="center" bgcolor="#C0C0C0"><b>SO THICH</b></td>
<td width="26%" align="center" bgcolor="#C0C0C0"><b>DIA CHI</b></td>
</tr>
<%
while not rs.eof
%>
<tr>
<td width="6%" align="center"><input type="checkbox" name="chkXoa" value=<%=rs.fields("MASV")
%>></td>
<td width="5%" align="center"><input type="radio" value=<%=rs.fields("MASV")%> checked

name="RdoSua"></td>
<td width="7%" align="center"><%=rs.fields("MASV")%></td>
<td width="20%"><%=rs.fields("HOTEN")%></td>
GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên
3
Default.asp
Xuly_Default.asp
Them.asp Sua.asp Xoa.asp
Xuly_Them.asp Xuly_Sua.asp
Bài thực hành số 4: ADO
<td width="7%" align="center">
<%
if rs.fields("Gioitinh") then
response.write "Nam"
else
response.write "Nu"
End if
%>
</td>
<td width="9%" align="center"><%=rs.fields("Ngaysinh")%></td>
<td width="20%"><%=rs.fields("Sothich")%></td>
<td width="26%"><%=rs.fields("Diachi")%></td>
</tr>
<%
rs.movenext
wend
rs.close
%>
</table>
<p><input type="submit" value=" XOA " name="cmdXoa">

<input type="submit" value=" SUA " name="cmdSua">
<input type="submit" value=" THEM " name="cmdThem"></p>
</form>
</body>
</html>
Xuly_Default.asp
<%
'Neu nhan nut Them
if request.form("cmdThem")<>"" then
%>
<!--#include file=Them.asp-->
<%
end if
'Neu nhan nut Xoa
if request.form("cmdXoa")<>"" then
%>
<!--#include file=Xoa.asp-->
<%
end if
'Neu nhan nut Sua
if request.form("cmdSua")<>"" and request.form("rdoSua")<>"" then
Session("MASV")=request.form("rdoSua") ' Tao bien kieu Session luu MaSV su dung trong file Sua.asp
%>
<!--#include file=Sua.asp-->
<%
end if
%>
Them.asp
<html>
<head><title>Nhập sinh viên</title></head>

<body>
<form method="POST" action="XULY_THEM.ASP">
<p><b>NHAP THONG TIN CA NHAN</b></p>
<table border="1" width="61%" height="240" bgcolor="#C0C0C0">
<tr><td width="47%" height="23"><b>Ma SV</b></td>
<td width="69%" height="23"><input type="text" name="txtMaSV" size="44"></td></tr>
<tr><td width="47%" height="23">
<p><b>Ho va ten</b></td>
<td width="69%" height="23"><input type="text" name="txtHoten" size="44"></td></tr>
<tr><td width="47%" height="19">
<p><b>Gioi tinh</b></td>
GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên
4
Bài thực hành số 4: ADO
<td width="69%" height="19">
<input type="radio" value="Nam" name="rdoGT" checked>Nam
<input type="radio" value="Nu" name="rdoGT">Nu </td></tr>
<tr><td width="47%" height="23"><b>Ngay sinh</b></td>
<td width="69%" height="23">
Ngay <select size="1" name="OptNgay">
<%FOR i=1 TO 31%>
<option <%if i=day(date()) then%> selected <%End if%> value=<%=i%>><%=i%></option>
<%Next%>
</select>
thang <select size="1" name="OptThang">
<%FOR i=1 TO 12%>
<option <%if i=month(date()) then%> selected <%End if%> value=<%=i%>><%=i%></option>
<%Next%>
</select>
nam <select size="1" name="Optnam">

<%FOR i=1970 TO year(date())%>
<option selected value=<%=i%>><%=i%></option>
<%Next%>
</select></td> </tr>
<tr> <td width="47%" height="57">
<b>So thich</b></td><td width="69%" height="57">
<input type="checkbox" name="chkSothich" value="Du lich">Du lich
<input type="checkbox" name="chkSothich" value="Ca nhac">Ca nhac
<input type="checkbox" name="chkSothich" value="The thao">The thao
<input type="checkbox" name="chkSothich" value="Nau an"> Nau an
<input type="checkbox" name="chkSothich" value="Xem TV">Xem TV
<input type="checkbox" name="chkSothich" value="Ve"> Ve
<input type="checkbox" name="chkSothich" value="Mua sam">Mua sam
<input type="checkbox" name="chkSothich" value="Tin hoc">Tin hoc
</td> </tr>
<tr>
<td width="47%" height="88"><b>Dia chi</b></td>
<td width="69%" height="88">
<textarea rows="6" name="txtaDiachi" cols="32"></textarea></td>
</tr>
</table>
<input type="submit" value=" Ghi " name="cmdNhap">
<input type="reset" value="Nhap lai" name="cmdnhaplai">
</form>
</body>
</html>
Xuly_Them.asp
<!--#include file=connection.inc-->
<%
Private Function TontaiSV() 'Ham kiem tra MaSV da ton tai chua

TontaiSV=FALSE
SQL="Select * from Sinhvien where MaSV='" & ucase(trim(request.form("txtMaSV"))) & "'"
set rs1=server.CreateObject("ADODB.Recordset")
rs1.open SQL,Conn,1,3
if rs1.RecordCount > 0 then ' .RecordCount tra ve tong so ban ghi trong Recordset
TontaiSV=TRUE
end if
rs1.Close
End Function
If trim(request.form("txtMaSV"))="" or TontaiSV then 'Neu ton tai MASV thi bao loi
Response.Write "Chua nhap MA SV hoac "
Response.Write "MA SV: " & ucase(trim(request.form("txtMaSV"))) & " da ton tai trong CSDL <BR><a
href=Them.asp>Quay lai</a>"
else 'Neu ko ton tai MASV thi them moi
StrSQL="Select * from Sinhvien"
rs.open strSQL,Conn,1,3
GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên
5

×