ASP calls in the acess database

xiaoxiao2021-03-06  15

When one day, your mood suddenly became very bad, but it was coincident. In your guestbook, your netizens left a very excited, so you quickly regained a happy mood. But soon, you have changed again, but this time is not last time, because no one gave you encouragement, so you naturally want to take a look at the last sentence to you very excited. At this time, you will think: If my message board has a message query function! In order to reach your wish, I decided to explain the message book program with the message query function in this chapter. Before explaining, let us still learn the Command object that will be used in the source program.

Let us first let us first understand the properties and methods provided by the Command object and their corresponding functions.

ActiveConnection Properties - Create a link relationship with the Connection channel CommandText property - Specify data query information CommandTimeout property - Start the maximum time COMMANDTYPE attribute that allows you to continue execution - Specify the type of data query information - Specify Does the data query information To compile CreateParameter methods first - create a new parameter object Execute method - ask a data query for the database

The Command object is also a member of the ADO object collection. It is mainly used to control the request information sent by the database, tell the database: "What is the data in which data? Do you want to meet what limit? Please All the data that I meet me is all stored in the Recordset object! "So, the function of the Command object is to perform SQL (Structured Query Language Structure Query language, is used to organize, manage, and" data in the computer database A tool for retrieving; it is a specific type of database-relational database. If you are a more careful person, you must remember that I have used the SQL command in the third chapter, so you will send me: "You didn't use the Command object at the time?"

Indeed, there is no COMMAND object we can also execute the SQL command. We can also perform SQL commands using the Connection object or using the RecordSet object. The method is as follows:

SET RS = conn.execute (SQL Command) 'Execute the Select SQL command using the Connection object and then specifies the result to the Recordset object. The conn.execute SQL command 'uses the Connection object to execute the SQL command for the data operation. SET RS = Server.createObject ("AdoDb.Recordset") RS.Open SQL command, conn

'Set up a RecordSet object first, then execute the SQL command to select the data. How is the command object to execute the SQL command? Please see: set cmd = server.createObject ("adoDb.command") set cmd.activeconnection = conncmd.commandtext = SQLSET RS = cmd.execute

'It is visible to execute the SQL command with the Command object, first set the Connection object and the SQL command to the ActiveConnection and CommandText property of the object Command, and then execute the cmd.execute function.

From the above we can know that the original use of the RecordSet object and using the Command object to execute the SQL command, the essence is the same. In this case, then why should we use the Command object? To illustrate this problem, I use two different ways to add a message query function for the message board, where the query "Message Board has been in the past five days message" and "Message Record Date Query" is to use the Command object to execute the SQL command. The Query of the "Message Name Query" and "The latest ten message record" is to use the Recordset object to execute the SQL command. Telling a lot should also get into the topic, we first learn to use the Recordset object to implement the "Message Name Query" and "Latest Ten Message Records" queries. We are also described below by way of steps.

Step 1: First we have to design an interface of a message query (Datesearch.asp), as shown below:

Step 2: Use the RecordSet object to execute the SQL command to implement the "Latest Ten Message Record" queries. Set conn = server.createObject ("adoDb.connection") dbpath = server.mappath ("book2.mdb") conn.open "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & dbpathsql = "SELECT TOP 10 * from Guestbook Order By ID DESC "

Here, "Order By ID" is used to sort by "ID" field from large to small. Because I set the data type of the id field to "Auto Number" in the database, the id recorded ID will be sorted from small to large, so the latest ten messages to query the last ten ID records. SET RS = Conn.execute (SQL)

Using the Recordset object to execute the SQL command, the principle of the query of the "The Name" of the "Message Name" is consistent with the above, and the program is as follows:

Namesearch = Request.form ("Name") nameSearch = Replace (Namesearch, "'", "'") "Remember the instructions when I introduce the SQLSTR function in Chapter 3? Forgot to go back and see. Set conn = server.createObject ("adoDb.connection") dbpath = server.mappath ("book2.mdb") conn.open "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & dbpathsql = "SELECT * From GuestBook Where Name = '"& NameSearch &" ORDER BY ID DESC "SET RS = Conn.execute (SQL)

Step 3: Design the web page of the query results (Search.asp).

转载请注明原文地址:https://www.9cbs.com/read-46108.html

New Post(0)