Você está na página 1de 18

ASP Tutorial HTML Form to Database Connectivity

3/23/2012

D. Nord

Database Connectivity(1 of 3)
You can connect to the database in two ways. (You can access the following info to discuss the two options.
http://www.powerasp.com/content/database/dsn_vs_dnsless.asp http://www.4guysfromrolla.com/webtech/070399-1.shtml http://www.powerasp.com/content/hintstips/permissions.asp -One more

DSN connection (DSN stands for Data Source Name this method can be used if you already have a server set up for the web pages via ODBC) DSN-less connection (this is an easier way to connect to your database you do not have to configure your computer to set this up) The following code shows you how to connect to your access database with a DSN-less connection.
3/23/2012 D. Nord 2

Create a Database(2 of 3)
First create a database named comments.mdb using Microsoft Access. Then create a table and name it commentadd. Include the following fields in the table:
name comments email
3/23/2012 D. Nord 3

Database Table Before data is entered(3 of 3)

3/23/2012

D. Nord

Connecting to a Database(1 of 5)
Next, create a form with the fields you require. Name the file anything you want. Ex: contact.htm In the <form method="POST" action="addrecord.asp"> area in the form, change the action=addrecord.asp to whatever name you want and include the .asp extension. 3/23/2012 D. Nord

Creating the HTML Form(2 of 5)


<html> <head> <title>Feedback Form</title> </head>

<body> <p><b><font size="6" face="Century Gothic">Feedback Form</font></b></p> <p><font size="4" face="Century Gothic">Please fill out this form to help us improve our site.</font></p>
3/23/2012 D. Nord 6

(3 of 5) <form method="POST" action="addrecord2.asp"> <p><b>Name: </b> <input name="name" type="text" size="32"></p> <p><b>Comments: </b> <textarea rows="4" name="comments" cols="30"></textarea></p> <p><b>Email Address: </b> <input type="text" name="email" size="26"></font></p>
3/23/2012 D. Nord 7

(4 of 5) <p>&nbsp;</p>

<p> <input type="submit" value="Submit Your Entries" name="B1"><input type="reset" value="Clear Your Entries" name="B2"> </p>
</form> </body> </html>
3/23/2012 D. Nord 8

Form Page - Example(5 of 5)

3/23/2012

D. Nord

Connecting to a Database(1 of 8)
Next create the ASP response page and give it the same name as is found in the form in action=.. The following slides contain the ASP code to connect to the database. Lines 3 6 declare all the variables required. Line 10 - sets up the connection to the database and opens the connection with the database

3/23/2012

D. Nord

10

Code Explanation(2 of 8)
Line 19 opens the table in the database using the rs.open method. Line 20 - 24 adds new data from the form to the table using the rs.addnew method. Line 25 - 27 updates the table and moves to end of the record. Lines 28 - 30 closes all the connections to the database.
3/23/2012 D. Nord 11

Connecting to a Database(3 of 8)
1. <%@ Language="VBScript"%> 2. <% 3. 4. 5. 6. 'Declare all local variables dim rs dim strconn dim strID

3/23/2012

D. Nord

12

(4 of 8)
7.

Sets up the connection to the database(The entire line 9 must be on one line)
set strconn=server.createobject("adodb.connection") strConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("database.mdb")

'Create an ADO recordset object set rs = server.createobject("adodb.recordset") 'Initialise the strID variable with an SQL statement to query the database strID = "SELECT commentadd.Name, commentadd.comments, commentadd.email FROM commentadd; 'Set the cursor type we are using so we can navigate through the recordset rs.CursorType = 2 'Set the lock type so that the record is locked by ADO when it is updated rs.lockType = 3 'Open the recordset with the SQL query (the 2,2 deals with writing and locking the table) rs.Open strID, strconn, 2,2
3/23/2012 D. Nord 13

(5 of 8) 20. Adds new data from the Form to the Table 21. rs.addnew 22. rs("name") = request("name") 23. rs("comments") = request("comments") 24. rs("email") = request("email") 25. Updates the Table and Moves to end of record 26. rs.update 27. rs.movelast 28. Closes all the connections to the Database 29. set rs = nothing 30. set conn = nothing 31. %>

3/23/2012

D. Nord

14

(6 of 8) 32. <html> 33. <head> 34. <title>Thank you</title> 35. </head> 36. <body> 37. <center>Thank you</center><br> 38. <center>Please click <a href="/index.html">here</a> 39. to return to the home page.</center> 40. </body> 41. </html>
3/23/2012 D. Nord 15

ASP Response Page(7 of 8)

This is the response page that is displayed once the form is submitted. This page is generated by the ASP code shown in the previous slides. The message displayed above can be changed to show your own message. 3/23/2012 D. Nord 16

Access Database(8 of 8)

This is what the database will look like after the form is submitted. The data from the form is saved in the database.
3/23/2012 D. Nord 17

Components and Objects


(These are different than the ADO Objects)
ASP consists of five primary built in objects
Application manages Web application's information Request retrieves information from a browser for processing at the server Response transmits information from a Web server to browser Server controls behavior of a Web server Session tracks and manages individual user sessions within particular Web application

The built-in objects are special because they are built into ASP and need not to be declared or created before they can be used in scripts.
3/23/2012 D. Nord 18

Você também pode gostar