using-myodbc-with-visual-basic-6-and-ado

32
Copyright 2003, Mike Hillyer Using Connector / ODBC with Visual Basic 6 and ADO Mike Hillyer Webmaster – VBMySQL.com

Upload: leque

Post on 08-Dec-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Using Connector / ODBC with Visual Basic 6 and ADO

Mike Hillyer

Webmaster – VBMySQL.com

Page 2: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Presentation Outline

• Why VB/MySQL• MySQL datatypes and their VB counterparts• Useful VB controls• ADO Basics• Displaying data• Inserting Data• Updating Data• Using BLOB fields• Error handling

Page 3: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Why Visual Basic?

• Fast development

• Easy to learn

• Well suited for database apps

• Performance

• Connector / ODBC 3.51

Page 4: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

What You Will Need

• Visual Basic 6– Service Pack 5

– MDAC 2.5+

• MySQL– 3.x or 4.x

• Connector / ODBC– 3.51.06 is now stable

Page 5: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

VB/MySQL Datatypes

• Use most appropriate MySQL datatype, deal with conversion in client application

• Visual Basic.NET handles most if not all MySQL data types, one reason to upgrade?

• www.vbmysql.com/articles/datatypes.html

Page 6: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Useful VB Controls

Listview

Treeview

Page 7: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Listview

• Located in Microsoft Windows Common Controls 6.0

• Great for displaying multiple records in one view

• More versatile than the datagrid

• Greater control by avoiding bound controls

• Data can be hidden in zero-width columns

Page 8: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Listview Demonstration

Page 9: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Treeview

• Located in Microsoft Windows Common Controls 6.0

• Useful for navigating groups or tables

• All items are nodes, nodes have parent/child relationships to determine hierarchy

• Set key values to link relationships

Page 10: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Treeview Demonstration

Page 11: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

ADO Basics

• Reference Microsoft ActiveX Data Objects 2.5 Library

• Replaced RDO, DAO, replaced by ADO.NET

RECORDSET STREAM

CONNECTION

MySQL

ODBC

ADO

Visual Basic

Page 12: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Connection Object

• Handles all interactions with ODBC Layer

• Handles transaction control (start, commit, rollback)

• Requires a cursor location

• Requires a connection string

• Can execute raw SQL statements

Page 13: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Cursor Locations

• Cursor is provided through Connector / ODBC

• Allows for ForwardOnly and Dynamic cursortypes

• Allows for all lock types

• Cursor is handled in VB

• Only Static cursor is available

• Pessimistic lock is unavailable

adUseServer adUseClient

Page 14: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Connection String

Connection.ConnectionString=

"DRIVER={MySQL ODBC 3.51 Driver};

SERVER=600.500.400.300;

DATABASE=MyDB;UID=MyUsername;

PWD=MyPassword;

OPTION=18475"

Page 15: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Option Values

Do not cache the results locally in the driver, instead read from server(mysql_use_result). This works only for forward-only

cursors.

1048576

Change LONGLONG columns to INT columns (some applications can't handle LONGLONG).

163841

Use the compressed server/client protocol .2048

Enable or disable the dynamic cursor support.32

Don't set any packet limit for results and parameters.8

The client can't handle that MySQL returns the true value of affected rows. If this flag is set then MySQL returns 'found rows' instead.

2

The client can't handle that Connector / ODBC returns the real width of a column.

1

DescriptionBit

Page 16: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Executing SQL

connect i on. Execut e _

" DELETE FROM myt abl e “ _

& “ WHERE name=' Mi ke' " , _

af f ect ed, _

adExecut eNoRecor ds

Page 17: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Recordset Object

• Stores rows returned by SQL queries

• Requires SQL statement, cursor type, lock type

• Allows for manipulation of data, traversing of records, insertion, updates, deletes

Page 18: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Cursor Types

Static set of records for manipulation and report generation. Changes by others are not visible.

adOpenStatic

Most resource-intensive. All changes made by others are visible, and all movement is allowed.

adOpenDynamic

Identical to static cursor, but allows for forward movement only. This is the default cursor type.

adOpenForwardOnly

DescriptionCursor Type

Page 19: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Lock types

This value indicates optimistic batch updates and is required for batch update mode.

adLockBatchOptimistic

The provider uses optimistic locking, locking records only when the Update method is called.

adLockOptimistic

The provider does what is necessary to ensure successful editing of the records.

adLockPessimistic

This value indicates read-only records where the data cannot be altered. This is the default.

adLockReadOnly

DescriptionLock Type

Page 20: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Opening a Recordset

rs.Open "SELECT * “ _

& “FROM mytable", _

conn, _

adOpenStatic, _ adLockReadOnly

Page 21: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Common Methods

• rs.MoveFirst

• rs.MovePrevious

• rs.MoveNext

• rs.MoveLast

• rs.AddNew

• rs.Delete

• rs.Update

• rs.requery

Page 22: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Displaying Data

• Open a Connection and Recordset

• Loop through recordset to end of file

• Load data into appropriate controls

• Close Recordset And Connection

• Respond to User Actions

Page 23: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Displaying Data Demonstration

Page 24: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Inserting Data

• Trigger with submit button

• Validate all data entered

• Substitute / remove illegal characters

• Use recordset .addnew method, or build a SQL statement to execute

• Recordset will need non-readonly lock

Page 25: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Insert Demonstration

Page 26: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Updating Data

• Trigger with submit button

• Validate data entered

• Substitute / remove illegal characters

• Send to MySQL through either a recordset or executed SQL statement

• Recordset will need non-readonly lock

Page 27: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Update Demonstration

Non-Bulk Bulk

Page 28: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Handling BLOBs

• Chunking vs. Streams

• BLOBs vs. file system

• BLOBs should occupy separate table

• Option 8 (No packet limit) must be set

• Max_allowed_packet will likely need to be set to 15M or higher in my.ini

Page 29: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

BLOBs in a Table

• Improved performance with MyISAM vs. InnoDB

• Link blobs to filenames in separate table for file history archive

• Store file size, file_id, and timestamp with BLOB

Page 30: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Blob Demonstration

Page 31: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

Error Handling

• Connection.errors(n).description

• Connection.errors(n).nativeerror

• include/errmsg.h & include/mysqld_error.h

• 0 – No ODBC driver installed

• 1045 – Access Denied

• 2003 – Cannot connect to host

• fooassociates.com/phpfer/html/rn41re779.html

Page 32: Using-MyODBC-with-Visual-Basic-6-and-ADO

Copyright 2003, Mike Hillyer

That’s All Folks!

• Thank you for coming

• www.vbmysql.com/presentations/uc2003

• Sample code also available

• I will be available for any questions