Download presentation
Presentation is loading. Please wait.
Published byNoreen Arnold Modified over 9 years ago
1
ActiveX Data Object (ADO) in JavaScript J.L.Wang, Yen-Cheng Chen Dept. of Infomation Management Ming-Chuan University Jan. 1999
2
Outlines u Overview u Object model of ADO u ADO techniques
3
ADO Overview u ADO allow us to write code in a scripting language that can interact with a database u The missing link between the web page and almost any kind of stored data
4
ADO Data Interface u To interface with database through ODBC u Use it with any data source for which an ODBC driver is available u ODBC u ODBC u Open DataBase Connectivity
5
Data Provider Active Server Page Active Database Component ADO ODBC Driver Data Provider Interface Data Source
6
ADO Object Model u Connection Object u Establish an active connection that allows us to gain access to data stored in a database u Command Object u Obtain records, excute SQL queries, or manipulate the data u Recordset Object u Access the data that is returned from executing an SQL query
7
ADO Object Hierarchy Collection Object
8
Property collectionError collection Connection Object Field collectionProperty collection Parameter collectionProperty collection Recordset Object Command object
9
Connection Object u Connection u Represent the physical link between applications and the remote database server u All communications between Recordset or Commands and the back-end database is negotiated through the connection u Transaction u Make the interaction with the database bulletproof u A series of changes can be grouped together to look like a single, all-or-nothing (atomic) change
10
Connection Object: Basic Flow u Create an instance of the Connection object u global.asa: Session_onStart or Application_onStart u Open a connection: data source name (DSN) u Execute commands:SQL command u Close the connection u Release the object resource
11
Connection Object: Basic Commands u ObjCon=Server.CreateObject(“ADODB.Conneciton”) u Create an instance of the Connection object u ObjCon.Open(“DSN”):Open a connection u ObjCon.Execute(“SQL COMMAND”) u Execute an execution, the result can be stored in a recordset u ObjCon.Close(): Close the connection u ObjCon.BeginTrans(): Begins a new transaction u ObjCon.CommitTrans(): Saves any changes and ends the transaction, May also start a new transaction u ObjCon.RollbackTrans(): Cancel any changes and ends the transaction. May also start a new transaction
12
<% conn=Server.CreateObject("ADODB.Connection"); conn.Open("ExampleAdoDSN"); rs=conn.Execute("select * from Books"); Response.Write(" Books "); Response.Write(" "); cnt=rs.Fields.Count; for (i=0;i<cnt;i++) { Response.Write(" "+rs(i).Name+" "); } Response.Write(" \n"); while (! rs.EOF) { Response.Write(" "); for (i=0;i<cnt;i++) { Response.Write(" "+rs(i)+" "); } Response.Write(" \n"); rs.MoveNext(); } Response.Write(" \n"); Response.Write(" "); conn.Close(); conn=null; %>
16
Connection Scope u global.asa u Session_onStart function Session_onStart() { ObjCon=Server.CreateObject(“ADODB.Conneciton”)... } u Application_onStart function Application_onStart() { ObjCon=Server.CreateObject(“ADODB.Conneciton”)... }
17
Connection Transaction u Perform a series of updates on a data source u Get the system to store up all the changes, and then commit them in one go u Before actually commit the change, the chnages can be rolling back ObjCon=Server.CreateObject(“ADODB.Conneciton”) ObjCon.Open("DSN") ObjCon.BeginTrans() ObjCon.Execute(“SQL COMMAND”) If (Conditions) { ObjCon.CommitTrans// Serve any changes } Else { ObjConn.RollbackTrans// Cancel any changes } ObjCon.Close()
18
Command Object u Provide methods and properties to manipulate individual commands u Methods u CreateParameter: Create a new Parameter object that can be appended to the Parameters collections u Execute: Execute the SQL statement or stored procedure u Property u ActiveConnection: Active one connection to be used by command object (DSN) u CommandText: Text of a command to be execute u CommandTimeout: No. of second for finishing a command u CommandType: adCmdText(1), adCmdTable(2), adCmdStoreProc(3),adCmdUnknown(3) u Prepared: Whether to create a prepare statement before execution (a command could be executed for multiple times)
19
Command Object: Basic Commands u Create an instance of the Command object u ObjCmd=Server.CreateObject(“ADOBE.Command”) u Create an active connection u ObjCmd.ActiveConnection = “DSN” ObjCmd.ActiveConnection = someConnectionObject u Execution a query u ObjCmd.CommandText = “SQL Command” u ObjCmd.CommandType = 1// SQL query u ObjCmd.Prepared = true// Compile the statement u ObjCmd.Execute() u Release the resource used u ObjCmd.ActiveConnection = null
20
<%cmd=Server.CreateObject("ADODB.Command");cmd.ActiveConnection="ExampleAdoDSN"; cmd.CommandText="select * from Books"; rs=cmd.Execute(); cnt=rs.Fields.Count; Response.Write(" Books "); Response.Write(" "); for (i=0;i<cnt;i++) { Response.Write(" "+rs(i).Name+" "); } Response.Write(" \n"); while (! rs.EOF) { Response.Write(" "); for (i=0;i<cnt;i++) { Response.Write(" "+rs(i)+" "); } Response.Write(" \n"); rs.MoveNext(); } Response.Write(" \n"); Response.Write(" "); cmd=null; %>
21
GenerateTable(rs, tableTitle) function GenerateTable(rs, tableTitle) { cnt=rs.Fields.Count; Response.Write(" tableTitle "); Response.Write(" "); for (i=0;i<cnt;i++) { Response.Write(" "+rs(i).Name+" "); } Response.Write(" \n"); while (! rs.EOF) { Response.Write(" "); for (i=0;i<cnt;i++) { Response.Write(" "+rs(i)+" "); } Response.Write(" \n"); rs.MoveNext(); } Response.Write(" \n"); }
22
<% function GenerateTable(rs, tableTitle) {... }cmd=Server.CreateObject("ADODB.Command");cmd.ActiveConnection="ExampleAdoDSN"; cmd.CommandText="select * from Books"; rs=cmd.Execute(); GenerateTable(rs, "Books"); %>
23
Recordset Object u Assign the query results to a Recordset object u Like a table in memory u Can create recorsets containing the data returned from that query u Can even create a recordset directly, without having to open a connection or execute a command first
24
Recordset Fundamentals u Open the recordset Set rs=Server.CreateObject(“ADODB.Recordset”) rs.Open(“select * from Books”, “DSN=ExampleAdoDSN;”) u Access the data field firstname = rs(“fieldname”) firstname = rs.Fields(“fieldname”) n = rs.Fields.Count// get the number of fields u Navigate the records while (! rs.EOF) { // do something with the data rs.MoveNext() }
25
<% rs=Server.CreateObject("ADODB.Recordset"); conn=Server.CreateObject("ADODB.Connection"); conn.Open("ExampleAdoDSN"); rs.Open("select * from Books", conn); cnt=rs.Fields.Count; Response.Write(" Books "); Response.Write(" "); for (i=0;i<cnt;i++) { Response.Write(" "+rs(i).Name+" "); } Response.Write(" \n"); while (! rs.EOF) { Response.Write(" "); for (i=0;i<cnt;i++) { Response.Write(" "+rs(i)+" "); } Response.Write(" \n"); rs.MoveNext(); } Response.Write(" \n"); Response.Write(" ");... %>
26
Recordset: Properties u AbsolutePage: Page of current position u AbsolutePosition: The original position of the current record u ActiveConnection: Active connection object u BOF: Before of first record ( True or False ) u Bookmark: Return/set a bookmark u CacheSize: Number of records cached u CursorLocation: Server, client, or client batch u CursorType: Forwarde, static, dynamic, keyset u EditMode: The editing status ( backward compatible with DAO) u EOF: End of file ( True or False ) u Filter: Hide types of records u LockType: Record locking for edits or updates u MaxRecords: Maximum records retrieved u PageSize: Number of pages total u RecordCount: Number of total records u Source: Source command u Status: Status of the last action
27
CursorType u Dynamic: adOpenDynamic Fully updateable recordset All actions made by other users while the recordset is open are visible All types of movement ( up and down ) u Keyset: adOpenKeyset Updateable recordset It prevents access to records that other users add after it was created All types of movement u Static: adOpenStatic Static non-updateable recordset ( retrieve data ) Changes made by other users while the recordset is open aren’t visible All types of movement u Forward-only: adOpenForwardOnly (default) Static non-updateable recordset Only Scroll forward through the records (MoveNext, GetRows) actions: additions, changes & deletion
28
Recordset: Method u AddNew: Create a new record in an updateable recordset u CancelBatch: Cancels a pending batch update u CancelUpdate: Cancel any changes made to the current or a new record u Clone: Create identical Recordset u Close: Close an open recordset u Delete: Delete the current record u GetRows: Get multiple records u Move: Move the position of the current record u MoveFirst, MoveLast, MoveNext, MovePrevious u NextRecordset: Move to the next set in multi-set query u Open: Establish a connection and execute the query u Requery: Refresh the data ( re-execute the original query ) u Resync: Synchronize data with server u Supports: Determine supported features u Update: Save any changes made to the current record u UpdateBatch: Write all pending batch updates to disk
29
Recordset: Create Recordset Directly u Create a recordset ObjRS = Server.CreateObject(“ADODB.Recordset”) u Fill the new recordset with values from the data source ObjRS.Open(Source,ActiveConnection,CursorType,LockType,Options) u Source: A Command object, SQL statement, table name or stored procedure u ActiveConnection: Data Source Name u CursorTYpe: adOpenForwardOnly (default) u LockType: adLockReadOnly (default) u Options: The type of query or table represented by Source u adCmdUnknows(0): Unknown(default) u adCmdText(1): SQL statement u adCmdText(2): Table name for creating a recordset u adCmdStoredProc(3): A stored procedure
30
Recordset: Moving u ObjRS.Move(n): Moving -n : move backward n records n: forward ( interger ) u ObjRS.AbsolutePosition u the current record number u Return value -1 (adPosUnknown: No current record (be deleted) -2 (adPosBOF): Before the first record -3 (adPosEOF): After the last record
31
Recordset: Connection ObjCon=Server.CreateObject(“ADODB.Conneciton”) ObjCon.Open(“DSN”) ObjRS = ObjCon.Execute (“SQL COMMAND”) ….
32
Recordset: Command ObjCmd = Server.CreateObject(“ADOBE.Command”) ObjCmd.ActiveConnection = “DSN” ObjCmd.CommandText = “SELECT * FROM JobCon” ObjCmd.CommandType = adCmdText ObjRS = ObjCmd.Execute() …
33
Recordset:Table/Command u ObjCmd = Server.CreateObject(“ADOBE.Command”) ObjCmd.ActiveConnection = “DSN” ObjRS = ObjCmd.Execute (“TableName”,,adCmdTable)... ObjRS.Close() … u ObjCmd = Server.CreateObject(“ADOBE.Command”) ObjCmd.ActiveConnection = “DSN” ObjCmd.CommandText = “TableName” ObjCmd.CommandType = adCmdTable ObjRS = ObjCmd.Execute() … ObjRS.Close() …
34
Recordset:Iteration ObjCon = Server.CreateObject(“ADODB.Connection”) ObjCon.Open(“DSN”) ObjRS = ObjCon.Execute(“TableName”,, adCmdTable) ObjRS.MoveFirst() While (! ObjRS.EOF) { … ObjRS.MoveNext() }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.