Integrating and operating VB with MySQL

Integrating and operating VB with MySQL

I am working on a project, building an application in VB, and MySQL is its database. Can you offer any tips on finding and using the code to connect VB to the MySQL database? Can VB apps and MySQL work together well? Also, how do I call the record set and add, delete and update in VB to the MySQL database?

    Requires Free Membership to View

    When you register, my team of editors will also send you resources covering Linux administration and management; integration and interoperability between Linux, Windows and Unix; securing Linux and mixed-platform environments; and migrating to Linux.

    Margie Semilof, Editorial Director

    By submitting your registration information to SearchEnterpriseLinux.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchEnterpriseLinux.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

MySQL and VB work well together, using MySQL Connector/ODBC for VB6 and Connector/NET for VB.NET. The connectors are available for download.

As for tip and tutorials, I will shamelessly plug a site I started back in 2003 called www.vbmysql.com. At the site you can find a good-sized archive of sample code and a series of articles regarding using Visual Basic and MySQL together.

Here's a basic example of inserting data using VB6 with MYSQL from the site's sample code collection:

 Dim conn As ADODB.Connection
    Set conn = New ADODB.Connection
 
 Dim rs As ADODB.Recordset
 Set rs = New ADODB.Recordset

 conn.CursorLocation = adUseClient
 conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
       & "SERVER=127.0.0.1;" _
       & "DATABASE=test;" _
       & "UID=testuser;" _
       & "PWD=12345;" _
       & "OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384

 conn.Open

 rs.Open "SELECT * FROM mytable WHERE 1=0", conn, adOpenStatic, adLockOptimistic
 rs.AddNew

 rs!Name = "Johnny Bravo"
 rs!address = "Mama's House"
 rs.Update

 txtID.Text = rs!person_id
 
 rs.Close
 Set rs = Nothing
 conn.Close
 Set conn = Nothing

This was first published in September 2005