Shortcuts for calling database files in ASP development

Source: Internet
Author: User

Introduction
This article focuses on ASP Program The most basic and critical part of the design is "database file calling", and some tips on database file calling in ASP programming are also described.

ASP Introduction

ASP (Active Server Pages) is a web application development technology launched by Microsoft in 1996. It is a combination of script language, ActiveX component, and HTML language, microsoft describes it as "a server script environment where dynamic, interactive, and high-performance WEB server applications can be generated and run ". Its main function is to provide a powerful method or technology for generating dynamic and interactive web server applications. The characteristic is that commands and scripts are interpreted and executed on the server, and the content sent to the client browser is only a standard HTML page. The advantage is that the program design is easy to understand and convenient. ASP is widely used in China, and a considerable number of dynamic websites use ASP technology.

ASP database file call

Although different systems may install different database drivers, generally server systems support iis4.0/5.0, therefore, there are at least three database drivers on the server system, including Microsoft Access driver, Microsoft ODBC for Oracle, and SQL Server. Therefore, the server system can use at least three types of databases, including access, Oracle, and SQL Server, to design ASP Web databases. Microsoft Access 97/2000 is one of Microsoft's office series and has similar interfaces with office series software. Therefore, this article is intended for beginners, use Access database files for ASP programming.

Create a database file (friend. mdb) through the access application)

Figure 1

In ASP programming, there are two ways to call the database file: one is to manually set the file directly on "Data Source (ODBC)" in "Control Panel; another method is programming. database files are called through relative paths. This method can be applied to any server without any further configuration. The first method is relatively simple and secure. This article describes the second method.

The following describes how to read Table records in database files in ASP programming:

01: <HTML> <body>
02: <! -- # Include file = "adovbs. Inc" -->
03: <%
04: 'Use the ASP connection object to open the database. The database file is <friend. mdb>'
05: dim objconn
06: Set objconn = server. Createobject ("ADODB. Connection ")
07: objconn. connectionstring = "provider = Microsoft. Jet. oledb.4.0 ;"&_
08: "Data Source =" & server. mappath ("friend. mdb ")
09: objconn. Open
10: read the records of the "data" table and store them in the record set object.
11: dim objrs
12: Set objrs = server. Createobject ("ADODB. recordset ")
13: objrs. Open "data", objconn, adopenkeyset, adlockoptimistic, adcmdtable
14: 'display the record indicated by the pointer on the browser
15: If not objrs. EOF then
16: Response. Write "No.:" & objrs ("no.") & "<br>"
17: Response. Write "name:" & objrs ("name") & "<br>"
18: Response. Write "Gender:" & objrs ("gender") & "<br>"
19: else
20: Response. Write "all matching records are displayed at the end of the database"
21: end if
22: 'close the database connection and release the object instance.
23: objrs. Close
24: Set objrs = nothing
25: objconn. Close
26: Set objconn = nothing
27: %>
28: </body>

AboveCodeDesign Steps for opening access database files in ASP programming.

Database File calling skills

(1) In fact, no matter which access database you change to, the steps for connecting to the database and reading table records are the same. The variable is the name of the database file and the table name, therefore, you can set the 3rd ~ Line 13 is rewritten as a function and coexist into a file, such as adofunctions. ASP. If you want to open a database file in the future, set the file adofunctions. ASP installation (include). The Code is as follows:

<%
Dim objconn
'Variable filename is the database file name, and variable table name is the table name.
Function getrecordset (filename, tablename)
'Use ASP connection object to open the database
Set objconn = server. Createobject ("ADODB. Connection ")
Objconn. connectionstring = "provider = Microsoft. Jet. oledb.4.0 ;"&_
"Data Source =" & server. mappath ("FILENAME ")
Objconn. Open
'Read the table records and store them in the record set object "objrs"
Dim objrs
Set objrs = server. Createobject ("ADODB. recordset ")
Objrs. Open tablename, objconn, adopenkeyset, adlockoptimistic, adcmdtable
End Function
%>

According to the code above, the function name is getrecordset, and its return value is the record set object instance that stores table records. It is saved as the file name adofunctions. asp. Now, you can use this file to read records of any database file. For example, the programming for reading databases can be simplified as follows:

<HTML> <body>
<! -- # Include file = "adovbs. Inc" -->
<! -- # Include file = "adofunctions. asp" -->
<%
'Call the getrecordset function to obtain a record set object instance F and assign it to the variable objrs.
Dim objrs
Set objrs = getrecordset ("friend. mdb", "data ")
'Display the records indicated by the current pointer on the browser
If not objrs. EOF then
Response. Write "No.:" & objrs ("no.") & "<br>"
Response. Write "name:" & objrs ("name") & "<br>"
Response. Write "Gender:" & objrs ("gender") & "<br>"
Else
Response. Write "all qualified records have been displayed at the end of the Database"
End if
'Close the database connection and release the object instance
Objrs. Close
Set objrs = nothing
Objconn. Close
Set objconn = nothing
%>
</Body>

Therefore, set objrs = getrecordset ("friend. you can call any access database file by changing the Database Name and table name in mdb "," data "). Of course, note that, the field names of each table in the database must match.

(2) In addition, no matter which access database you change to, the steps for connecting to the database and filtering table records are the same. The variable is an SQL statement (for example, "select * from data ") the name of the database file and the name of the table. Therefore, you can use these three variables as function parameters to write the getsqlrecordset function and save it as a file named adosqlfunctions. ASP. If you want to use this file later, you can use the getsqlrecordset function to open the database connection and filter table records as long as you include the file at the beginning of the program, the return value of this function is a record set object instance that stores SQL statements.

The Code is as follows:

<%
Dim objconn
Dim getsqlrecordset
Function getsqlrecordset (strsql, filename, tablename)
'Use ASP connection object to open the database

Set objconn = server. Createobject ("ADODB. Connection ")
Objconn. connectionstring = "provider = Microsoft. Jet. oledb.4.0 ;"&_
"Data Source =" & server. mappath ("FILENAME ")
Objconn. Open
'Read records that comply with SQL statements from the table and place them in the record set object
Set getsqlrecordset = server. Createobject ("ADODB. recordset ")
Getsqlrecordset. Open _ strsql, objconn, adopenkeyset, adlockoptimistic, adshorttext
End Function
%>

In the above Code, it is called getsqlrecordset and the file name is adosqlfunctions. asp.

Now, you can use this file to call any access database connection and filter the table records. Take the friend. MDB file as an example to list all the records in the table data. The program code is as follows:

<HTML> <body>
<! -- # Include file = "adovbs. Inc" -->
<! -- # Include file = "adosqlfunctions. asp" -->
<%
Dim objrs
Set objrs = getsqlrecordset ("select number, name, gender from _ data", "Friend. mdb", "data ")
Do while not objrs. EOF
Response. Write "No.:" & objrs ("no.") & "<br>"
Response. Write "name:" & objrs ("name") & "<br>"
Response. Write "Gender:" & objrs ("gender") & "<br>"
Loop

Objrs. Close
Set objrs = nothing
Objconn. Close
Set objconn = nothing
%>
</Body>

Summary

In ASP programming, good functions can be easily understood for our program code, and the reading structure is easy to maintain. At the same time, a large number of complicated and repetitive codes can be avoided. In the above case, if you just connect to the database, use the first case to set the file adorecordset. you can install (include) Asp. to filter the records of a table in the database or operate other SQL statements, use the second case to set the file adosqlrecordset. you can install ASP.

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.