Database solutions for personal home pages ASP

Source: Internet
Author: User
Tags dsn execution connect odbc odbc connection query access database
Personal Homepage | solution | data | Database Now that CGI is fading, the ASP is doing a great deal to replace it. CGI is also really complicated for personal homepage writers, especially in database
The complexity is daunting. ASP, by contrast, is much simpler. Its object-oriented thinking hides the complex underlying operation, leaving only a simple call to connect
Mouth. 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 the operation is
Can be applied equally to any ODBC-enabled database. (Access is chosen because it is easy to operate and widely used, and for most small and medium projects
, access is undoubtedly the best choice. )

First use access to create a datasheet as follows:

User number name City phone number

1 Zhang three Wuhan 027-84713858

2 Dick Beijing 010-99581426

3 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 recustomerlist. EOF%>

< tr>

< Td><%=recustomerlist ("customer number")%></td>

< Td><%=recustomerlist ("name")%></td>

< Td><%=recustomerlist ("city")%></td>

< Td><%=recustomerlist ("phone number")%></td>

</tr>

<% recustomerlist. Movnext LOOP%>

</table>

</center>

You can see that the ASP's syntax is VBScript, its object-oriented thinking is similar to VB, so, as long as you first pass VB or VBScript, that ASP can easily
Hands on the ground.

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. Server in
When a user request is returned, the contents of <% and%> are interpreted as standard HTML code, which is then sent to the client browser. When you access a database, you first establish a connection,
That is, declaring an instance of a connection class (object), and then using the open method of the connection class to connect the connection object to an ODBC data source, in the example
Is DSNname, this is a DSN. As for what is DSN, the following will be introduced, and first press not the table. To this end, the connection to the database has been completed, the following to enter
The row is the operation of the database.

The operation is also very simple, first define an SQL statement string, and then call connection Execute method execution, the result of execution stored in a recordset class
In an object that can be viewed as a table that temporarily holds data, in the example of Recustomerlist. That way, the query call to the database is complete, and then you just lose it
Out can be.

In doing while ... When the loop is output, the write method of the Respond object in the ASP is invoked, which can generally be abbreviated to "=". and 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, repeatedly executed until the recustomerlist EOF (end of file) is true. In this way, the returned query results are exported.

This is just a little bit of basic knowledge of ASP data access, but we can get a glimpse of the powerful capabilities of ASP.

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 currently available for ASP support do not provide
Database support. In other words, you cannot build and own your own database in its database system. If you use a local database, you cannot configure the server host
ODBC, you cannot get DSN from your local database, and you cannot access the database through ASP. How to solve this problem? The following discussion will give a simple square
method, that is the file DSN.

First, explain a few nouns

DSN: According to the official Microsoft documentation, DSN means "the application is used to request the name of a connection to an ODBC data source (CONNECTION)", in other words
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. Therefore, when
When building 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, let's look at other types of DSN

System DSN: This DSN can be used by any user 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 registry of your system.

Once you know the preliminary knowledge above, let's see what a file DSN is. File DSN stores the details of a DSN in a file, rather than storing it in a system
The registration form of the EC. This file is a simple text file with the. dsn as the extension.

While this sounds simple, you need to know the parameters that make up the DSN file. A simple DSN file for accessing access is given below.

-----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. (Wuhan Netwalker)



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.