Using-MyODBC-with-Visual-Basic-6-and-ADO
Transcript of 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
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
Copyright 2003, Mike Hillyer
Why Visual Basic?
• Fast development
• Easy to learn
• Well suited for database apps
• Performance
• Connector / ODBC 3.51
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
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
Copyright 2003, Mike Hillyer
Useful VB Controls
Listview
Treeview
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
Copyright 2003, Mike Hillyer
Listview Demonstration
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
Copyright 2003, Mike Hillyer
Treeview Demonstration
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
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
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
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"
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
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
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
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
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
Copyright 2003, Mike Hillyer
Opening a Recordset
rs.Open "SELECT * “ _
& “FROM mytable", _
conn, _
adOpenStatic, _ adLockReadOnly
Copyright 2003, Mike Hillyer
Common Methods
• rs.MoveFirst
• rs.MovePrevious
• rs.MoveNext
• rs.MoveLast
• rs.AddNew
• rs.Delete
• rs.Update
• rs.requery
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
Copyright 2003, Mike Hillyer
Displaying Data Demonstration
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
Copyright 2003, Mike Hillyer
Insert Demonstration
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
Copyright 2003, Mike Hillyer
Update Demonstration
Non-Bulk Bulk
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
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
Copyright 2003, Mike Hillyer
Blob Demonstration
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
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