Unit 6 - Question Bank
Unit 6 - Question Bank
Released in 1996, activeX data object (ADO) originated from the concept of RDO
(remote data object) and DAO (data access object). One of the constituents of MDAC
(Microsoft data access components), ADO and other MDAC constituents provides a
framework of components used by client applications to access SQL, semi-structured and
legacy data stores.
The object model of ADO contains four collections of twelve objects. The different
collections are fields, properties, parameters and errors. Each collection consists of the
following twelve objects:
1. Connection - for connecting to data source through OLE DB
2. Command - for sending an instruction (SQL query or stored procedure) to data
provider
3. Recordset - a group of records representing the data
4. Immediate - a recordset locked in optimistic or pessimistic way
5. Batch - for committing or doing a rollback database transaction
6. Transaction - the database transaction
7. Record - a set of fields
8. Stream - for reading and writing a stream of bytes
9. Parameter - for changing the functionality
10. Field - a column in the database
11. Property - the ability of OLEDB provider
12. Error - the error faced by the OLEDB provider during its execution
2) Discuss ODBC and OLEDB
ODBC
It is short for Open Database Connecting. It is an interface standard, designed for
communication between different apps and operating systems (OS).ODBC was created by
SQL Access Group and first released in September 1992. ODBC was originally created for
Structured Query Language (SQL). It has since expanded to handle more programming
languages.
OLEDB
OLE BD is short for Object Linking and Embedding Database. This is a group of
API’s designed to provide access to app data in different file formats. This included SQL
capability (like ODBC), and many other languages.
The objects in OLE DB consist mainly of a data source object, a session object, a
command object, and a rowset object. An application using OLE DB would use this request
sequence:
1 Initialize OLE.
2 Connect to a data source.
3 Issue a command.
4 Process the results.
5 Release the data source object and uninitialize OLE.
<%
'declare the variable that will hold new connection object
Dim Connection
'create an ADO connection object
Set Connection=Server.CreateObject("ADODB.Connection")
Now we have an active connection to our database. Let's retrieve all the records
from the 'Cars' table. For that we have to create an instance of the recordset object and
feed it an SQL statement.
<%
'declare the variable that will hold our new object
Dim Recordset
'create an ADO recordset object
Set Recordset=Server.CreateObject("ADODB.Recordset")
'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL, Connection
%>
We have returned a recordset based on our SQL statement so let's now print out
them in the browser.
<%
'first of all determine whether there are any records
If Recordset.EOF Then
Response.Write("No records returned.")
Else
'if there are records then loop through the fields
Do While NOT Recordset.Eof
Response.write Recordset("Name")
Response.write Recordset("Year")
Response.write Recordset("Price")
Response.write "<br>"
Recordset.MoveNext
Loop
End If
%>
Finally, need to close the objects and free up resources on the server.
<%
Recordset.Close
Set Recordset=Nothing
Connection.Close
Set Connection=Nothing
%>
LockTypeEnum Values
Constant Value Description
adLockUnspecified -1 Unspecified type of lock. Clones inherits lock
type from the original Recordset.
adLockReadOnly 1 Read-only records
adLockPessimistic 2 Pessimistic locking, record by record. The
provider lock records immediately after editing
adLockOptimistic 3 Optimistic locking, record by record. The
provider lock records only when calling update
adLockBatchOptimistic 4 Optimistic batch updates. Required for batch
update mode
Example
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT * FROM Customers"
rs.CursorLocation=adUseClient
rs.CursorType=adOpenStatic
rs.LockType=adLockBatchOptimistic
rs.Open sql,conn
rs.Close
conn.Close
%>
Syntax
objRecordset.CursorType
CursorTypeEnum Values
Constant Value Description
adOpenUnspecified -1 Does not specify the type of cursor.
adOpenForwardOnly 0 Default. Uses a forward-only cursor. Identical to a
static cursor, except that you can only scroll forward
through records. This improves performance when
you need to make only one pass through a Recordset.
adOpenKeyset 1 Uses a keyset cursor. 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 Recordset. Data changes by
other users are still visible.
adOpenDynamic 2 Uses a dynamic cursor. Additions, changes, and
deletions by other users are visible, and all types of
movement through the Recordset are allowed, except
for bookmarks, if the provider doesn't support them.
Properties
Property Description
CommandTimeout Sets or returns the number of seconds to wait while attempting to
execute a command
ConnectionString Sets or returns the details used to create a connection to a data source
ConnectionTimeout Sets or returns the number of seconds to wait for a connection to open
CursorLocation Sets or returns the location of the cursor service
DefaultDatabase Sets or returns the default database name
Mode Sets or returns the provider access permission
Provider Sets or returns the provider name
State Returns a value describing if the connection is open or closed
Version Returns the ADO version number
Methods
Method Description
BeginTrans Begins a new transaction
Cancel Cancels an execution
Close Closes a connection
CommitTrans Saves any changes and ends the current transaction
Execute Executes a query, statement, procedure or provider specific text
Open Opens a connection
RollbackTrans Cancels any changes in the current transaction and ends the transaction
Events
Event Description
BeginTransComplete Triggered after the BeginTrans operation
CommitTransComplete Triggered after the CommitTrans operation
ConnectComplete Triggered after a connection starts
Disconnect Triggered after a connection ends
ExecuteComplete Triggered after a command has finished executing
InfoMessage Triggered if a warning occurs during a ConnectionEvent operation
RollbackTransComplete Triggered after the RollbackTrans operation
WillConnect Triggered before a connection starts
WillExecute Triggered before a command is executed
Collection
Collection Description
Errors Contains all the Error objects of the Connection object
Properties Contains all the Property objects of the Connection object
Example
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0"
conn.open server.mappath("database.mdb")
conn.close
%>
11) List out property of Recordset object.(Remember Any Six)
Property Description
AbsolutePage Sets or returns a value that specifies the page number in the Recordset object
AbsolutePosition Sets or returns a value that specifies the ordinal position of the current record in
the Recordset object
ActiveCommand Returns the Command object associated with the Recordset
ActiveConnection Sets or returns a definition for a connection if the connection is closed, or the
current Connection object if the connection is open
BOF Returns true if the current record position is before the first record, otherwise
false
CacheSize Sets or returns the number of records that can be cached
CursorLocation Sets or returns the location of the cursor service
CursorType Sets or returns the cursor type of a Recordset object
DataSource Specifies an object containing data to be represented as a Recordset object
EOF Returns true if the current record position is after the last record, otherwise false
Filter Sets or returns a filter for the data in a Recordset object
Index Sets or returns the name of the current index for a Recordset object
LockType Sets or returns a value that specifies the type of locking when editing a record in
a Recordset
MaxRecords Sets or returns the maximum number of records to return to a Recordset object
from a query
PageCount Returns the number of pages with data in a Recordset object
PageSize Sets or returns the maximum number of records allowed on a single page of a
Recordset object
RecordCount Returns the number of records in a Recordset object
Sort Sets or returns the field names in the Recordset to sort on
Source Sets a string value or a Command object reference, or returns a String value that
indicates the data source of the Recordset object
State Returns a value that describes if the Recordset object is open, closed,
connecting, executing or retrieving data
Status Returns the status of the current record with regard to batch updates or other
bulk operations
MoveLast Method
This method is used to move to the last record in a Recordset object. It also makes the last
record the current record.If you call MoveLast() when the Recordset is empty, it generates an
error.
Syntax
objRecordset.MoveFirst
objRecordset.MoveLast
objRecordset.MoveNext
objRecordset.MovePrevious
14) Explain System DSN
Data Source Name (DSN) provides connectivity to a database through an ODBC driver. The
DSN contains database name, directory, database driver, UserID, password, and other
information. Once you create a DSN for a particular database, you can use the DSN in an
application to call information from the database.The developer creates a separate DSN for
each database. To connect to a particular database, the developer specifies its DSN within a
program. In contrast, DSN-less connections require that all the necessary information be
specified within the program.
System DSN -- can be used by anyone who has access to the machine. DSN info is
stored in the registry.if you set up a System DSN, it does not matter who logs on to
the machine; all users that logon to that workstation will have access to that Data
Source.
Type in a name that is easily remembered and relevant to your database in the 'Data Source
Name' textbox
Click Select and browse to your local database file
Click OK all the way out
You have now created a DSN which will come in handy when you try to connect and retrieve
information from your database