Recommended: ASP tip example: About the program for operating forms The following is the referenced content: Inserting Form content into Database with ASP. <% ' -- Loader.asp -- ' -- version 1.5.2
8.3.3 Use connection statusSaving connection strings into application variables is a common trick, and it works as well as using an included file. For example, you can add the following code to the global.asa file:
Sub Application_OnStart()
strConn = Provider=SQLOLEDB; Data Source=WATCHER; & _
Initial Catalog=pubs; User Id=davids; Password=whisky
Set Application(ConnectionString) = strConn
End Sub
In the ASP page, you can use the following code:
Set conPubs = Server.CreateObject(ADODB.Connection)
conPubs.Application(ConnectionString)
From a personal point of view, I prefer to use the included file method because I have written many different examples of connecting to various servers and databases. Using the application method will mean that you have to close the browser every time you restart the application. Readers can use any method they like, and they don't make any difference in speed.
For examples in this section of this book, a connection.asp file with the connection string will be used as a include file.
8.3.4 Connection Syntax
What is described above is a related theory. What should I do when I really want to connect to a data store? If you use an explicitly defined Connection object, you can use the Open method, and its syntax is as follows:
connection.Open [ConnectionString], [UserID], [Password], [Options]
The parameters are shown in Table 8-1:
Table 8-1 Parameters and descriptions of Open method
parameter
illustrate
ConnectionString
A string containing the connection details. It can be the name of the ODBC DSN, the name of the data link file, or the real connection details
UserID
The name used by the user during the connection. Override any username provided in the connection string
Password
User password. Overwrite any passwords provided in the connection string
Options
It can be adAsyncConnect, specifying that the connection is established asynchronously. Ignore this parameter and establish a synchronous connection
Asynchronous connections are not used in ASP environments because scripting languages cannot receive events from ADO.
8.3.5 Connection example
Here are a few examples, here assume that strConn contains a valid connection string.
To open a connection, use the Open method of the Connection object. For example:
Set conPubs = Server.Connection(ADODB.Connection)
conPubs.Open strConn
' Some processing
conPubs.Close
You can also use the ConnectionString property:
Set conPubs = Server.CreateObject(ADODB.Connection)
conPubs.ConnectionString = strConn
conPubs.Open
' Some processing
conPubs.Close
There is no difference between these two implementation methods. If the former method is used to implement the connection, the ConnectionString property is also assigned.
It is worth noting that once a connection is established with the data store, ADO may change the ConnectionString property value. Don't worry, ADO only fills in some extra attribute values.
8.3.6 Connecting to the buffer pool
Connection pools always confuses many people, but the principle is actually very simple. When a connection is closed, the connection is closed in the case of the user (and ADO). But in fact, OLE DB does not close the connection, but just put it into the inactive connection buffer pool. Any time a user (or others) opens a connection, the OLE DB first detects whether there is a connection with the same connection details in the connection pool. If so, this connection will be obtained directly from the buffer pool. If not, create a new connection for the user. To avoid wasting resources, the connection is cleared from the buffer pool after a default period of time.
So, what are its advantages? Opening a connection is probably one of the slowest operations performed, and the connection buffer pool allows the user to connect to the data store again without having to recreate the connection. This is particularly important for Web sites that continuously open and close a large number of connections.
For ODBC connections, the connection buffer pool is controlled by the ODBC Data Source Administrator. For OLE DB, the connection buffer pool (or session buffer pool) cannot be changed.
It must be noted that the connection buffer pool is not a connection share. A connection can only be used again after being closed by the customer.
Housekeeping
In order for the connection buffer pool to take effect, it is necessary to ensure that housekeeping is in an orderly state. This includes closing Connection objects in time so that they can be reused back to the buffer pool. You may think that constantly opening and closing connections is expensive to the system, but you have to measure scalability - your application may be used by many people, and OLE DB is very good at managing connection resources.
The general principle is: establish the connection as late as possible, and at the same time close the connection as early as possible, so as to ensure the shortest time period for the connection to open.
8.4 Record Set
As mentioned earlier, recordsets are the most commonly used objects in ADO, which is not surprising. After all, they contain data. However, there is more to the record set than you think, and it is important to know how the data is saved and processed, as this provides more reference for choosing which record set to use.
Record sets have different types, and there are differences in some small places, which can easily lead to mistakes. The first thing you need to seriously talk about is the concept of the cursor.
8.4.1 Cursor
Cursor is a concept that confuses many people, but it is actually very simple.
The cursor is used to manage the record set and the current position of the record set, which is processed by the current record pointer.
Isn't this what the Recordset object does? Yes, but the record set also relies on its cursor. This still does not answer the question of what the cursor is, so let’s first look at a record set, as shown in Table 8-2:
AU_ID
AU_LNAME
AU_FNAME
PHONE
172-32-1176
White
Bob
408 496-7223
219-46-8915
Green
Marjorie
415 986-7020
238-95-7766
Carson
Cheryl
415 548-7723
267-41-2394
O'Leary
Michael
408 286-2428
274-80-9391
Straight
Dean
415 834-2919
341-22-1782
Smith
Meander
913 843-0462
There are six rows and four columns here. Open a record set, and the current record is the first record, that is, the record of Bob White. What to use to identify the current record? Use the current record pointer. So how to deal with this pointer? When it is necessary to move to the next record or other record, it is achieved through the cursor. When accessing the field of the current row, the cursor knows which row is currently located, so it can return the correct value. If you try to move out of the last row of the record set, the cursor will also process it.
A good way to understand a cursor is to imagine the cursor as a window that can be moved within the record set. This window is as tall and long as a single row in the record set, so you can only see one row of data values at a time. When you move to another record, the window moves along.
Maybe you think this is pretty simple, but it does matter because what you can do with a cursor is determined by the type of cursor.
Share: Routine: Use ASP to determine whether the file address is valid The following is the quoted content: <% Response.Write(<head><style><!--span{ font-size: 9pt }--></style></head&g