With the rapid development of Internet / Intranet technology, Web has become a new business. Various manufacturers have also established direct contacts with end users through Internet / Intranet technology, such as selling products to users on the Web, so that users Query your favorite items online and provide online ordering services. So the web-based database technology came into being. However, in the traditional HTML page, the access database is generally implemented by the public gateway interface (CGI), which is not only developing difficulties, but also reduces the running efficiency of the server when there is a lot of concurrent requests, while using ASP (Active) Server Pages) Technical implementation of database access can better solve this problem.
Microsoft's ASP technology is a script writing model running on the server, which enables developers to write scripts using almost all scripting languages (VBScript, JScript or Perl, etc.), which can perform application logic and can call ActiveX components. Perform special tasks, such as database queries, file input and other. It integrates the simplicity of IDC and the flexibility of ISAPI. Since Microsoft IIS 3.0 (Internet Information Server 3.0) has made Microsoft and has been committed to developing ASP technology. So far, ASP technology has been further developed, providing new features such as ASP scripting debugging tools, transaction, new ActiveX components, RDS in IIS 4.0.
In the ASP script, you can access the database in three ways: Traditional IDC (Internet DataBase Connector) mode, and RDS (Remote Data Service). In terms of concept, these three access methods are done by Internet Information Server. The web browser uses the HTTP protocol to submit requests to the Internet Information Server (IIS). The Internet Information Server performs the operation of the access database and answered as a document in an HTML format.
One. Internet Database Interface (IDC)
IDC is a traditional database query tool that defines and executes the SQL command for the database query and returns a page of the specified data format to the browser. The maximum feature of using IDC access to the database is simple, almost no need to program access to the database.
IDC uses two files to control how to access databases and how to create a returned web page. These files are IDC (.idc) files and HTML extensions (.htx) files, respectively.
The .idc file must include an ODBC data source (DataSource), the file name (Template) of the HTML extended file, and the SQL statement to be executed. There are many optional words
Segment, you can use it as needed.
The .htx file is a HTML document with the additional marker enclosed in <%%> or .
Use these tags to add dynamic data to the document. There are six keywords in .htx files
(Begindetail, EndDetail, IF, ELSE, ENDIF, and "% z") use to control how data in the database is merged with .htx files in HTML format. Database column name description What data is returned in the HTML document.
Below is a simple example of IDC application, this example is a inventory inventory of a virtual fruit store.
.idc file: DataSource: Kucun
template: idcreslt.htx
SQLStatement: select * from inventory table
.htx file:
Sequence Number TD> | Product Name TD> | Stock Quantity TD> |
Td> | Unit TD> | Enterprise Date TD> TR>
<% endIF%> | |||||
<% ID%> TD> | <% Product Name%> TD> | <% inventory
%> Td> | <% 进价%> td> | <% unit%> td> | <% purchase date%> td> tr>
<% enddetail%> Table> body> html> Note: <% BEGINDETAIL%> and <% endDetail%> Determines the boundary, determine which of the database will be used as return data and displayed in the document. The column returned by the query is enclosed by <%%>, as in the example of <% product name%> and <% inventory%>, etc. To perform an IDC query, the usual approach is to embed a .idc file connection in the HTML file. example For example, the following HTML statement issues a request to the web server and requires the IDCTEST.IDC file. Stock query When the web server receives this request, call httpodbc.dll (IDC), with a certain data source Connect, pass the SQL command to the database. When the SQL statement is executed, the IDC integrates the returned data into the .htx file. IDC returns this document to the web server, the web server returns to the browser. two. ActiveX Data Object (ADO) Unlike the IDC, use the ADO access database more similar to writing database applications, ADO puts most of the database operations in seven objects, programming these objects to perform the appropriate database operations in the ASP page. ADO is one of the core of ASP technology, which is concentrated in ASP technology rich and flexible database access. ADO has established a script writing model based on a web-way access to the database, which not only supports the core functions of any large database, but also supports the specific properties proprietary in many databases. ADO uses native data sources to access databases via ODBC. These databases can be a relational database, a textual database, a hierarchical database, or any database that supports ODBC. The main advantages of ADO are ease of use, high speed, occupancy memory and disk space, so it is very suitable for database access technologies as server-side. Compared to the CGI program accessed, it is multithreaded. When there is a lot of concurrent requests, the server's operational efficiency can also be maintained, and through the connection pool (Connection pool) technology and the full control of the database connection resource. Provide efficient connection and access with the remote database, but it also supports transaction (Transaction) to develop high-efficiency, highly reliable database applications. It is because using ADO needs to write scripts, so ADO can achieve more complex, more flexible database access logic. Currently, ADO includes seven objects such as Command, Connection, RecordSet and a dynamic Properties collection, and most of the database access tasks can be done through their combinations. The ASP script generally uses the ADO access database should use the Connection object to establish and manage the connection to the remote database; provide a flexible query using the Command object; use the RecordSet object to access the database query returned. These three are the most basic and core objects in ADO. The following example explains how to use these three objects to access the database. <% DIM STRDSN, STRSQL 'Define the used variable DIM CN, RS, CM, Objname, Objkucun, ObjPrice, Objunit, Objdate STRDSN = "filedsn = kucun.dsn" 'Establish DSN string SET CN = Server.createObject ("AdoDb.Connection") 'Creating an instance of the Connection object Cn.open strDSN 'Establish a connection to the data source specified by STRDSN Set cm = server.createObject ("adoDb.command") 'Creating an instance of a Command object Set cm.activeConnection = CN 'Specify CM to connect to the database established by CN cm.commandtext = "INSERT INTO inventory" (product name, inventory, price, unit, enter Values (?,?,?,?,?) 'Predefined SQL query commands cm.prepared = true 'Notification of the data source pre-processed the query command CM.Parameters.Append Cm.createParameter (Product Name ", 200,, 255) 'Define query parameters CM.Parameters.Append Cm.createParameter ("Inventory", 200,, 255) CM.Parameters.Append Cm.createParameter ("Inc.", 200, 255) CM.Parameters.Append Cm.createParameter ("Unit", 200,, 255) cm.parameters.Append Cm.createParameter ("Promoting Date", 200, 255) CM (Product Name) = "Grape" 'Assigning parameters cm ("stock") = 400 CM ("Imprid") = 0.8 CM ("unit") = "kg" CM ("purchase date") = # 95-6-12 # cm.execute 'Perform a predefined SQL query command according to the given parameter value CM ("Product Name") = "Persimmon" cm ("stock") = 300 CM ("Imprid") = 0.4 CM ("unit") = "kg" cm ("purchase date") = # 95-6-11 # cm.execute SET RS = Server.createObject ("AdoDb.Recordset") 'Creating an instance of the Recordset object strsql = "SELECT * FROM inventory" 'Establish a query command string RS.Open Strsql, CN 'Use the CN database connection to execute strsql definition query commands Set objName = rs ("Product Name") 'Save the returned column in the variable Set objkucun = rs ("inventory") Set objprice = rs ("Inc.") Set objunit = rs ("unit") Set objdate = rs ("purchase date")%> 'Use the cycle statement to output the query results in tabular form
|