Querying Web Database with ASP and ADO

Source: Internet
Author: User
Tags count html page http request insert web database
ado|web| Data | Database ASP and ADO Introduction

In do can be used in VBScript, JavaScript language to control the access of the database (while ASP is precisely the interpretation of the script language environment) and the output of the query results; ADO uses the Recordsets object to manipulate the database, and can connect multiple databases that support OD BC. Data Search ASP (Active server Pages) is a server-side (server-side) script execution environment that users can use to generate and execute dynamic, interactive, high-performance Web server applications. When the script executes on the server side rather than on the client, the Web server completes all the work that produces the HTML (Hypertext Markup Langua GE) page that is sent to the browser. Figure 1
Figure 1 is the ASP's working mode. When the browser presents a request for a file (. asp) to the Web server, an ASP script starts executing, when the Web server invokes the ASP, reads the file into and executes each command, and sends the results back to the browser as an HTML page.
ADO (ActiveX Data Object,activex) is an ASP built in a Ctivex server component (ActiveX server Component) for database access, which can be combined with ASP, Set up the content of the Web page to provide database information, query, insert, UPDATE and delete the database.
To perform ADO, the server side must have Windows NT Server and Internet I nformation serv er (IIS) installed, and the client will only have IE or Netscape's newer version of the browser. Several scenarios for site database access
The realization of web database access can be divided into two categories, one is to use the Web server as the intermediary, the browser and data source connected to the server side to perform the operation of the database; the other is to download the application and database to the client and perform the access to the database on the client.
1. Public Gateway Interface CGI (Common gateway Interface)
When the browser makes an HTTP request, the Web server executes the appropriate application, accesses the site database, and sends the query results back to the browser in HTML format. This approach has many drawbacks, limited functionality, difficult development, and no transaction processing (Transaction) functionality, which limits its application to a large extent.
2. Internet DB connector IDC (Internet database Connector)
This is a simple solution, the operation of the database is still on the web side, but the user can query, input, UPDATE, delete, etc. through SQL (Structured Query Language) statement. The IDC design is simple, straightforward to execute without compiling, and supports multiple queries for data (multiple query).
3. Advanced Database Connector ADC (Advanced data Connector)
The operation of the database in the first two scenarios is done on the server side, while the ADC performs the data query action on the browser side. When the browser raises the operation request to the database, ADC first downloads the corresponding database to the browser side, and operates the database on the client side. Clearly, such programmes are highly efficient. The client's query to the database is done by a DC ActiveX control installed on the client, which is automatically downloaded and installed on the client by the site server.
4. ADO and ASP
This is a complete Web database access scheme, which can combine ADO with ASP, set up a Web page to provide database information, execute SQL commands in Web pages, query, insert, UPDATE, delete the database. A case: Hong Kong and Macao Certificate Information inquiry
This example to complete the operation of the Hong Kong and Macao Immigration database to query, customers can give one or several query conditions, and then display the query results as required.
1. Determining the data source
To access the online database, first set the data source. On the server, open Control Panel in My Computer, double-click ODBC, and select Add under System DSN to select a predefined database type, name, and location. Here is to use the FoxPro database, the data source name is GACX, the primary database name is VISA.DBF, the encoded database name is ZD.DBF.
2. ADO query the use of the database
The design method of ADO query database is to get Object "ADODB" first with Server.creatobject. An instance of Connection, and open the database to be accessed with "open":
Set conn=server.creatobject ("ADODB. Connection ")
Conn.Open "database name to be accessed"
Then execute the SQL command, which operates on the database, where you use the Execute command:
Set RS =conn. Execute ("Select ..."). From ... Where ... ") Next you can query the database to use the following command for the Recordsets object:
* RS. Fildes.count
Number of fields recordsets
* RS (i). Name
First field name
* RS (i)
Record of field I
* RS ("field name")
Record of specified field
* RS. Eof
Whether to point to the last field True or False
* RS. MoveFirst
Point to First record
* RS. MovePrev
Refers to a record in the forward line
* RS. MoveNext
Point to the last record
* RS. MoveLast
Point to last record
* RS. GetRows
Store the results of the query in an array and then read from the array
* RS. Properties.Count
Get the total number of connection or ResultSet properties for ADO
* RS. Properties (Item). Name gets the attribute names of the connection or Resultse T of ADO
* RS. Properties (Item)
Get the connection or ResultSet attribute value of ADO
3. Concrete implementation
Homepage for Hong Kong and Macao documents information inquiries, the corresponding procedures for gacx.asp. Query results in two ways, one is to display the main information-Hong Kong and Macao documents information Query results list, the corresponding procedure for gacxlb.asp; First, show all the information-------------------------------- If the query condition is not satisfied, also displays a prompt page, tells the user does not have satisfies the condition the result, completes by the program gacxts.asp.
According to the database provided by the customer and the requirements of the query to establish inquiries, query results, query results list page, and give a simple prompt interface, tell the user does not exist to meet the conditions of the results, please requery, these work can be done in FrontPage 98, the entire page should be simple and generous, the theme is clear. The program code is written under Microsof t Visual I Nterdev.
The following deals with the contents of the encoded database ZD.DBF. You can see from Figure 2 that there are many options with drop-down boxes, such as "gender", "birthplace", and so on, which are extracted from the ZD.D BF database. Because there are a lot of options, such as the exit port has 101, although also available < select><option>...</option></se lect> format in the HTML page of the various options listed , but will cause great trouble to the maintenance of the program, once the content of the coding database changes, the program will also make a lot of changes. And the option of the Drop-down box is encoded in the ZD.DBF, when entering data into the main database, these options are only input encoding, but the display can not display the code, must be encoded corresponding to the Chinese character display. The code for this procedure is as follows (in the case of sex): Figure 2
<%
Set conn=server.creatobject ("ADODB. Connection ")
Conn.Open "Gacx"
Set RS =conn.execute ("Select BC,MC from Zd Where BC like" se% ")
Response.Write "<select size=1 name=" SEX ">" Do, not R s.eof
Response.Write "<option value=" "RS (0)" ' > "&rs (1)
Rs.movenext
Loop
Response.Write "</SELECT>"
Rs. Colse
Conn.colse
%>
Among them, ZD.DBF contains two fields MC, BC,MC Express to encode items, such as male, female, BC expression code, such as the first two of the sex code is "se", male use "SE1" said, female "Se2" said.
The most important thing in a database query is how to get the query criteria. In this example, the items to be queried can be divided into two categories: one is the query condition of the input text class, the other is the item selected from the Drop-down box, and treats it differently. Te xt type of query conditions are well handled, such as the name, the author's input is named "XM", if you enter the query item is not empty, add a query condition "name=request.form (" XM ") can be. The Drop-down box is more complex to handle, when the Drop-down box selects an item, for example, the gender-selected male, the Drop-down box is entered "SE 1" (but is reflected in the database when "S ex=1", because the database input dictionary with encoded items when only input encoding), is to query the "SEX=SE1" item. Processing should be "SE1" in the "1" extracted, with a function mid can be, query conditions for "Sex=mid (" SE1 ", 3, 1)", that is, from the database to find sex=1 items, and all meet the conditions of the items are not repeated list.
Query results are easy to implement, only the query results of each record should be displayed in the form of a list, but in the display of query results to display the corresponding characters of the encoding, is to find the code for the "Se" & "1" the corresponding encoding value of the program is as follows:
<table border= "1" width= "#C0C0C0" cell spacing= "0" cell padding= "0"



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.