ASP 3.0 advanced programming (38)

Source: Internet
Author: User
8.3.3 connection status
Saving a connection string to an application variable is a common technique, which is as effective 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
On the ASP page, you can use the following code:
Set conPubs = Server. CreateObject ("ADODB. Connection ")
ConPubs. Application ("ConnectionString ")
From a personal perspective, I prefer the file inclusion method, because I have written many different examples of connecting to various servers and databases. Using the application method means that the browser must be closed each time to restart the application. Readers can use any of their favorite methods and there is no difference in speed.
The example in this section of this book uses a connection. asp file that contains a connection string as an inclusion file.
8.3.4 connection syntax
The above describes the relevant theories. What should I do when I really want to connect to the data storage? If you use an explicitly defined Connection object, you can use the Open method. 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 the Open method
Parameter
Description
ConnectionString
A string that contains the connection details. It can be the name of odbc dsn, the name of the data link file, or the actual connection details.
UserID
The name used by the user during the connection. Overwrite any user name provided in the connection string
Password
The user's password. Overwrite any password provided in the connection string
Options
It can be adAsyncConnect, specifying an asynchronous connection. Ignore this parameter to establish a synchronization connection.
Asynchronous connections are not used in ASP environments because the scripting language cannot receive events from ADO.
8.3.5 connection example
The following are examples. Here we 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 attribute:
Set conPubs = Server. CreateObject ("ADODB. Connection ")
ConPubs. ConnectionString = strConn

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.