ADO: Difference between revisions
mNo edit summary |
|||
Line 56: | Line 56: | ||
==== Connection properties ==== | ==== Connection properties ==== | ||
For details please see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681546%28v=vs.85%29.aspx | For details please see [https://msdn.microsoft.com/en-us/library/windows/desktop/ms681546%28v=vs.85%29.aspx Connection Object Properties, Methods, and Events]. | ||
=== Populate the recordset === | === Populate the recordset === |
Revision as of 19:12, 27 February 2016
Most of the following text was taken from Wikipedia, connectionstrings.com:
Microsoft's ActiveX Data Objects (ADO) is a set of Component Object Model (COM) objects for accessing data sources. As part of MDAC, it provides a middleware layer between programming languages and OLE DB (a means of accessing data stores, whether they be databases or otherwise, in a uniform manner). ADO allows a software developer to write programs that access data without knowing how the database is implemented; developers must be aware of the database for connection only. No knowledge of SQL is required to access a database when using ADO, although one can use ADO to execute SQL commands directly. The disadvantage of the latter is that it introduces a dependency upon the type of database used.
ADO is positioned as a successor to Microsoft's earlier object layers for accessing data sources, including Remote Data Objects (RDO) and Data Access Objects (DAO). ADO was introduced by Microsoft in October 1996.
There are many ways to retrieve, add and change data using ADO. This tutorial only describes a fraction of what can be done with ADO:
- How to read data
- How to use the OLEDB provider, ODBC and other providers are not part of this tutorial
- To open the connection a connection string is used. Setting the properties of the connection object to describe the connection is not part of this tutorial
- The command to retrieve data is specified with the Open method for the RecordSet object. The Command object is not part of this tutorial
Microsoft ActiveX Data Objects Extensions for Data Definition Language and Security (ADOX) is an extension to the ADO objects and programming model. ADOX includes objects for schema creation and modification, as well as security. Because it is an object-based approach to schema manipulation, you can write code that will work against various data sources regardless of differences in their native syntaxes.
ADOX is a companion library to the core ADO objects. It exposes additional objects for creating, modifying, and deleting schema objects, such as tables and procedures. It also includes security objects to maintain users and groups and to grant and revoke permissions on objects.
How does ADO, OLEDB and ODBC fit together?
This chart shows the "big picture" so you can see how this all fits together (Picture taken from http://community.qlik.com/thread/106540).
Internals
ADO is made up of four collections and twelve objects. For details please see page → ADO Internals.
Usage
Some basic steps are required in order to be able to access and manipulate data using ADO:
- Create a connection object to connect to the database
- Create a recordset object in order to receive data in
- Open the connection
- Populate the recordset
- Do all the desired searching/processing on the fetched data
- Close the recordset
- Close the connection
Create a connection object
In AutoIt this is simply done by:
Global $oConnection = ObjCreate("ADODB.Connection")
Create a recordset object
In AutoIt this is simply done by:
Global $oRecordSet = ObjCreate("ADODB.Recordset")
Open the connection
To open the connection you have to build a connection string.
When your application connects to a database or a data file you let ADO utilize a provider to do the job for you. The connection string contains the information that the provider needs to know to be able to establish a connection to the database or the data file. Because there are different providers and each provider has multiple ways to make a connection there are many different ways to write a connection string. It's like the address when sending a regular mail. Depending on the origin and destination and who is going to make the transport you need to write down the address in different ways.
Connection string
The connection string is really just a string inside the application you pass on to the connection object. The property is passed through the Open function of the connection object.
For details please see page → ADO ConnectionString.
Connection properties
For details please see Connection Object Properties, Methods, and Events.
Populate the recordset
The ADO Recordset object is used to contain the set of data extracted from a database or file and is composed of records (rows) and fields (columns).
The Recordset object is the heart of ADO. Via this object, we can select desired data. Equally important is the ability to move around inside the database. However, the functionality of the provider may impose limitations.
RecordSet.Open(Source, Connection, CursorType, LockType, Options)
The Open method is called on a Recordset object to open a cursor which gives you access to the records contained in the base table, the results from a query (used in the examples of this tutorial), or a previously saved Recordset.
For details please see page → ADO RecordSet Populate.
Search/process the fetched data
ADO provides methods and properties to
- move around in the RecordSet
- search for rows
- write the recordset to a 2D array or a single record to a string
- save the recordset to a file
- sort the records
- delete rows from the RecordSet
and much more.
For details please see page → ADO RecordSet Process.
Close the recordset
Close the recordset and assign a new value to the object to release it:
$oRecordSet.Close()
$oRecordSet = 0
Close the connection
Close the connection and assign a new value to the object to release it:
$oConnection.Close()
$oConnection = 0
Examples
- IBM DB2
- Microsoft Access
- Microsoft Excel
- Microsoft Project
- Microsoft SQL Server
- MySQL
- Oracle Database Server
- SQLite
- Text files
Tools
Some (AutoIt) tools to make life easier can be found on page → ADO Tools.
Internal links
Links to ADO related threads on the forum:
- ADO (by mLipok) - A modifed version of the _sql.au3 UDF to access all kind of databases.
- ActiveX Data Objects (by GreenCan) - DSN-Less Database connection demo.
- ADODB (by spudw2k) - ADODB Example.
- MS SQL (by TheLuBu) - MSSQL.au3.
- MS SQL (by ChrisL) - _SQL.au3. ADODB Connection.
- MySQL (by cdkid) - MySQL relational database management system UDF.
- MySQL (by James) - MySQL ODBC Connector.