Вы находитесь на странице: 1из 8

ADO Connection Object

To use a DSN connection:


Dim cn as ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "DSN=BIBLIO"
cn.Open
OR
Dim cn as ADODB.Connection
Set cn = New ADODB.Connection
cn.Open "DSN=BIBLIO"
At the end use:
cn.Close
Set cn = Nothing

The PROVIDER Property


The default provider is MSDASQL.1. This is the OLE DB provider for ODBC.
Since it is the default, it can be omitted.
PROVIDER=MSDASQL.1; DATA SOURCE=WidgetsDSN; USER ID=lquinn; PASSWORD=pword;

Example 1:
Dim con as ADODB.Connection
Set con = New.ADODB.Connection
'set the connection string property
con.ConnectionString = "Provider=MSDASQL.1; " _
& "Data Source=BiblioDSN"
'open the Connection object with the connection string set above
con.Open
OR
'set the connection string property with the open method
con.Open "Provider=MSDASQL.1; Data Source=BiblioDSN"
Example 2:
Dim con as ADODB.Connection
Dim rst as ADODB.Recordset
Set con = New ADODB.Connection
Set rst = New ADODB.Recordset

ado_connection_object.doc

Page 1 of 8

'open the recordset, creating a Connection object implicitly


rst.Open "Titles", "Provider=MSDASQL.1; Data Source=BiblioDSN " _
adOpenForwardOnly, , adCmdTable
'set con the to the Connection object that was just created by
'the Recordset object
Set con = rst.ActiveConnection

The EXECUTE method


The Execute method returns a recordset from an SQL statement.
Dim cn as ADODB.Connection
Dim rs as Recordset
Set cn = New ADODB.Connection
cn.Open "DSN=BIBLIO"
Set rs = cn.Execute("SELECT * FROM Authors WHERE [Year] > 1950")
Do While Not rs.EOF
List1.AddItem rs![Name]
rs.MoveNext
Loop
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
Execute can run SQL statements that do not return a recordset:
cn.Execute "DELETE FROM Authors WHERE Name = 'Jones'"
OR
cn.Execute "INSERT INTO Authors ([Name], [Year]) SELECT 'Jones', 1955"

ado_connection_object.doc

Page 2 of 8

Connections without DSN


Using the ConnectionString property
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Data
Source=C:\VB6\Biblio.mdb"
cn.ConnectionString = "Driver=Microsoft Access Driver
(*.mdb);DBQ=D:\VB6\Biblio.mdb"
The above connection string names the Driver rather than the DSN. For
Access, the DBQ parameter points to the database name.
The following examples connects to a SQL Server database:
cn.ConnectionString = "Provider=SQLOLEDB.1;Password=groovy:User
ID=lquinn:LOCATION=SQLSRV1;database=employee"

ado_connection_object.doc

Page 3 of 8

ADO Recordsets
Creating a recordset
In addition to creating a recordset with the Execute method, as
described above, recordsets have methods and properties that can be
used.
Dim rs AS ADODB.Recordset
Set rs = New ADODB.Recordset
Set the ActiveConnection property to an ADO Connection object:
rs.ActiveConnection = cn
OR
rs.ActiveConnection = "DSN=BIBLIO"
If you use the string value instead of a connection object, the
recordset will open its own connection to the database.

Creating an ADO Recordset


Const sSQL = "SELECT * FROM Authors WHERE [Name] Like 'S%'"
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
'Open a connection
Set cn = New ADODB.Connection
cn.Open "DSN=BIBLIO"
The OPEN method causes the recordset to be populated with data from the
SOURCE.
'Open a recordset
Set rs = new ADODB.Recordset
rs.ActiveConnection = cn
rs.Source = sSQL
rs.Open
'Print the authors' names
rs.MoveFirst
Do While not rs.EOF
Debug.print "Author = " & rs!Name
rs.MoveNext
Loop
'Close Everything
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

ado_connection_object.doc

Page 4 of 8

CursorType Property
adOpenForwardOnly
adOpenKeySet
adOpenDynamic
adOpenStatic

This property is fast but allows only forward


movement. It is the default value.
Your program can see some of the data changes made
by other users.
Your program can see all of the changes made by
other users.
This property provides a static picture of the
database; you cannot see others' changes.

adOpenKeySet is best for updates and complex operations.

LockType Property
adLockReadOnly
adLockPessimistic
adLockOptimistic
adLockBatchOptimistic

ado_connection_object.doc

Sets data in the recordset as read only


The record is locked while you are using it
The records are locked only when you call the
Update method
Updates multiple records at a time with the
UpdateBatch method

Page 5 of 8

Recordsets and Records


Updating Records
rs!Name = "Linda"
rs!Update
Unlike DAO, there is no rs.Edit method. Changing the fields alue
automatically places it in edit mode.

Adding New Records


rs.AddNew
rs!Name = "Linda"
rs.Update

By default, ADO Recordsets are read-only.


CursorType and LockType

To update them, change the

Deleting Records
rs.Open "SELECT Author, Books FROM Authors WHERE Books = 10"
Do While Not rs.EOF
rs.Delete
rs.MoveNext
Loop
Delete all records where Book = 10.

ado_connection_object.doc

Page 6 of 8

Recordset Filters
The filter property of the Recordset object allows you to filter a recordset
even after it has been created.
Using a search criteria string.
rs.Open "Authors", "DSN=BiblioDSN", adOpenKeyset
rs.Filter = "[LastName]='Twain'"
rs.Filter = "[Books]=12"
rs.Close

Recordset Sorting
Records can be sorted on an opened recordset.
A list of comma-delimited fields set which fields to sort.
DESC or ASC can be added after a space to any field in the list.
The sort property only works on client-side cursors.
rs.CursorLocation = adUseClient
rs.Open "Authors", "DSN=BiblioDSN"
rs.Sort = "[Books] DESC, Author"
rs.Close

Find Records
rs.Open "Authors", "DSN=BiblioDSN"
rs.Find "Authors='Dickens'"

Record Navigation
MoveNext and MovePrevious will set EOF and BOF to true. The record
pointer is not at a valid record.
rs.Open "Authors", "DSN=BiblioDSN"
rs.MoveFirst
rs.MoveNext
rs.MovePrevious
rs.MoveLast

ado_connection_object.doc

Page 7 of 8

Command Object
In MS Access, create a generic query with SQL:
PARAMETERS Letters Text;
SELECT *
FROM Authors
WHERE Author Like Letter+'*';
Running this query in Access will prompt for the Letter parameter.
To run this query from code, use a command object.
Dim cmd As ADODB.Command
Dim parmTemp As ADODB.Parameter
Set cmd.ActiveConnection = cn
cmd.CommandText = "LetterLookup"
cmd.CommandType = adCmdStoredProc
Set parmTemp = cmd.CreateParameter("Letter", adChar, adPaaramInput, 1)
cmd.Parameters.Append parmTemp
cmd("Letter") = "Q"
Set rs = cmd.Execute

CommandTypes
adCmdText
adCmdStoredProc
adCmdTable
adCmdUnknown

Runs a SQL query on the data source


Calls a stored procedure on a database
Specifies the name of the database table; used to
return the entire table
Unknown command type

ado_connection_object.doc

Page 8 of 8

Вам также может понравиться