Introduction to ADO By David R. Stevenson Consulting Software Engineer ABB Automation.

Slides:



Advertisements
Similar presentations
Basics of Database Programming with VB6
Advertisements

ADO DB in Access VBA © Walter Milner 2005 Slide: 1 ADO VBA Programming in Access.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Persistent data storage: relational databases and ADO.
CIS 338: Using ADO (ActiveX Data Objects) [largely replaced by adonet.vb] Dr. Ralph D. Westfall April, 2003.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
The ADO Data Control. Universal Data Access Open Database Connectivity (ODBC) –standard for accessing data in databases OLE-DB –allows access to data.
ActiveX Data Object ISYS 562. ADO An ActiveX control ActiveX is build upon COM, a contract that defines a standard interface by which objects communicate.
Using ADO Programmatically The Connection Object, Command Object, and Recordset Object.
Using Visual Basic 6.0 to Create Web-Based Database Applications
ActiveX Data Control BICS 546. Intrinsic and ActiveX Controls VB supports two types of controls: –Intrinsic controls (command button, listbox, etc): When.
Web-based Programming Lanjut Pertemuan 6 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007.
Programming the RecordSet Object
 2004 Tau Yenny, SI - Binus M0194 Web-based Programming Lanjut Session 6.
Using ADO Programmatically The Connection Object and the Command Object.
Database Connectivity Session 2. Topics Covered ADO Object Model Database Connection Retrieving Records Creating HTML Documents on-the-fly.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Visual Basic Database Access BICS546. Microsoft Universal Data Access OLE DB: The OLE database protocol –Allows a program to access information in any.
CIS 451: ASP Recordsets Dr. Ralph D. Westfall May, 2002.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
ASP & ADO. Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales”
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
ADO Recordsets. Recordset Objects Similar to Tables and Queries: data Using VBA/VBScript you… –Open a recordset, –Locate a record –Update or add a record.
The Tip of the ADO-burg Francisco H Tapia. Ado Objects Command Connection Error Parameter Property Record RecordSet Stream.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
ActiveX Data Object (ADO) in JavaScript J.L.Wang, Yen-Cheng Chen Dept. of Infomation Management Ming-Chuan University Jan
© 2006 ITT Educational Services Inc. Course Name: IT390 Business Database Administration Unit 9 Slide 1 IT 390 Business Database Administration Unit 9:
1 VBScript Session What we learn last session?
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Using Visual Basic 6.0 to Create Web-Based Database Applications
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 11 Accessing Database.
Tutorial 10 by Sam ine1020 Introduction to Internet Engineering 1 Database & Server-side Scripting Tutorial 10.
ADO 2.5 Kamaljit Bath, Program Manager Data Access Group.
EM412 Using Adaptive Server Anywhere with Visual Basic Ali Chalhoub Technical Support Consultant iAnywhere Solutions
Lecture Note 10: Simple Database Techniques. Introduction –Database System –Access, SQL Server and others. –Microsoft Access - Interacting with this databases.
Visual Basic ADO Programming 56:150 Information System Design.
What’s new in ADO 2.5 Greg Hinkel Program Manager Data Access Group
What is database?  Any Method for access info into Application from DataBase?  ODBC is standard for Accessing Data.  Problem with ODBC:  Information.
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.
ASP-12-1 Data Store Access Colorado Technical University IT420 Tim Peterson.
Lab 8 Data Access Using Microsoft ActiveX Data Object (ADO)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
HNDIT Rapid Application Development
ADO ActiveX Data Object. ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard.
What’s new in ADO 2.5 Greg Hinkel Program Manager Data Access Group
What’s new in ADO 2.5 Greg Hinkel Program Manager Data Access Group
Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com.
ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson.
1 ADO Activex Data Objects. 2 ADO ADO allows users to access data easily from many existing databases (such as Access or Paradox) From ODBC compliant.
ADO & Recordsets. ADO Description & History  ActiveX Data Objects  History  1991 ODBC  Data Access Objects (DAO) for VB developers (JET)  Remote.
Using databases ActiveX Data Objects (ADO) Connecting to a database Reading data from a database Inserting, updating and deleting records Using databases.
Chapter 5 Building Your Product Catalog database Objectives Create Database. Create Table. Connect to Database. Use ASP Script to add new products. Use.
 2 Data Object Library approaches ◦ DAO (Data Access Objects)  Original access strategy (up to VB6)  Closely linked to MS Access ◦ ADO (ActiveX Data.
Visual Basic Database Access
Visual Basic Database Programming.
Data Access Objects .
ADO VBA Programming in Access
Find, filter etc with connection to Access code internally
ActiveX Data Objects (ADO)
INT213 Updating the Database.
ربط الفيجوال بيسك VB مع قواعد البيانات
VISUAL BASIC INTRODUCTION TO DATA CONNECTIVITY.
Chapter 10 ASP and Data Store Access
Database Objects 1/12/2019 See scm-intranet.
What’s new in ADO Greg Hinkel Program Manager Data Access Group
Working With Databases
Excel Import data from Access to Excel (ADO) using VBA
Source: Link Posted By: HeelpBook Permalink: Link
Presentation transcript:

Introduction to ADO By David R. Stevenson Consulting Software Engineer ABB Automation

ADO Object Model Connection Errors Command Recordset Fields Error ParameterParameters Field

ADO Object Model Connection Command Recordset Field PropertiesProperty

VB Project References Microsoft Active X Data Objects 2.0 (2.1) Library Microsoft Active X Data Objects 2.0 Recordset Library ADOX: Microsoft ADO Ext. 2.1 for DDL and Security Microsoft ActiveX Data Objects (Multi- Dimensional) 1.0 Library

Connection Object Connection to the Database Transaction Support Database Schema Command Execution

Types of Connections DSN-Less Connection DSN Connection

Opening a Connection Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.Open ConnectionString, UserId, Password, Options ‘ Note: UserId and Password override Connection String ‘ Do some ADO Work here. cn.Close

Connection String? Easy Way: Let the ADO Data Control Build one for you! (Components: Microsoft ADO Data Control 6.0 (OLEDB) ) Put Control on Form. Right-Click and select properties. Use Connection String Build button. Test Connection. Copy Connection String.

Building Connection String - 1

Building Connection String - 2

Connection String Example SQL Server: “Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=ENG157” MS Access: “Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\drs\BegDB\Nwind.mdb” Oracle example (Look Ma! No Connection String): cn.Provider = "MSDAORA" cn.Open, “Scott”, “Tiger”

Providers MSDASQL (ODBC via System DSN) Microsoft.Jet.OLEDB.3.51 (Access) Microsoft.Jet.OLEDB.4.0 (Access) SQLOLEDB (SQL Server) MSDAORA (Oracle) MSIDXS (Index Server) ADSDSOObject (Active Directory Services) MSDataShape (Hierarchical Recordsets)

Processing Connection Errors Private Sub BuildErrorInformation ( cn As ADODB.Connection, ErrorNumber As Long, ErrorString As String ) Dim adoError As ADODB.Error For Each adoError In cn.Errors ErrorNumber = adoError.Number ' ErrorNumber = adoError.NativeError If ErrorString <> "" Then ErrorString = ErrorString & vbCrLf ErrorString = ErrorString & adoError.Description Next adoError End Sub

DSN Types User - specific to the user, cannot be shared System - All users can access it File - Similar to System DSN, stored by default as a text file in: \Program Files\Common Files\ODBC\Data Sources

Creating a DSN Start -> Settings -> Control Panel Double-click ODBC Data Sources Click System DSN tab. Click Add button. 1st dialog: Select driver (SQL Server, Access, Oracle, etc.) Enter a Name and Description for DSN. Enter Database specific access info. Test by selecting Test Data Source button.

Using a DSN Connection Dim cn As New ADODB.Connection cn.Open “DSN=FoodMart”

Transaction Support Connection Methods –BeginTrans –CommitTrans –RollbackTrans Transactions may be nested Not all Providers support transactions

Executing Commands Execute method of Connection Object executes queries, stored procedures, or provider specific text. Asynchronous or synchronous execution Set Recordset = Connection.Execute ( CommandText [, RecordsAffected] [,Options] ) Returns forward-only, read-only (firehose) cursor

Execute Method Parameters SQL Statement (Select, Update, Insert, Delete, DDL), Table Name, Stored Procedure Name, or provider specific text. RecordsAffected - Variable returning Number of records that the operation affected.

Execute Method Option adCmdText (SQL Statement) adCmdTable (Table Name) adCmdTableDirect (Table Name) adCmdStoredProc (Stored Procedure Name) adCmdFile (saved recordset) adCmdUnknown (default) adAsyncExecute (asynchronous execution) adAsyncFetch (asynchronous fetching) adExecuteNoRecords (non-row returning command)

Command Object Execution of commands (queries, update, insert, delete, DDL, stored procedures) Parameters (input and output) for Stored Procedures Dim cmd As New ADODB.Command Set cmd.ActiveConnection = cn

Command Object Methods CreateParameter ( Name, Type, Direction, Size, Value ) –Direction: adParamInput, adParamOutput, adParamInputOutput, adParamReturnValue –Type example: adInteger Execute ( RecordsAffected, Parameters, Options)

Command Object Properties ActiveConnection CommandText: SQL, Stored Procedure Name CommandType: adCmdText, adStoredProc Prepared: Save a compiled version of command? State: adStateClosed, adStateOpen, adStateExecuting, adStateFetching

Parameters Collection Methods Append: append a Parameter object Delete: delete a Parameter object Refresh: query for parameters Properties –Count: number of parameters –Item ( Index ): fetch a parameter item

Parameters Collection Dim prm As ADODB.Parameter Set prm = cmd.CreateParameter("OrderID", adInteger, adParamInput, 0, 10248) cmd.Parameters.Append prm Dim rs As ADODB.Recordset cmd.CommandText = "CustOrdersDetail" Set rs = cmd.Execute(lngRecordsAffected,, adCmdStoredProc)

Parameters: Return Value ' The return value is always the first parameter in the Parameters Collection. Dim lngReturnValue As Long Set prm = cmd.CreateParameter("RETURN_VALUE", adInteger, adParamReturnValue, 0, lngReturnValue) cmd.Parameters.Append prm

Opening a Recordset Method: Open ( Source, ActiveConnection, CursorType, LockType, Options ) –Source: Command object, SQL statement, stored procedure, or file name of persisted recordset. –ActiveConnection: Connection string or object –CursorType: adOpenForwardOnly, adOpenKeyset, adOpenDynamic, adOpenStatic –LockType: adLockReadOnly, adLockPessimistic, adLockOptimistic, adLockBatchOptimistic –Options: adCmdText, adCmdTable, adCmdStoredProcedure, adCmdUnknown, adCmdFile

Fields in a Recordset Refer to by Field Number or by Field Name –Ex.: rs.Fields(0).Value or rs.Fields(“ShipperID”).Value Field Properties –ActualSize: useful for variable length data. –Name –Type –UnderlyingValue, Value

Modifying a Recordset Recordset Methods –AddNew ( [FieldList] [, Values] ) –Ex: rs.AddNew Array(“CompanyName”, “Phone”), Array(“Speedy Delivery”, “ ”) –Delete ( [AffectRecords]): delete the current record or a group of records. –Update ( [Fields], [Values] ): update records in current recordset.

Disconnected Recordsets Set CursorLocation to adUseClient After obtaining recordset, set ActiveConnection to Nothing rs.CursorLocation = adUseClient rs.Open “Select * from Customers”, cn, adOpenForwardOnly, adLockReadOnly, adCmdText Set rs.ActiveConnection = Nothing Set FunctionValueReturned = rs

Recordset Navigation Methods Move ( NumRecords As Long [, Start As Bookmark ] ) MoveFirst MoveLast MoveNext MovePrevious Related Properties: BOF, EOF, Bookmark

Saving Recordsets To File Save (FileName As String [, PersistFormat]) PersistFormat: –adPersistADTG ( Proprietary Recordset File Format ) –adPersistXML ( XML Format, ADO 2.5 only ) May also save to ADODB.Stream and ASP Response objects (ADO 2.5)

Recommended Reading ADO 2.1 Programmer’s Reference

Recommended Reading Beginning Visual Basic 6 Database Programming

Recommended Reading Professional ADO 2.5 Programming

Recommended Reading Professional Visual Basic 6 Databases

OpenSchema Method Get Database Information (Meta-Data) Set recordset = Connection.OpenSchema ( QueryType [, Criteria ] [, SchemaID] )

OpenSchema QueryType adSchemaAsserts adSchemaCatalogs adSchemaCharacterSets adSchemaCheckConstraints adSchemaCollations adSchemaColumnPrivileges adSchemaColumns adSchemaColumnDomainUsage adSchemaConstraintColumnUsage adSchemaConstraintTableUsage adSchemaForeignKeys adSchemaIndexes adSchemaColumnUsage adSchemaPrimaryKeys adSchemaProcedureColumns adSchemaProcedureParameters adSchemaProcedures adSchemaProviderSpecific adSchemaProviderTypes adSchemaReferentialConstraints adSchemaSchemata adSchemaTableConstraints adSchemaTables adSchemaViewColumnUsage adSchemaViews more...

OpenSchema Example Dim rs As ADODB.Recordset Set rs = cn.OpenSchema(adSchemaTables) Do While Not rs.EOF Debug.Print rs("TABLE_NAME") Debug.Print rs("TABLE_TYPE") rs.MoveNext Loop

Properties Collection Refresh Count: number of properties Item: reference by number or property name.

Property Object Attributes of a single property for objects of type: Connection, Command, Recordset, Field –Attributes: adPropNotSupported, adPropRequired, adPropOptional, adPropRead, adPropWrite –Name –Type: see DataTypeEnum –Value

Parameter Object Properties Attributes: adParamSigned, adParamNullable, adParamLong (binary) Direction: adParamInput, adParamOutput, adParamInputOutput, adParamReturnValue Name Size: in bytes or characters Type: example: adInteger Value