Connect via ASP with Access database (with source code) (2)

Source: Internet
Author: User
Tags connect table name access database

Three key parts: Establish a connection to the database and add the information gathered in the form to an Access database.

The ASP code is placed between 〈% and%>.

<%
CompanyName = Request ("CompanyName")
CName = Request ("CName")
City = Request ("City")
Tel = Request ("tel")

Above, first get the data filled out in the form, it is easy to collect data by using the Request object of ASP. Double quotes are the names (name) of each item that was created in the previous step. So it's a good habit to name each project in the future, although sometimes it's not necessarily used. The data obtained is stored in the variable on the side of the equal sign.

If CompanyName = "" or CName = "" or city = "" or Tel = "" Then
' Response.Write ' Response.Redirect "Error.htm"
Response.End
End If

Here is to detect whether each item entered something, if it is empty, do not go down and display the error message. Note: You can use two methods. I've listed it all. Method One: Use Response.Write method to write an error page, similar to javascripts document.write writing, just ASP use Response.Write bale. Method Two: Use Response.Redirect method to guide error page error.htm. I precede the code in method one with a single quotation mark to denote the comment. (in ASP, the annotation is expressed in single quotes ').

Set Dbconnection=server.createobject ("ADODB. CONNECTION ")
DBPath = Server.MapPath ("Customer.mdb")
DbConnection. Open "Driver={microsoft Access driver (*.mdb)};d bq=" & DBPath

(1) Establishing a Connection object: DbConnection, establishing a connection (Connection) is the beginning of database access. We use the ASP's built-in object method Server.CreateObject to establish a connection to the database. ADODB is an ASP database access component, do not worry as long as the ASP has it, is built.
(2) Use the Dbconnection.open method to open the database Customer.mdb. Driver parameter syntax: Driver={microsoft Access driver (*.mdb)} must write this. Tells the system to use Access ODBC-driven processing. The DBQ parameter is used to specify the open database file, which must be the full path. For example, "C:\inetpub\wwwroot\customer\customer.mdb", we use the Server.MapPath method to get the full path of the database.

Set rs = Server.CreateObject ("ADODB.") Recordset ")

Establish the Recordset object Rs. Connection only establishes a connection to the database, and the Recordset object should be set up to access the data.

Rs. Open "Customer", DbConnection, adOpenDynamic, 3

Open the table "customer" in the database (the customer is the table name). Grammar:

Rs.Open data table name or SQL instruction, already established connection object (here is DbConnection), lock type


Rs. AddNew
RS ("company name") = CompanyName
RS ("Contact person name") = CName
RS ("town") = City
RS ("phone number") = Tel
Rs. Update

Use AddNew to add data to a database request. Finally write with update. Note: The variable on the right side of the equal sign holds the form data obtained with request. Double quotes inside the database are the fields, should be consistent with the database AH. Otherwise, there will be mistakes. The data within this variable is written to the corresponding field in the database.

Response.Write "Response.Write "<a href= ' viewdatabase.asp ' > View Data </a>"
Response.Write "</font></center></body>

A successful write displays a congratulatory message and establishes a connection to view the database records.

%>

<title></title>

<body>
</body>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.