Database file invocation in ASP development (2)

Source: Internet
Author: User
Tags filter include variables table name access database
Data | Techniques for database file invocation

(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
%>



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.