Presentation is loading. Please wait.

Presentation is loading. Please wait.

DT221/3 Internet Application Development Active Server Pages & Database Connection.

Similar presentations


Presentation on theme: "DT221/3 Internet Application Development Active Server Pages & Database Connection."— Presentation transcript:

1 DT221/3 Internet Application Development Active Server Pages & Database Connection

2 Example of ASP Database Connection A simple application example 1)Use ASP for HTML form 2)Login Check 3)Database Connection 4)Database Insertion, Update and Deletion

3 Example of ASP Database Connection User ManagementAction Process Insertion Form Update Form Deletion Conform Login Form Insertion Update Deletion

4 Example of ASP Database Connection-login.asp Login page User Name: Password:

5 Example of ASP Database Connection-check.asp(1) <% Dim user, password user = Request.Form("user").Item password = Request.Form("password").Item dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs.ActiveConnection = Conn mySQL = "Select SecurityLevel From cliu.users Where UserName= '" & user & "' and Password = '" & password & "'" Rs.Open(mySQL)

6 Example of ASP Database Connection-check.asp(2) if not Rs.eof then if Rs("securityLevel")=12 then Session("level")=12 Conn.Close Set Conn = NOTHING Response.Redirect ("menu.asp") end if if Rs("securityLevel")=8 then Session("level")=8 Conn.Close Set Conn = NOTHING Response.Redirect("showuser.asp") end if if Rs("securityLevel")=1 then Session("level")=1 Conn.Close Set Conn = NOTHING Response.Redirect("welcome.asp") end if

7 Example of ASP Database Connection-check.asp(3) else Session("level")=-1 Conn.Close Set Conn = NOTHING Response.Redirect("login.asp") end if %>

8 Example of ASP Database Connection-check.asp(3) else Session("level")=-1 Conn.Close Set Conn = NOTHING Response.Redirect("login.asp") end if %>

9 Example of ASP Database Connection-adduser.asp …… Please enter the new user name: Please enter the new password: Please enter the first name: Please enter the second name: Please enter the security level:

10 Example of ASP Database Connection-addusertodb.asp <%Dim user, password, first, second, level user = Trim(Request.Form("user").Item) password = Trim(Request.Form("password").Item) first = Trim(Request.Form("first").Item) second = Trim(Request.Form("second").Item) level = Request.Form("level").Item dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") mySQL = "Insert into cliu.users values ( '" & user& "', '" & password & "', '" & first & "', '" & second &"', " & level & ")“ Conn.Execute(mySQL) Conn.Close Set Conn = NOTHING Response.Redirect("showuser.asp") %>

11 Example of ASP Database Connection-edit.asp …… ") dim Conn, Rs, user_id, i set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs.ActiveConnection = Conn Rs.Open("Select * From cliu.users") While not Rs.eof Response.Write("user name = " &Rs("UserName").Value & " ") Response.Write(" edit ") Response.Write(" ") Rs.MoveNext Wend %>

12 Example of ASP Database Connection-edituser.asp(1) <%Dim userid userid = Int(Request.QueryString("id").Item) dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs = Conn.Execute("cliu.selectuser " &userid) %> Select user to edit

13 Example of ASP Database Connection-edituser(2).asp //Client side javascript function check(){ test=true if (document.form1.user.value==""){ alert("Please enter the user name"); document.form1.user.focus(); test=false;} if (document.form1.password.value==""){ alert("Please enter the password"); document.form1.password.focus(); test=false;} if (document.form1.first.value==""){ alert("Please enter the user' first name"); document.form1.first.focus(); test=false;} if (document.form1.second.value==""){ alert("Please enter the user's second name"); document.form1.second.focus(); test=false;}

14 Example of ASP Database Connection-edituser(3).asp if (document.form1.level.value==""||(document.form1.level.value!=12&&docu ment.form1.level.value!=8&&document.form1.level.value!=1)){ alert("Please enter the correct level"); document.form1.user.focus(); test=false; } return test } >

15 Example of ASP Database Connection-edituser.asp(4) Please enter the new user name: " size="20"> Please enter the new password: " size="20"> Please enter the first name: " size="20"> Please enter the second name: " size="20"> Please enter the security level: " size="20">

16 Example of ASP Database Connection-editusertodb.asp <%Dim user, password, first, second, level userid = Int(Request.Form("userid").Item) user = Trim(Request.Form("user").Item) password = Trim(Request.Form("password").Item) first = Trim(Request.Form("first").Item) second = Trim(Request.Form("second").Item) level = Int(Request.Form("level").Item) dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Conn.Execute("cliu.edituser "& userid &",'" & user& "', '" & password & "', '" & first & "', '" & second &"', " & level) Conn.Close Set Conn = NOTHING Response.Redirect("menu.asp") %>

17 Example of ASP Database Connection-deluser.asp(1) Database Connection function ConfirmDelete(s_User, s_ID) { var s_Dialog = "Are you sure you want to delete user '" + s_User + "'?"; if(confirm(s_Dialog)) { document.location.href = "deluserdb.asp?id=" + s_ID; } }// end function

18 Example of ASP Database Connection-deluser(2).asp ") dim Conn, Rs, user_id, i set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs.ActiveConnection = Conn Rs.Open("Select * From cliu.users") While not Rs.eof Response.Write("user name = " &Rs("UserName").Value & " ") Response.Write(" del ") Response.Write(" ") Rs.MoveNext Wend %>

19 Example of ASP Database Connection-deluserdb.asp <% Dim userid userid = Int(Request.QueryString("id").Item) dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Conn.Execute("cliu.deluser "& userid ) Conn.Close Set Conn = NOTHING Response.Redirect("menu.asp") %>


Download ppt "DT221/3 Internet Application Development Active Server Pages & Database Connection."

Similar presentations


Ads by Google