Integrating Web & DBMS Gianluca Ramunno < ramunno@polito.it > english version created by Marco D. Aime < m.aime@polito.it > Politecnico di Torino Dip. Automatica e Informatica Open Database Connectivity (ODBC) technology for DBMS access that is part of Microsoft Data Access Components (MDAC) C++ low level interface included in Microsoft O.S. allows accessing data from any application in DBMS-independent way inserts a middleware (the database driver) between application and DBMS uses SQL as data access & manipulation language used by DAO (application level interface) ODBC A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-1
Open Database Connectivity (ODBC) application driver manager driver driver driver data source data source data source SqlServer oracle xls ODBC Open Database Connectivity (ODBC) application driver manager (DLL) loads the drivers upon application request driver (DLL) executes the ODBC calls received by the Driver Manager the application sees the driver manager and the drivers as a single component data source DBMS, network, OS hosting the DBMS ODBC A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-2
Driver in single-tier configuration the same node hosts: application driver manager driver software for data access (data access software) data storage two possible configurations: stand alone network ODBC Driver in single-tier configuration stand-alone network ODBC A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-3
Driver in multiple-tier configuration the client node hosts: application driver manager driver the server node hosts: software for data access (data access software) data storage ODBC Driver in multiple-tier configuration ODBC A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-4
Driver multiple-tier with gateway features an additional gateway node forward the requests to the data access software ODBC technology (MDAC) included in Microsoft OSes evolution of ODBC COM (Component Object Model) interface to provide applications with uniform access to data from different sources relational DBMSes not relational DBMSes (new) file system, mail system, spreadsheets, directory, not limited to SQL C++ low level interface A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-5
consumer applications accessing data through the interface provider intermediary providing the data implements methods not all providers have the same capabilities; it depends on which methods they implement consumer application or tool interface ODBC File Native provider A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-6
application or tool interface OLE.ODBC interface ODBC Driver Manager ODBC Driver interface Text File interface database database ODBC provider simple provider native provider provider example Jet 4.0 is the Access engine native provider application proprietary interface Jet 4.0 database native provider A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-7
(ActiveX Data Object) middleware with object oriented interface evolution of DAO makes the interface accessible to other languages than C++ it is an application level wrapper for can be used with different languages by complied user programs (VisualBasic) macros inside applications like MS-Office (VisualBasic for Application - VBA) WSH: script console (Jscript, VBscript) ASP pages in IIS (Jscript, VBscript) application or tool interface OLE.ODBC interface ODBC driver manager ODBC driver inteface text file interface database database ODBC provider simple provider native provider A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-8
: object model Connection Recordset Fields Field Command Properties Parameters Property Parameter Properties Property Errors Error objects Connection object represents a connection to a specific data source Command object used to define a specific command, like a SQL query, to a data source Recordset object represents the set of records belonging to a whole table or resulting by a query composed by rows (record) and columns (field) A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-9
Record object represents a data row objects corresponds to a single record of a recordset Field object represents a data column corresponds to a column of a recordset Parameter object represents a parameter associated with command object (e.g. parameters for a stored procedure) objects & collections Error object details on an error occurred during a failed operation with the provider Fields collection contains all the Field objects of a recordset Errors collection contains all the Error objects generated during a failed operation with the provider Parameters collection contains all the Parameter objects of a command A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-10
Connection Provider Connection Open ConnectionString Execute Mode properties methods Close Connection Mode DB opening mode read-only is default for read-write con.mode = admodereadwrite Execute (stringsql) returns a read-only RS with forward-only cursor if other properties are needed, d use rs.open instead of con.execute A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-11
Recordset Open Close EOF Index proprietà p Recordset AddNew Delete MoveFirst MoveLast MoveNext MovePrevious Update metodi Recordset: open method opening a recordset ObjRS.Open O ( Source, ActiveConnection, CursorType, LockType, option ); Source: SQL command, table name, stored procedure ActiveConnection: an object of type Connection, or a connection string CursorType: type of cursor on the resulting RS Locktype: type of lock on data Option: interpretation for Source A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-12
adopenunspecified adopenforwardonly (default) adopenkeyset adopendynamic adopenstatic CursorType Unspecified type of cursor. This improves performance when only one pass through a RS is needed. Like a dynamic cursor, except that you can't see records that other users add, although records that other users delete are inaccessible from your RS. Data changes by other users are still visible. Additions, changes, and deletions by other users are visible, ibl and all types of movement through the RS are allowed. A static copy of a set of records that you can use to find data or generate reports. Additions, changes, or deletions by other users are not visible. Recordset: AddNew method adding a record // if the table is not accessible... objrs.open("users", constring, adopendynamic, adlockpessimistic); // adding the record objrs.addnew(); objrs("name") = "Alberto"; objrs("surname") = "Rossi"; objrs.update(); A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-13
operation model (query) 1. create an connection object 2. open the connection towards a DBMS 3. create an recordset object 4. open the recordset 5. manipulate data inside the recorset 6. close the recordset 7. close the connection 8. remove the objects : ASP/JScript example (query) creating and opening the connection <% @LANGUAGE="JScript" %> <!--#include file="adojavas.inc"--> <% var con; var constring; con = Server.CreateObject("DB.Connection"); constring = "Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source= c:\\webdata\\mydb.mdb "; con.open(constring); %> A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-14
: ASP/JScript example (query) creating and opening the recordset <% var rst; var ssqlstring; rst = Server.CreateObject("DB.Recordset"); ssqlstring = "Select * from Customers"; rst = con.execute(ssqlstring); %> : ASP/JScript example (query) extracting data from the recordset rst <% while (!rst.eof) { Response.write(rst("Name") + "<BR>"); rst.movenext( ); } %> A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-15
: ASP/JScript example (query) closing the recordset & connection <% rst.close( ); con.close( ); rst = null; con = null; %> DB access with relative path with the method Server.MapPath <% @LANGUAGE="JScript" %> <% var con; var constring; con = Server.CreateObject("DB.Connection"); constring = "Provider = Microsoft.Jet.OLEDB.4.0; " + "Data Source = " + Server.MapPath("./mydb.mdb"); con.open(constring);... A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-16
: example of update via SQL creating and opening the connection <% @LANGUAGE="JScript" %> <!--#include file="adojavas.inc"--> <% var con; var constring; con = Server.CreateObject("DB.Connection"); constring = "Provider=Microsoft.Jet.OLEDB.4.0; t " + "Data Source= c:\\webdata\\mydb.mdb "; con.open(constring); %> : example of update via SQL adding records and closing the connection <% var ssqlstring; ssqlstring = "INSERT INTO tbooks (Title, Description, Category) VALUES ('DB Design','How to design a database', 3);" con.execute(ssqlstring); con.close( ); con = null; %> A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-17
Reserved keywords errors may occur if table or column names corresponds with keywords of ASP or the scripting language (e.g. value) to avoid these errors, enclose the name within square brackets (e.g. [ value ] ) A.Lioy, G.Ramunno - Politecnico di Torino (2010) K-18