300 likes | 433 Views
Web-based Programming Lanjut Pertemuan 5. Matakuliah : M0492 / Web-based Programming Lanjut Tahun : 2007. ActiveX Data Object (ADO). OLE DB and ADO Architecture The ADO Object Model Connection Object Command Object Recordset Object Record Object Stream Object Collections
E N D
Web-based Programming Lanjut Pertemuan 5 Matakuliah : M0492 / Web-based Programming Lanjut Tahun : 2007
ActiveX Data Object (ADO) • OLE DB and ADO Architecture • The ADO Object Model • Connection Object • Command Object • Recordset Object • Record Object • Stream Object • Collections • Connecting to Data Stores
ADO Basics • ActiveX Data Object (ADO) • The components that allow us to interact with data stores • One way to access data • OLE DB • The underlying technology • Interfaces between our programs and the source of the data
Application VC++ VB Script Java ADO OLEDB Data Store Directory Services E-mail RDBMS OLE DB and ADO Architecture
OLE DB and ADO Architecture • ADO is a COM component and can be used in any COM compliant language, such as Delphi, or scripting language that supports the Active Scripting interface. • OLE DB was not designed to be used in all language, so ADO sits on top of OLEDB, and provides an interface for those languages, such as VB and scripting languages, which can’t access OLE DB directly.
OLE DB and ADO Architecture • Two main reasons why do we need OLEDB and ADO • First, OLEDB and ADO are designed to give you access to data stores. Note that ‘data store’ does not always be ‘databases’. Although databases are still the most widely used form of data storage, they don’t necessarily contain all of your data. • Second, the rise of Internet applications, and the stateless nature of the Web. The older data access methods aren’t designed to handle data when they are not permanently connected to their store of data. OLEDB and ADO provide disconnected recordsets.
Errors Collection Connection Execute() ActiveConnection Active Connection Active Connection SourceRecordset ActiveCommand Command Recordset Record GetChildren() Execute() Open() Source Collection Collection Collection Fields Parameters Open() Save() Open() Stream The ADO Object Model
The ADO Object Model • The Connection Object • The Connection object is what enables us to connect to data stores. Can be constructed or stored commands (for example SQL command or a stored procedure) • We can specify which OLEDB Provider we wish to use, the security details used to connect to the data store, and any other details. • It is not necessary to explicitly create a Connection object to connect to a data store. We can create Command, Recordset and Record objects without a Connection object • ADO implicitly creates a Connection object if we do not create one ourself.
The ADO Object Model • The Command Object • The Command object is designed for running commands against a data store. • Connection objects has restrictions on its facilities for handling commands, whereas the Command object was specifically created for dealing with all aspects of commands. • Implicitly created when we run a command from the Connection object.
The ADO Object Model • The Recordset Object • Probably is the most commonly used object in ADO, since it is this object that contains the sets of data we extract from our data stores. • The Recordset is the object that holds set of records. • It allows us to change the data (additions, updates and deletions), move around the records, filter the records, and so on. • Contains a Fields collection, where there is a Field object for each field (column) in the recordset.
The ADO Object Model • The Record Object • The Record object is used to model a semi-structured recordset in ADO. • Semi-structured recordset is the sets of data where the columns may be different (number of column and data type) for each row.
The ADO Object Model • The Stream Object • The Stream object is used to access the contents of a node, such as an email message, or a Web page. • Have access to the actual contents of a file, or resource. • The Stream object is designed to handle binary data. • Another use for streams is XML, where we can access a set of data (structured or semi-structured) as a stream of XML.
The ADO Object Model • Collections • The Fields Collection Holds Fields object associated with a recordset or a record. • The Parameters Collection Used solely by the Command object, and identifies the parameters in stored commands. • The Errors Collection Contains details of the last ADO or OLE DB Provider error that was generated by a command, and can be only accessed through the Connection object.
Connection Properties Record Command Fields Recordset The ADO Object Model • The Properties Collection Designed to work with many different data stores, each of which might have different facilities
The ADO Object Model • We can use the same structure of code to loop through the collections: The syntax in VBScript is : For Each Object In Collection ‘ Do Something with object Next For example, to loop through the Fields collection of a Recordset object : For Each objField In rs.Fields Response.Write objField.Name & “<BR>” Next If you prefer JScript, then you can use the Enumerator object : for (objField = new Enumerator(rs.Fields); !objField.atEnd(); objField.moveNext()) Response.Write (objField.item().Name + ‘<BR>’);
The ADO Object Model Example of loop through Errors Collection : For Each objError In rs.ActiveConnection.Errors Response.Write objError.Name & “<BR>” Next Example of the OLE DB Provider for Jet allows us to access the Jet specific security properties: Set conDB = Server.CreateObject(“ADODB.Connection”) conDB.Open “DSN=NWind” conDB.Properties(“Jet OLEDB:Database Password”) = “LetMeIn”
The ADO Object Model • ADO Constants • The first method of reference the constants is to include them in your ASP file: • You can either copy the include file into your local directory, or reference it from the installation directory. One disadvantage of this method is that it makes your ASP page larger, since a whole host of constants are included, many of which you won’t typically need. • A better solution is to create a reference to the type library, which makes the constants available without having to include them in the file : <!-- #INCLUDE FILE=“adovbs.inc” --> <!– METADATA TYPE=“typelib” FILE=“C:\Program Files\Common Files\System \ado\msado15.dll” -->
Connecting to Data Stores • Several different ways connect to a data source: • A Connection String. Put the connection details in a string, or directly into the command that opens the data store. • A Data Link File. A file (with a .udl extension) that contains the connection details. • ODBC Data Source, or DSNs. Similar to Data Link files, but are only applicable to ODBC Data Sources. To be used in an ASP page, the Data Source must be a System Data Source.
Connecting to Data Stores • Connection Strings The connection string varies upon the Provider. The OLEDB Provider for ODBC is the default – therefore if you leave off the Provider= section, you will automatically use ODBC. • Microsoft Access If using an ODBC connection, without a DSN: Driver = {Microsoft Access Driver (*.mdb)}; DBQ=C:\MyData\thedata.mdb For native OLE DB Provider: Provider=Microsoft.Jet.OLEDB.4.0; Data Source= C:\MyData\thedata.mdb
Connecting to Data Stores • Microsoft SQL Server Using the Provider for ODBC: Driver = {SQL Server}; Server=server_name; Database=database_name; UID=user_name; PWD=user_password For example: Driver = {SQL Server}; Server=MUGGLE; Database=pubs; UID=arine; PWD=letmein For the native OLE DB Provider: Provider =SQLOLEDB; Data Source=server_name; Initial Catalog=database_name; User Id=user_name; Password=user_password For example: Provider =SQLOLEDB; Data Source=MUGGLE; Initial Catalog=pubs; User Id=arine; Password=letmein
Connecting to Data Stores • Microsoft Indexing Service The Indexing Service is only accessible through a native OLE DB Provider. The syntax is: Provider =MSIDXS; Data Source=catalog_name For example, using the Web catalog: Provider =MSIDXS; Data Source=Web
Connecting to Data Stores • ODBC Drivers In the example that use the OLE DB for ODBC, the Driver is shown in curly braces. For example: Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\MyData\database_name.mdb The name you should use be the exact name taken from the list of drivers when creating a new data source :
Connecting to Data Stores • Data Link Files Create a Data Link file by simply creating a blank text file and renaming the extension to .udl. Once you have the physical data link file, you can open it, either by double-clicking, or right-mouse clicking and selecting Open. You are presented with the following dialog:
Connecting to Data Stores The details of this screen change depending upon the Provider selected. Note that if you select the Allow saving password option, the password you type will be saved in plain text in the UDL file. To change the Provider you can select the Provider tab:
Connecting to Data Stores Alternatively, you can edit the file in a text editor : You can select that the UDL file is really just storing a connection string. To use the data link you simply specify the data link file when opening the connection: conDW.Open “File Name=C:BI\Batches.udl”
Connecting to Data Stores • ODBC Data Sources ODBC Data Sources (commonly called Data Source Names, or DSNs) are set up through the Data Sources option on the Administrative Tools menu. Previous versions of Windows had this as an applet in the Control panel. To access a DSN from an ASP page you must make sure that the DSN is set up as a System DSN. This simply involves selecting the System DSN tab on the Data Source Administrator, and then selecting the Add.. button: Once a DSN is set up, you use the DSN= attribute of the connection string. For example: conDW.Open “DSN=pubs“
Using Include Files • Using include files to contain connection strings gives a central place to store the connection details for a number of ASP pages. • In your ASP pages, add this on the top of the page: This saves you having to type the connection details for each ASP page, and lets you change the connection used for the entire site from one central location. <% strConn = “Provider=SQLOLEDB; Data Source=MUGGLE; “ & _ “Initial Catalog=pubs; User Id=Arine; Password=letmein” Response.Write “” %> Connection.asp <!-- #INCLUDE FILE=“Connection.asp” -->
Using Connection State • Storing the connection string in an Application variable is quite a common trick too, and equally as effective as using an include file. For example, you could add the following to your global.asa file: Sub Application_OnStart() strConn = “Provider=SQLOLEDB; Data Source=MUGGLE; “ & _ “Initial Catalog=pubs; User Id=Arine; Password=letmein” Set Application (“ConnectionString”) =strConn End Sub In your ASP pages you can then use the following : Set conDW = Server.CreateObject(“ADODB.Connection”) conDW.Open Application(“ConnectionString”)
Connection Syntax • If using an explicit Connection object, use the Open method: connection.Open [ConnectionString], [UserID], [Password], [Options] The arguments are as follows: Connecting asynchronously is no use in an ASP environment, since scripting languages can’t receive events from ADO.
Connection Examples • To open a connection use the Open method of the Connection object. For example, assume that strConn contains a valid connection string: • Alternatively, use the ConnectionString property: • There is no difference between the two, and if you use the former method, then the ConnectionString property is filled in for you. Set conPubs = Server.CreateObject(“ADODB.Connection”) conPubs.Open strConn ‘ Some processing conPubs.Close Set conPubs = Server.CreateObject(“ADODB.Connection”) conPubs.ConnectionString = strConn conPubs.Open ‘ Some processing conPubs.Close