7 |
The LA Fox Developer Newsletter
|
February 2000
|
ADO Quickstart
(Con’t from page 6)
ProgID: ADODB.Connection
The purpose of the Connection object is to provide access to a data store. To illustrate, the following code creates an ADO Connection object:
oConnection
=
CreateObject(”adodb.connection”)
Once an ADO Connection object has been created, you can access its data store. An active connection can be established by providing a few pieces of key information and invoking the Open() method of the Connection object. The following code opens a connection to the Visual FoxPro TasTrade database:
oConnection.Open(”TasTrade”)
Alternatively, the following code accesses the SQL Server Northwind database:
oConnection.Open("Northwind", "sa","")
These
two
examples work with the OLE DB Provider for ODBC drivers. Different OLE DB providers can be used as well. The following example sets some common properties of the Connection object and uses the OLE DB Provider for SQL Server:
With oconnection
.Provider
=
“SQLOLEDB.1”
Open
EndWith
Using and creating data link files
The syntax of the ConnectionStnng property appears complicated. Fortunately, you don’t have to code this by hand. When you install the Microsoft Data Access Components (MDAC), you can create a data link file.
To create a data link file:
1.Right-click your desktop and choose New\Microsoft Data Link from the pop-up menu.
2.Specify a name for the file.
3.Right-click and select Properties to modify the file properties.
4.ln the Properties dialog box, click the Provider tab, and choose a provider. The OLE DB Provider for ODBC is the default choice. For this example, select the OLE DB Provider for SQL Server.
|
5.Click the Connection tab.
6.Specify the name of the server, your user name and password, and the name of the database you wish to connect to.
7.Open the UDL file in Notepad.
Now, it is just a matter of copying and pasting the information.
Alternatively, you can use the file itself:
oConnection.Open(”File Name=c:\temp\test.udl”)
ADO recognizes four arguments in the ConnectionString:
+
File Name: Specifies the name of a UDL file to use.
+
Provider: Specifies the name of an OLE DB provider to use.
+
Remote Provider: Specifies the name of a provider to use with Remote Data Services (RDS).
|
Page 7
|
7 |