15. Common ASP ActiveX Components

Source: Internet
Author: User
Tags web database microsoft iis
When you use ASP to write server applications, you must rely on ActiveX components to provide powerful web application functions. For example, you need to connect to the database and perform online operations on the database, after introducing the ad rotator component in the previous article, this article will introduce you to the usage of some other commonly used ASP ActiveX components.

Recently, many people have asked me if ASP can only run on Microsoft IIS and can operate on non-nt platforms? I have answered this question many times: I have heard of some software that can be supported, but I have never seen it. However, some enthusiastic friends still wrote to ask questions tirelessly, so with the kindness of my friends, I visited ASP-related sites and was surprised to find that, originally, ASP can run on other non-nt platforms. Therefore, at the beginning of this article, I will give a brief introduction to my friends about how to use ASP on non-nt platforms.

To develop and run an ASP application on a non-NT platform, we can rely on a set of third-party software named instant ASP. Its advertisement slogans are very attractive: "ASP anytime, anywhere ", I think all ASP developers will be excited when they see such slogans. This software developed by halcyon allows you to run it on any operating platform without having to repeat the original ASP application! This not only saves a lot of development time, but also makes ASP a cross-platform Internet, Intranet, or excompute application. Instant ASP is actually a Java-based application. Therefore, you can use it to run Web-Based ASP applications on any platform, the following table lists the operating platforms supported by the current version of instant ASP.

Even more surprising is that instant ASP not only provides an ASP operating environment, but also provides more powerful and practical functions than ASP applications on the market, it combines ActiveX components with Enterprise Java Beans or CORBA-compliant objects to Make ASP more widely used. It also provides a powerful function to access various databases through the ADO interface and generate dynamic pages. For developers can use their good at programming languages or tools such as: Visual Basic, JScript, VBScript, C ++, Java, HTML, Delphi, MS Visual InterDev and so on for development. I will not deploy the installation and operation of instant ASP here. If you are interested, you can go to its website and check halcyonsoft.com. You can also download a trial version for free.
After the author introduced how to use the ad rotator component in the previous article, let's take a look at some other common ASP components.

I. Database Access Component

The most common and practical task we use web applications on a Web server is to access the database on the server. The built-in database access component of ASP allows us to easily access the information stored in the database on the server or other table-based data structures through ActiveX Data Objects (ADO. ADO is the most effective and simple method to operate the databases currently supported by Microsoft. It is a powerful data access programming mode, this allows you to extend the programmable attributes of most data sources directly to your Active Server Page. You can use ADO to write compact and concise scripts to connect to Open Database Connectivity (ODBC)-compatible databases and ole db-compatible data sources, so that ASP programmers can access any ODBC-compatible databases, includes ms SQL Server, access, Oracle and so on. If you are a script writer who has a certain understanding of database connections, you will find that the ADO command statements are not complex and easy to understand. Similarly, if you are an experienced database programmer, you will have a correct understanding of ADO's advanced language-independent and query processing functions. If you are familiar with VB Database Programming, you will find that ADO and rdo (Remote Data Objects) have some similarities. However, it is said that the access speed of ADO is faster and the memory needs to be smaller.
The following describes how to use the ASP database access component to connect to and operate a web database through ADO.

Step 1: Specify the database to be connected. There are two methods: DSN and DSN-less.

DSN (Data Source Name): Create a system data source name. The method is as follows:

1. Click Start and select Settings Control Panel.

2. Double-click the icon "32-bit ODBC". A dialog box is displayed, with the label "system DSN" selected"

3. Click "add" to add a DSN entry, select "Microsoft Access drive" and confirm.

4. Enter the DSN you want to specify in the "Data Source Name" column, and click "select" to select the database storage location. You can select the database by "Browse.

5. After completing the preceding steps, specify the DSN in the ASP program as follows:

<% Connstr = "DSN" %>

DSN-less: Another method is to directly specify the location of the database file in the ASP file, without the need to establish a DSN. Because many companies do not have their own web servers, their websites are often stored on remote virtual servers. Therefore, it is difficult to set up and modify DSN. Using the DSN-less method to directly specify the location of the remote database solves this problem. The method is as follows:

<%
Connstr = "DBQ =" + server. mappath ("database/source. mdb") + "; defaultdir =;
Driver = {Microsoft Access Driver (*. mdb)}; driverid = 25; fil = MS access;
Implicitcommitsync = yes; maxbuffersize = 512; maxscanrows = 8;
Pagetimeout = 5; safetransactions = 0; threads = 3; usercommitsync = yes ;"
%>

After specifying the database to be connected, you can connect to and open the database using the following methods:

<%
Set conn = server. Createobject ("ADODB. Connection") Conn. Open constr
%>

Step 2: Specify the SQL command to be executed. You can use recordset.

After connecting to the database, you can perform operations on the database, such as querying, deleting, and updating. These operations are completed using SQL commands, for example, in the database table datebase, query all records whose names contain ":

<%
SQL = "select * From datebase where name like 'a % '"
Set rs = conn. Execute (SQL)
%>

Although the connection object simplifies database connection and query tasks, there are still many shortcomings in the connection object. Specifically, the connection object for retrieving and displaying database information cannot be used to create scripts. You must know exactly the changes to the database before using the query to implement changes. ADO provides A recordset object for retrieving data, checking results, and changing databases. As its name implies, the recordset object has many features that you can use to retrieve and display a set of database rows, that is, records, based on your query restrictions. The recordset object maintains the position of the record returned by the query, allowing you to scan the results one by one. You can scroll and update records based on the pointer type attribute settings of the recordset object. Database pointers allow you to locate specific items in a group of records. Pointers are also used to retrieve and check records and then perform operations based on these records. The recordset object has some attributes that can be used to precisely control pointer behavior and improve your ability to check and update results.
Recordset is used as follows:

Set rs = server. Createobject ("ADODB. recordset ")

Rs. Open SQL command, Conn, 1, 1' read

Or

Rs. Open SQL command, Conn, 1, 3' add, modify, or delete
Step 3: Use the recordset attribute and method and display the execution result.

Using the preceding command, we created a data-containing cursor (recordset) "Rs". In fact, the cursor is stored in an array of similar records and fields in the active memory, when a cursor is created through the recordset component, it obtains a dataset from the data provider and uses it to enrich the cursor. we can imagine that the recordset generated by ADO is a record like a workbook, it has a row record. At any time, a row is its current row, and the recordset field is represented by the field set of recordset. The following lists the attributes and methods of the created recordset object (cursor:
Rs. Fields. Count: Total number of fields in the recordset object.

RS (I). Name: name of the I-th field. I starts from 0 to Rs. Fields. Count-1.

RS (I): reads data from the I field. I starts from 0 to Rs. Fields. Count-1.

RS ("field name"): reads data from a specified field.

Rs. recordcount: Total number of data records in the cursor.

Rs. EOF: indicates whether the last record has been specified.

Rs. movenext: Move the indicator to the next record.

Rs. moveprev: Move the indicator to the previous record.

Rs. movefirst: Move the indicator to the first record.

Rs. movelast: Move the indicator to the last record.

Rs. Close: closes the recordset object.

For more information about ado, I will explain it in detail in the future.

Ii. Content linking component

If your website has a series of associated pages, the content linking component will be very suitable for your needs. Instead of creating a directory table on these pages, you can also create a dynamic connection between them and automatically generate and update the directory table and the navigation links of the previous and subsequent web pages. This is an ideal choice for listing online newspapers, Electronic Reading websites, and Forum emails.

The content linking component creates a nextlink object for managing the URL list. To use the content linking component, you must first create a content linking list file. The content linking component reads the file to obtain information about all pages that we want to link. In fact, this file is a plain text file with the following content:
Page1.htm one
Page2.htm two
Page3.htm three
Page4.htm four
Page5.htm five
Page6.htm six

Each line of the text file is in the following format:

URL description comment

URL is a page-related hyperlink address. Description provides text information that can be used by the hyperlink. Comment contains comments that are not interpreted by the content linking component, it serves as a comment in a program. Description and comment parameters are optional.

Let's take a look at how to use the content linking component:

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> online e-book treasure </title>
</Head>
<Body>
<P> online e-books navigation
<%
Set link = server. Createobject ("mswc. nextlink ")
Count = link. getlistcount ("nextlink.txt ")
Dim I
For I = 1 to count
%>
<Ul> <li> <a href = "<% = link. getnthurl (" nextlink.txt ", I) %>">
<% = Link. getnthdescription ("nextlink.txt", I) %> </a>
<% Next %>
</Body>
</Html>

In the above Code, we first use the getlistcount method to determine the number of items in the nextlink.txt file, and then use the loop statement, use the getnthurl and getnthdescription methods to read the content stored in the nextlink.txt file one by one and display it to the client browser.

The following lists all the methods available for the content linking component. getlistcount (File) counts the number of items linked to the content link list file.

Getnexturl (File) gets the URL of the next page listed in the content link list file.

Getpreviusdescription (File) gets the description line of the previous page listed in the content link list file.

Getlistindex (File) gets the index of the current page in the content link list file.

Getnthdescription (file, index) gets the description of page N in the content link list file.

Getpreviusur (File) gets the URL of the previous page listed in the content link list file.

Getnextdescription (File) gets the description of the next page in the content link list file.

Getnthurl (file, index) obtains the description of page N in the content link list file.

After we create a general navigation page for the website, we also want to add a navigation superchain of "Previous Page" and "next page" to each page, next, let's take a look at how to implement navigation superchains.

Because your website may contain thousands or more pages, it is impossible for us to write ASP code that implements navigation hyperlink in every page. Therefore, we must use the server mentioned in the previous articles to tolerate SSI, so as to avoid a large number of repeated jobs. Please refer to the following example:
<! -- # Include file = "nextprev. Inc" -->

You only need to put the code on each page. The content of the nextprev. inc file is as follows:

<%
Set link = server. Createobject ("mswc. nextlink ")
Count = link. getlistcount ("nextlink.txt ")
Current = link. getlistindex ("nextlink.txt ")
If current> 1 then
%>
<A href = "<% = link. getpreviusurl (" nextlink.txt ") %>"> previous page </a>
<%
End if
If current <count then
%>
<A href = "<% = link. getnexturl (" nextlink.txt ") %>"> next page </a>
<% End if %>

Today's study is over again. Here I would like to thank all the friends who care about my articles again. Your letter has inspired me a lot, however, please do not refer to me as a "teacher". In fact, I am still learning and exploring like you. If you have any questions, we can discuss them together, I am sure there are still many shortcomings or vulnerabilities in my article. I hope that you can submit them to me in time. ASP has been widely used in Chinese websites recently, so with the enthusiastic help of ASP experts in China, I decided to write some ASP application instances as a companion article in this article. This will provide you with some practical things. Please stay tuned.

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.