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

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

Đang tải... (xem toàn văn)

Thông tin tài liệu

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 Bài thực hành số 4: ADO 'ghi thong tin tu form vao CSDL ngaysinh=Cdate(request.form("optNgay") & "/" & request.form("optThang") & "/" & request.form("OptNam")) rs.addnew rs.fields("maSV")=Ucase(trim(request.form("txtMaSV"))) 'Ham Ucase doi chu thuong thanh chu HOA rs.fields("hoten")=request.form("txthoten") if request.form("rdoGT")="Nam" then rs.fields("Gioitinh")=TRUE else rs.fields("Gioitinh")=FALSE end if rs.fields("Ngaysinh")=Ngaysinh rs.fields("Sothich")= request.form("chksothich") rs.fields("Diachi")=request.form("txtaDiachi") rs.update rs.close response.redirect("Default.asp") End if %> Sua.asp <!--#include file=connection.inc--> <% StrSQL="Select * from Sinhvien where MaSV='" & Session("MaSV") & "'" rs.open strSQl,Conn,1,3 %> <html> <head><title>Ho va ten</title></head> <body> <form method="POST" action="XULY_SUA.ASP"> <b>SUA THONG TIN CA NHAN</b> <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"><%=rs.fields("MaSV")%></td> </tr> <tr> <td width="47%" height="23"><b>Ho va ten</b></td> <td width="69%" height="23"> <input type="text" name="txtHoten" size="44" value=<%=" ' " & rs.fields("Hoten") & " ' "%>> </td> </tr> <tr> <td width="47%" height="19"><b>Gioi tinh</b></td> <td width="69%" height="19"> <input type="radio" value="Nam" name="rdoGT" <%if rs.fields("gioitinh") then%> checked <%end if %>>Nam <input type="radio" value="Nu" name="rdoGT" <%if not rs.fields("gioitinh") then%> checked <%end if %>>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(rs.fields("Ngaysinh")) 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(rs.fields("Ngaysinh")) then%> selected <%End if%> value=<%=i%>><%=i %></option> <%Next%> GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên 6 Bài thực hành số 4: ADO </select> nam<select size="1" name="Optnam"> <%FOR i=1970 TO year(date())%> <option <%if i=year(rs.fields("Ngaysinh")) then%> selected <%End if%> 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" <%if Instr(Ucase(rs.fields("sothich")),"DU LICH")>0 then%> checked <%end if%>>Du lich <input type="checkbox" name="chkSothich" value="Ca nhac" <%if Instr(Ucase(rs.fields("sothich")),"CA NHAC")>0 then%> checked <%end if%>>Ca nhac <input type="checkbox" name="chkSothich" value="The thao" <%if Instr(Ucase(rs.fields("sothich")), "THE THAO")>0 then%> checked <%end if%>>The thao <input type="checkbox" name="chkSothich" value="Nau an" <%if Instr(Ucase(rs.fields("sothich")),"NAU AN")>0 then%> checked <%end if%>> Nau an <input type="checkbox" name="chkSothich" value="Xem TV" <%if Instr(Ucase(rs.fields("sothich")),"XEM TV")>0 then%> checked <%end if%>>Xem TV <input type="checkbox" name="chkSothich" value="Ve" <%if Instr(Ucase(rs.fields("sothich")),"VE")>0 then %> checked <%end if%>> Ve <input type="checkbox" name="chkSothich" value="Mua sam" <%if Instr(Ucase(rs.fields("sothich")),"MUA SAM")>0 then%> checked <%end if%>>Mua sam <input type="checkbox" name="chkSothich" value="Tin hoc" <%if Instr(Ucase(rs.fields("sothich")),"TIN HOC")>0 then%> checked <%end if%>>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"><%=rs.fields("Diachi")%></textarea></td> </tr> </table> <input type="submit" value=" Ghi " name="cmdGhi"></p> </form> </body> </html> Xuly_Sua.asp <!--#include file=connection.inc--> <% StrSQL="Select * from Sinhvien where MaSV='" & Session("MaSV") & "'" rs.open strSQL,Conn,1,3 ngaysinh=Cdate(request.form("optNgay") & "/" & request.form("optThang") & "/" & request.form("OptNam")) 'ghi thong tin tu form vao CSDL rs.fields("hoten")=request.form("txthoten") if request.form("rdoGT")="Nam" then rs.fields("Gioitinh")=TRUE else rs.fields("Gioitinh")=FALSE end if rs.fields("Ngaysinh")=Ngaysinh rs.fields("Sothich")= request.form("chksothich") rs.fields("Diachi")=request.form("txtaDiachi") rs.update rs.close response.redirect("Default.asp") %> Xoa.asp <!--#include file=connection.inc--> <% For i=1 to request.form("ChkXoa").count GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên 7 Bài thực hành số 4: ADO if request.form("ChkXoa")(i)<>"" then strSQL="Select * from Sinhvien Where MaSV=' " & ucase(trim(request.form("ChkXoa")(i))) & " ' " rs.open strSQL,Conn,1,3 rs.delete rs.close End if next response.redirect("Default.asp") %> GV: LÊ ANH TÚ - Bộ môn Mạng & Truyền thông – Khoa CNTT ĐH Thái Nguyên 8 . 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. 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

Ngày đăng: 25/12/2013, 23:18

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan