Database File calling in ASP development (2)

Source: Internet
Author: User
Tags table name access database
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> </HTML>
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.
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.