Today, CGI has gradually declined, and the ASP has a big way, and it has a great potential. CGI is indeed too complicated to the individual page producer, especially in the database
In the event, it is daunting. In contrast, the ASP is much simpler. Its object-oriented thinking, covering complex underlying operations, leaving only a simple call connection
mouth. The operation of the database is even more than its advantages.
This article will briefly introduce the use of ASP connection databases in the Personal Home page to implement dynamic web pages. I believe that you will be more exciting after reading this article.
1. Query Access database in ASP
ASP allows us to query, update the database in a simple method. The examples will be given in the examples of MS Access Database, but the principle of its operation is
It can be applied to any database that supports ODBC. (Which ACCESS is selected because it is easy to operate, and it is widely used, and most of the small and medium-sized projects.
The ACCESS is undoubtedly the best choice. )
Use Access to build a data sheet as follows:
User ID Name City Phone Number
1 three Wuhan 027-84713858
2 Li Si Beijing 010-99581426
3 Wang Wuhang 021-85541253
Please look at the following example:
<%
Set dbconnection = server.createObject ("adoDb.connection")
DBConnection.open "dsnname"
SQLQuery = "SELECT * from Customers"
Set recustMerlist = dbconnection.execute (SQLQuery)
%>
<% Do while not recustomlist.eof%>
<% = Recustomerlist ("Customer Number)%> TD>
<% = recustomerlist ("Name)%> TD>
<% = RecustomerList ("City")%> TD>
<% = Recustomerlist ("Phone Number)%> TD>
TR>
<% Recustomerlist.movnext loop%>
TABLE>
Center>
It can be seen that the syntax of the ASP is VBScript, and its object-oriented thought is similar to the VB, so as long as you start VB or VBScript, the ASP can be easy.
Ground hand.
Part of the above can be said to be the essence of ADO in ASP. Let's discuss the code of the code <% and%> is the beginning and end of the ASP code. Server
When returning to the user request, first explain the contents of the content in <% and%> becomes a standard HTML code, and then send it to the client browser. When you have access to a database, you first build a connection.
That is, declare an instance (object) of a CONNECTION class, then use the open method of the Connection class to connect the connection object to an ODBC data source. In the above example, DSNNAME is DSNAME, which is a DSN. As for what is DSN, it will be introduced later, and press it first. At this point, the connection of the database has been completed,
The line is the operation of the database.
The operation is also very simple, first define a SQL statement string, then call the Connection execute method to execute, the result is stored in a Recordset class
(It can be seen as a table of a temporary storage data), in the previous example, RecustomerList. In this way, the database's query call is also completed, and then you only need to lose it.
I.e.
When outputting in the loop of Do While ... Loop, the Write method of the responder object in the ASP is called, which is generally abbreviated as "=". Statement
RecustomerList returns the value of the current record corresponding field. Finally, the MoveNext method of the Recordset class is called, move the record pointer to the next
Record, repeatedly, until the RecustomerList's EOF is true. In this way, the result returned the query result is output.
The above is just a basic knowledge of ASP data access, but we have symmelly promptly asppted powerful features.
2. Now we can access databases through ASP to achieve dynamic effects. But there is also a question: now provides personal free webpages supported by ASP, most of them do not provide
Support for databases. That is, you can't create and have your own database in its database system. If you use a local database, you can't configure the server host.
ODBC, you can't get the DSN of your local database, you can't access the database via ASP. How to solve this problem? The following discussion will give a simple party
Law, that is, File DSN.
First explain a few nouns
DSN: According to Microsoft's official documentation, DSN means "application to request a connection to the connection" of the ODBC data source,
Say, it is a symbol that represents an ODBC. It hides details such as database file names, directories, database drivers, user ID, passwords, etc. Therefore, when
When establishing a connection, you don't have to consider the database file name, where is it, etc., just give it a DSN in the ODBC.
File DSN: When we discuss this problem, let's take a look at other DSN types.
SYSTEM DSN: This DSN can be used by any user logged in to the system.
User DSN: This is a DSN established for a particular user. Only users who create this DSN can see and use it. In both cases, DSN's details are stored
In the registry of the system.
After knowing the above preparatory knowledge, let's take a look at what is File DSN. File DSN stores the details of the DSN in a file instead of storage
In the registry. This file is a simple text file with .dsn as an extension.
Although this sound is quite simple, you need to know the parameters that make up the DSN file. Let's give a simple DSN file to access Access.
---- File.dsn ----
[Odbc]
Driver = Microsoft Access Driver (* .mdb)
Readonly = 0
UserCommitsync = YES
Threads = 3
Safetransactions = 0
PageTimeout = 5
MaxScanRows = 8maxbuffersize = 512
IMPlicitCommitsync = YES
FIL = MS Access
Driverid = 25
Name it as file.dsn, of course, you can rename it according to your will.
After the establishment of this document, the work has been completed. The following work is to create a connection. When using a SYSTEM DSN, establish a connection as long as it is simply referenced
System DSN:
Conn.open "MYDSN" (conn is the name of the connection object)
When applied with the above Fils DSN, you need to add more parameters to explain.
C: / WebDir is the directory where the .dsn file is located, it needs to use the physical path to the file. You can get it with a server.mappath method. Server.mappath
("/Mysite/file.dsn"). Again, this is also necessary for DBQ parameters. The UID and PWD parameters are optional.
Simply, build a reusable File DSN
1) Create a text file with DSN as an extension with a DSN parameter.
2) Establish a connection with FileDSN and DBQ parameters, you can get a physical path with server.mpapath.
3) Use only the DBQ, UID, and PWD parameters, you can use this File DSN on any Access database.
It should be noted that different parameter values and some additional parameters are required for other non-Access types of databases. (Wuhan Netwalker)