Presentation is loading. Please wait.

Presentation is loading. Please wait.

ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer.

Similar presentations


Presentation on theme: "ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer."— Presentation transcript:

1 ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer to any OLE DB source. ADO

2 ConnectionCommandRecordset Errors Error Parameters Parameter Fields Field ADO Objects ConnectionAllows control over the connection to the data source RecordsetContains the records that are the result of a query CommandExecutes database commands and queries ErrorContains information about Errors from ADO FieldRepresents a field in a data set ParameterWorks with Command to set parameters for stored procedures or parameter queries PropertyAllows you to access ADO object Properties

3 Before using ADO, you must establish a reference to the ADO library. Project, References, Microsoft ActiveX Data Objects 2.5 Library Establish Reference to ADO

4 1.Create a Connection Object. 2.Specify the connection string to connect to the database of interest. 3. Open the connection 4.Create a Recordset object. The Recordset object contains the results of the query after execution. 5.Specify the SQL text. When you open a recordset, you can either use a table, a stored procedure, or string containing a SQL statement. 6.Open the recordset. 7.Opening the recordset executes the query and returns the records to the recordset object. The records are accessible through the recordset object now. Basic Steps to executing a query using ADO

5 A Connection object represents a unique session with a data source. In the case of a client/server database system, it may be equivalent to an actual network connection to the server. Important Properties and Methods ConnectionString ConnectionTimeout Mode CursorLocation Provider Open and Close Connection Object

6 'Create ADODB Connection Dim connAVB As ADODB.Connection Set connAVB = New ADODB.Connection connAVB.ConnectionString = "Provider = Microsoft.Jet.OLEDB.3.51;" & "Data Source=" & App.Path & "\AVB.mdb;Mode=readwrite“ connAVB.Open Connection Object

7 'Create ADODB Connection Dim connAVB As ADODB.Connection Set connAVB = New ADODB.Connection connAVB.ConnectionString = "Provider = Microsoft.Jet.OLEDB.3.51;" & "Data Source=" & App.Path & "\AVB.mdb;Mode=readwrite“ connAVB.Open ‘code connAVB.Close Set connAVB = Nothing Connection Object

8 Represents the entire set of records from a base table or the results of an executed command. You use Recordset objects to manipulate data from a provider. Important Properties and Methods CursorLocation CursorType LockType Mode Open, Close MoveFirst, MoveLast, MoveNext, and MovePrevious BOF, EOF Update, AddNew, Delete GetString, GetRows Recordset Object

9 Cursor refers to system resources needed to manage a set of data. Specifies the location of the cursor service. Constant Value Description adUseClient -Uses client-side cursors supplied by a local cursor. Use for desktop applications. adUseServer - Default. Uses data-provider or driver-supplied cursors. These cursors are sometimes very flexible and allow for additional sensitivity to changes others make to the data source. However, some features of the Microsoft Cursor Service for OLE DB (such as disassociated Recordset objects) cannot be simulated with server-side cursors and these features will be unavailable with this setting. CursorLocation

10 Dynamic cursor — allows you to view additions, changes, and deletions by other users; allows all types of movement through the Recordset Keyset cursor — behaves like a dynamic cursor, except that it prevents you from seeing records that other users add, and prevents access to records that other users delete. Data changes by other users will still be visible. Static cursor — provides a static copy of a set of records for you to use to find data or generate reports; always allows bookmarks and therefore allows all types of movement through the Recordset. Additions, changes, or deletions by other users will not be visible. This is the only type of cursor allowed when you open a client-side Recordset object. Forward-only cursor — allows you to only scroll forward through the Recordset. Additions, changes, or deletions by other users will not be visible. This improves performance in situations where you need to make only a single pass through a Recordset. Cursor Types

11 LockType is important when you have multiple users accessing the same data. Lock Types Lock TypeConstantResult N/AadLockUnSpecified Read OnlyadLockReadOnlyNo edits OptimisticadLockOptimisticLocked as each update submitted. BatchOptimisticadLockBatchOptimisticLocked when all updates submitted PessimisticadLockPessimisticLocked from Recordset creation

12 Public Sub MoveX() ' connection and recordset variables Dim rstAuthors As ADODB.Recordset Dim Cnxn As ADODB.Connection Dim strCnxn As String Dim strSQLAuthors As String ' Open connection Set Cnxn = New ADODB.Connection strCnxn = "Provider=sqloledb;Data Source=a-iresmi2000;Initial Catalog=pubs;User Id=sa;Password=;" Cnxn.Open strCnxn ' Open recordset from Authors table Set rstAuthors = New ADODB.Recordset rstAuthors.CursorLocation = adUseClient Opening a RecordSet

13 strSQLAuthors = "SELECT au_id, au_fname, au_lname, city, state FROM Authors ORDER BY au_lname" rstAuthors.Open strSQLAuthors, strCnxn, adOpenStatic, adLockOptimistic, adCmdText rstAuthors.MoveFirst numRecords = rstAuthors.RecordCount city = rstAuthors!city rstAuthors.Close Cnxn.Close Set rstAuthors = Nothing Set Cnxn = Nothing End Sub Opening a RecordSet

14 Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name, Connection Object, CursorType, LockType, adCmdStoredProc Source = string rsObject.Open SQLstring, Connection Object, CursorType, LockType, adCmdText Opening a RecordSet


Download ppt "ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer."

Similar presentations


Ads by Google