visual basic ado programming 56:150 information system design

21
Visual Basic ADO Programming 56:150 Information System Design

Upload: asher-hardy

Post on 13-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Visual Basic ADO Programming 56:150 Information System Design

Visual Basic ADO Programming

56:150 Information System Design

Page 2: Visual Basic ADO Programming 56:150 Information System Design

Introduction 1

Microsoft ActiveX Data Objects (ADO) enables you to write an application to access and manipulate data in a database server through an OLE DB data provider.

High speed, ease of use, low memory overhead, and a small disk footprint

Page 3: Visual Basic ADO Programming 56:150 Information System Design

Introduction 2What’s data provider

A control or object that provides data for use with another control or program. The data provider makes data connectivity much easier by hiding most of the implementation of data storage.

What’s OLE DBA set of COM-based interfaces provide applications with uniform access to data stored in diverse information sources, or data stores

Page 4: Visual Basic ADO Programming 56:150 Information System Design

Introduction 3

To use ADO objects in an application, you must first add a reference to the ADO component.

Start a Standard EXE project and then select Project References. In the Reference window, locate Microsoft ActiveX Data Objects 2.x Library and check the box before it.

Page 5: Visual Basic ADO Programming 56:150 Information System Design

Main Objects

The ADO object model defines a collection of programmable objects that can be used by any of the Microsoft Visual languages

Page 6: Visual Basic ADO Programming 56:150 Information System Design

The Connection Object

to establish connections between the client and database server ConnectionString Property

a long string with several attributes separated by semicolons“Provider = Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program Files\VB98\Nwind.mdb”“Provider=SQLOLEDB.1;User ID=sa; password=; Initial Catalog=Northwind; Data Source=EXPERTNEW”

Page 7: Visual Basic ADO Programming 56:150 Information System Design

The Connection Object

Open MethodCN.open

The open method accepts a number of optional arguments (ConnString, UserID, password, options)

Close MethodCN.Close

Set CN = Nothing (remove the Connection Object from memory)

Page 8: Visual Basic ADO Programming 56:150 Information System Design

Connection ExampleDim dbcon as ADODB.ConnectionSet dbcon = New ADODB.Connectiondbcon.ConnectionString _

="Provider=MSDASQL.1;Persist Security _ Info=False;Data Source=NWIND"

dbcon.ConnectionTimeout = 10dbcon.Open

dbcon.closeSet dbcon = Nothing

Page 9: Visual Basic ADO Programming 56:150 Information System Design

The Command Object

to issue commands, such as SQL queries and updates, to the database

ActiveConnection PropertyIf ActiveConnection is set with a reference to a Connection Object, the Command object uses an exiting connection.

If ActiveConnection is set with a connection string, a new connection is established.

Page 10: Visual Basic ADO Programming 56:150 Information System Design

The Command Object

Execute MethodUse the Execute method of the Command object to execute a query, data definition command, or stored procedure.

Set rs = cmd.Execute(NumRecords, Parameters, Options)

Options specify the type of query (in the form of CommandTypeEnum constant) to optimize processing.

Page 11: Visual Basic ADO Programming 56:150 Information System Design

CommandTypeEnum

adCmdStoreProc The command is the name of a Stored procedure

adCmdTable The command is a table’s name. “Select * from table_name” is passed to the server

adCmdTableDirect The command is a table’s name. More efficient that adCmdTable option

adCmdText The command is a SQL statement

adCmdUnknown The command is unknown (default)

Page 12: Visual Basic ADO Programming 56:150 Information System Design

Command ExampleDim cmd As ADODB.CommandDim rst As ADODB.Recordset

Set cmd = New ADODB.Commandcmd.CommandText = "select distinct ShipCountry

from orders"cmd.CommandType = adCmdTextSet cmd.ActiveConnection = dbconSet rst = New ADODB.RecordsetSet rst = cmd.Execute

Page 13: Visual Basic ADO Programming 56:150 Information System Design

Command ExampleYou can do delete, update, insert using Command Object with the right sql sentence.

Dim cmd As ADODB.Command Dim lngAffected As Integer Set cmd = New ADODB.Commandcmd.ActiveConnection = dbconcmd.CommandType = adCmdTextcmd.CommandText = "UPDATE tblOrders SET

ShipCountry = 'United States' WHERE ShipCountry = 'USA'“

cmd.Execute lngAffected

Page 14: Visual Basic ADO Programming 56:150 Information System Design

The Recordset Objectto view and manipulate the results of the queryOpen Method

To execute a query open ( [Source], [ActiveConnection], [CursorType As CursorTypeEnum = adOpenUnspecified], [LockType As LockTypeEnum = adLockUnspecified], [Options As Long = -1]))Source can be a sql statement, a valid command object, a table name, a query name (Access), a stored procedure name (SQL Server)Options is a constant that indicates how the provider should evaluate the Source argument if it represents something other than a Command object

Page 15: Visual Basic ADO Programming 56:150 Information System Design

Cursor Type (CursorTypeEnum)adOpenForwardOnly This cursor can be scanned forward only, is

suitable for one-pass operations. Less expensive than other types of cursors (default)

adOpenStatic A snapshot of the database the moment the cursor was created. It can be scanned in both directions. You can’t see modifications made by other users after the creation of the cursor.

adOpenKeyset Like a dynamic cursor, except that you can't see records that other users add. Data changes by other users are still visible.

adOpenDynamic Additions, changes, and deletions by other users are visible, and all types of movement through the Recordset are allowed.

Page 16: Visual Basic ADO Programming 56:150 Information System Design

The Recordset ObjectExample

Dim rst As ADODB.RecordsetDim StrSQL As String Set rst = New ADODB.RecordsetStrSQL = "select Description from categories where

categoryname = '" & Combocategory.Text & "'"rst.Open Source:=StrSQL,

ActiveConnection:=dbcon, Options:=adCmdText

Page 17: Visual Basic ADO Programming 56:150 Information System Design

The Recordset Object

AddNew: add new rows to recordset

rst.AddNewrst.Fields("LastName") = "Smith" rsr.Fields("FirstName") =

"Tommy" rst.UpdateUse the update method to save the new row. If you attempt to close the recordset with an update pending but haven't explicitly saved the row, you'll get a runtime error

Page 18: Visual Basic ADO Programming 56:150 Information System Design

The Record Object

Change dataMove to the desired row

Make changes

optionally use update method to save updates

rst.Find "[ContactTitle] = 'Owner'" If rst.EOF Then MsgBox "No Match was Found!" Else rst.Fields("ContactTitle") = "Manager" rst.Update

Page 19: Visual Basic ADO Programming 56:150 Information System Design

The Recordset Object

Delete recordsFind the desired rowsUse delete method to delete.

rst.Find "[ContactTitle] = 'Owner'" If rst.EOF Then MsgBox "No Match was

Found!" Else rst.deleteEnd if

Page 20: Visual Basic ADO Programming 56:150 Information System Design

The Recordset Object

Other frequently used methods Cancelupdate, Movefirst, Movenext, Movelast, Moveprevious

Other frequently used PropertiesFields, Filter, RecordCount

Page 21: Visual Basic ADO Programming 56:150 Information System Design

ADO Data Control

Nothing new but a wrapper for the ADO Recordset object.

Unlike the Recordset object, ADODC is visible at run time.

It will be shown in Sample program.