Brief discussion on the shortcut of database file call in ASP programming

Source: Internet
Author: User
Tags filter functions html page odbc sql variables table name variable
Program | design | data | database Introduction
  
This paper describes the most basic and critical part of "Database file invocation" in ASP programming, and discusses some skills of database file invocation in ASP programming.
  
   Introduction to ASP
  
ASP (Active Server Pages) is a Web application development technology launched by Microsoft in 1996, a synthesis of scripting languages, ActiveX components, and HTML languages, which Microsoft describes as "a server scripting environment where you can generate and run dynamic, Interactive, high-performance Web server application. Its main function is to provide a powerful way or technology for generating dynamic, interactive Web server applications. The feature is that both commands and scripts are interpreted in the server, and then the content sent to the client browser is just the standard HTML page. Its advantage is that the program design is simple and easy to understand, and convenient and quick. ASP in the domestic application has been very common, a considerable part of the dynamic Web site are using the ASP technology.
  
   calls to ASP database files
  
Although different system may install different database driver, but the general server system basically supports iis4.0/5.0, so the server system will have at least Microsoft Access Driver, Microsoft ODBC for Oracle, SQL Server and other 3 kinds of database drivers. Therefore, the server system can use at least 3 kinds of databases, such as access, Oracle, SQL Server, to work on ASP Web database design. Because Microsoft Access 97/2000 is part of Microsoft's Office family, and the interface is similar to the Office family of software, there is little or no barrier to learning, so this article uses an Access database file for ASP programming from a beginner's point of entry.
  
Make a database file (Friend.mdb) as shown in Figure 1 through an Access application
  
   Figure 1
  
Then, there are two ways to invoke the database file in ASP programming, one is manually set directly on the data source (ODBC) in Control Panel, and the other is programming to invoke the database file through a relative path, which can be applied on any server without further configuration. The first approach is simpler and safer, and this is the second way.
  
The following is a list of methods for general reading of table records in the database file in ASP programming:
  
: <HTML> <BODY>
!--#include file= "Adovbs.inc"-->
03: <%
04: ' Use ASP's connection object to open the database, the database file is the image of the <Friend.mdb> '
05:dim objconn
06:set objconn=server.createobject ("ADODB"). Connection ")
07:objconn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data source=" & Server.MapPath ("Friend.mdb")
09:objconn.open
10: Read the records of the "Data" table and store it 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 that the current pointer refers to on the browser
15:if not objRS. EOF Then
16:response.write "No.:" &objrs ("number") & "<BR>"
17:response. Write "Name:" &objrs ("name") & "<BR>"
18:response. Write "Sex:" &objrs ("Gender") & "<BR>"
19:else
20:response.write "to the end of the database, all records that meet the criteria have been displayed."
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:%>
</BODY> </HTML>
  
The code above is the design step for generally opening an Access database file in ASP programming.
  
   tips for database file calls
  
(1) In fact, the steps to open a database connection and read a table record are the same, regardless of which Access database is replaced, where the variable is the name of the database file and the name of the table, so you can rewrite line 3rd to 13th of the above program into a function form, Coexist into a file such as: Adofunctions.asp, in the future to open a database file, the file Adofunctions.asp installed (include), the code is as follows:
  
<%
Dim objconn
' variable filename is the database filename, and the variable table name is the table name
Function GetRecordSet (Filename,tablename)
' Open the database using the ASP's Connection object
Set objconn=server.createobject ("ADODB. Connection ")
Objconn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data source=" & Server.MapPath ("Filename")
objConn.Open
' Read the records of the table and then store it in the record set object ' objRS '
Dim objRS
Set objrs=server.createobject ("ADODB. Recordset ")
Objrs.open tablename,objconn,adopenkeyset,adlockoptimistic,adcmdtable
End Function
%>
  
It is known from the above code that the function name is GetRecordSet and its return value is the record set object instance that holds the table records, and is stored as a file named Adofunctions.asp. Now you can use this file to read the records of any database file. such as the general reading of the database programming can be simplified as follows:
  
<HTML> <BODY>
! --#Include file= "Adovbs.inc"-->
! --#include file= "adofunctions.asp"-->
<%
' Call the GetRecordSet function to get a real f example of the record set object and assign it to the variable objRS
Dim objRS
Set objrs=getrecordset ("Friend.mdb", "data")
' Display the record that the current pointer is pointing to on the browser
If Not objrs.eof Then
Response.Write "No.:" &objrs ("number") & "<BR>"
Response.Write "Name:" &objrs ("name") & "<BR>"
Response.Write "Sex:" &objrs ("Gender") & "<BR>"
Else
Response.Write "To the end of the database, all records that meet the criteria have been displayed."
End If
' Close the database connection and release the object instance
objRS. Close
Set objrs=nothing
Objconn.close
Set objconn=nothing
%>
</BODY> </HTML>
  
So as long as you change the database name and table name in this code set Objrs=getrecordset ("Friend.mdb", "Data"), you can invoke any Access database file, and of course, note that The field names of each table in the subsequent database must match.
  
(2) In addition, the steps to open a database connection and to filter a table record are the same regardless of which Access database is replaced, where the variables lie in the SQL statement (such as "SELECT * from Data"), the name of the database file, and the name of the table. So in the same way, you can write these 3 variables as arguments to the function, Getsqlrecordset functions, Coexistence into file name adosqlfunctions.asp, in the future to use, as long as the first of the program to include this file included, you can use the Getsqlrecordset function to open the database connection, but also to filter the table records, the function return value is stored in accordance with the SQL statement of the RE Cord the Set object instance.
  
The code is as follows:
  
<%
Dim objconn
Dim Getsqlrecordset
Function Getsqlrecordset (Strsql,filename,tablename)
' Open the database using the ASP's Connection object
  
Set objconn=server.createobject ("ADODB. Connection ")
Objconn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data source=" & Server.MapPath ("Filename")
objConn.Open
' Reads records from a table that conform to the SQL statement and is stored in the record set object
Set getsqlrecordset=server.createobject ("ADODB. Recordset ")
Getsqlrecordset.open_ Strsql,objconn,adopenkeyset,adlockoptimistic,adcmdtext
End Function
%>
  
The function name in the above code is: Getsqlrecordset, the file name is adosqlfunctions.asp.
  
You can now use this file to invoke the connection to any Access database, and to filter the table records. Take the Friend.mdb file as an example, listing all the records in table data, with the program code as follows:
  
<HTML> <BODY>
!--#include file= "Adovbs.inc"-->
!--#include file= "adosqlfunctions.asp"-->
<%
Dim objRS
Set objrs=getsqlrecordset ("Select number, name, sex from_ data", "Friend.mdb", "data")
Do as Not objrs.eof
Response.Write "No.:" &objrs ("number") & "<BR>"
Response.Write "Name:" &objrs ("name") & "<BR>"
Response.Write "Sex:" &objrs ("Gender") & "<BR>"
Loop
  
Objrs.close
Set objrs=nothing
Objconn.close
Set objconn=nothing
%>
</BODY> </HTML>
  
   Summary
  
In ASP programming, the use of good functions can often be simple to our program code, read the organization is easy to maintain, but also can avoid a lot of duplication of complex code. Like the above case, if it is simply connected to the database, in the first case, the file Adorecordset.asp installed (include) in it, to filter the records of a table in the database or other SQL statement operations, in the second case, It's OK to put the file adosqlrecordset.asp in.

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.