Personal Homepage | solution | data | Database Now that CGI is fading, the ASP is doing a great deal to replace it. CGI is also really too complicated for a personal homepage author, especially in database operations, which is incredibly complex and daunting. ASP, by contrast, is much simpler. Its object-oriented thinking hides the complex underlying operation, leaving only a simple calling interface. The operation of the database is the embodiment of its merits incisively and vividly.
This article will briefly introduce the use of ASP in the personal home page to connect the database to achieve dynamic Web pages. After reading this article, I believe your homepage will be more exciting.
1. Querying an Access database in ASP
ASP allows us to query and update the database in a simple way. The following examples are given in MS ACCESS database, but the rationale for doing so can be applied equally to any ODBC-enabled database. (Access is chosen because it is easy to operate and widely used, and access is undoubtedly the best choice for most small and medium projects.) )
First use access to create a datasheet as follows:
User number name City number 1 three Wuhan 027-847138582 li si Beijing 010-995814263 Harry Shanghai 021-85541253
Again, take a look at the following example:
<%
SET Dbconnection=server. CreateObject ("ADODB.") CONNECTION ")
DbConnection. OPEN "DSNname"
sqlquery= "SELECT * from CUSTOMERS"
SET recustionmerlist=dbconnection. EXECUTE (SQLQuery)
%>
center>
TABLE border=1>
% do whereas not recustomerlist. EOF%>
tr>
Td><%=recustomerlist ("customer number")%>
Td><%=recustomerlist ("name")%>
Td><%=recustomerlist ("city")%>
Td><%=recustomerlist ("Telephone number")%>
/tr>
% recustomerlist. Movnext LOOP%>
/table>
/center>
You can see that the ASP's syntax is VBScript, and its object-oriented thinking is similar to VB, so, as long as you first pass VB or VBScript, that ASP can easily on the ground hand.
The above part can be said to be the essence of ADO in ASP, we discuss the content of the code <% and%> is the beginning and end of ASP code. When the server returns a user request, it interprets the contents of <% and%> as standard HTML code and sends it to the client browser. When you access a database, you first establish a connection, that is, declare an instance of a connection class (object), and then use the Open method of the connection class to connect the connection object to an ODBC data source, in the example DSNname, which is a DSN. As for what is DSN, the following will be introduced, and first press not the table. To this end, the database connection has been completed, the following is the operation of the database. The
operation is also simple, defining an SQL statement string, and then invoking the execution of the connection Execute method, which is stored in the object of a recordset class (which can be viewed as a table that temporarily holds data). In the example above, it is recustomerlist. In this way, the database query call is completed, and then simply output it.
in the Do While ... When the loop is output, the write method of the Respond object in the ASP is invoked, which can generally be abbreviated to "=". The Statement recustomerlist (field name) returns the value of the corresponding field in the current record. The last call is the MoveNext method of the recordset class, which moves the record pointer to the next record, and executes repeatedly until the recustomerlist EOF (end of file) is true. In this way, the returned query results are exported.
is just a little bit of the basics of ASP data access, but we've been able to get a glimpse of ASP's powerful capabilities.
2. Now we can access the database through ASP to achieve dynamic effect. But there is another problem: most of the personal free Web pages available for ASP support are not supported by the database. In other words, you cannot build and own your own database in its database system. If you use the local database, you can not configure the server host ODBC, can not get your local database DSN, you can not access the database through ASP. How to solve this problem? The following discussion will give you an easy way to do this, which is the file DSN.
First explains several nouns
DSNs: According to the official Microsoft documentation, DSN means "the application is used to request the name of a connection (CONNECTION) connected to an ODBC data source", in other words, It is a symbol that represents an ODBC connection. It hides details such as database file names, directories, database drivers, user IDs, passwords, and so on. So when you build a connection, you don't have to think about the database file name, where it is, and so on, just give it a DSN in ODBC.
FILE DSN: When we discuss this issue, first look at the other DSN types
SYSTEM DSN: This DSN can be used by anyone who logs on to the system.
User dsn: This is a DSN that is established for a specific user. Only the user who created the DSN can see and use it. In both cases, the details of the DSN are stored in the system's registry. After the
knows the preliminary knowledge above, let's look at what a file DSN is. The file DSN stores the details of the DSN in a file, rather than in the system registry. This file is a simple text file with the. dsn as the extension.
Although this sounds simple, you need to know the parameters that make up the DSN file. The following is a simple DSN file for accessing access,
-----FILE.DSN
[ODBC]
Driver=microsoft ACCESS DRIVER (*.mdb)
Readonly=0
Usercommitsync=yes
Threads=3
Safetransactions=0
Pagetimeout=5
Maxscanrows=8
maxbuffersize=512
Implicitcommitsync=yes
Fil=ms ACCESS
Driverid=25
Name it file.dsn, of course, you can change your name according to your wishes.
After the establishment of this document, the work has been completed in half. The next task is to establish a connection. When using a System DSN, establishing a connection simply refers to the system DSN:
Conn.Open "MyDSN" (Conn is the name of the connection object)
When applying the Fils DSN as above, you need to add more parameters to explain.
Conn.Open "FILEDSN=C:\WEBDIR\FILE.DSN;DBQ=C:\DATABASE\MYDB.MDB; Uid=admin; Pwd=admin; "
C:\webdir is the directory where the. dsn file resides, and it needs to use the physical path of the file. You can use the Server.MapPath method to get it.
such as Server.MapPath ("/mysite/file.dsn"). Also, this is necessary for DBQ parameters. The UID and PWD parameters are optional.
Jane, to create a reusable file DSN
1 The DSN parameter is used to establish a text file with the name of DSN extension.
2 use FILEDSN and DBQ parameters to establish the connection, you can use Server.mpapath to get the physical path.
3 You can use this file DSN on any Access database simply by changing the DBQ, UID, and PWD parameters.
It is worth noting that different parameter values and some additional parameters are applied to databases of other non-access types.