This step-by-step tutorial takes the reader through the process of using databound controls in VB.NET using the MyODBC Driver to connect to a MySQL database. We will start by creating a very basic and simple MySQL database, then connect to it and bind controls from within Visual Basic.
For the technical description of the MySQL Connector/ODBC, read this section of the MySQL Reference Manual 5.1.
For a code-heavy example of using Connector/ODBC with Visual Basic, read this section of the MySQL Reference Manual 5.1.
While I have found plenty of fine tutorials for using MySQL and VB.NET, I have not found one that specifically covers the MyODBC Driver in enough detail for me to do what I wanted to accomplish. I found my way through it, and want to document the steps, as much for myself as well as anyone else who may benefit from it.
Many are aware of Michael Hillyar’s excellent tutorials using the MySQL Connector/NET to connect VB.NET to a MySQL database. If you are not, you really should be. I learned a lot by following along with his tutorials. Please note that this tutorial also appears on his site.
I developed a database application using Connector/NET, and tired of writing SQL Statements. I wanted a connector that could be used with VB.NET’s cool databound controls – mostly because I am lazy. I tried playing with MyODBC, and while not perfect, it gets me closer to where I want to be when it comes to Rapid Application Design.
For a helpful discussion about connecting MySQL and Visual Studio .NET using MyODBC, visit this thread.
Many tutorials I have followed tend to throw in a lot of extra code, such as formatting of labels and text boxes, naming forms, and throwing in extra tools. Of course, the purpose of which is to demonstrate good programming practices. I want to skip all of that and focus on databinding. This tutorial assumes you are already coherent in database design and Visual Basic programming.
Please note that this tutorial requires the full version of Visual Studio .NET. The Express (free) version does not allow you to connect to a MySQL Database throught the MyODBC Connector.
For a tutorial on database design, visit Part 1 of The VB.NET-MySQL Tutorial.
The topology in the figure below shows the computers and software I am using.
Of course, MySQL runs fine under Windows, so it is not necessary to use a separate computer for the MySQL Server. Regardless of your configuration, this tutorial should work as long as you know how to connect to your MySQL Server.
Warning – I am breezing through these steps. I want to get to the “meat” of the tutorial, which is the databound controls. Besides, most of the downloads come with installation instructions, and Mike Hillyar explains most of this better than I can.
For a tutorial on downloading and installing MySQL and MySQL Query Browser, visit Part 2 of The VB.NET-MySQL Tutorial.
For a tutorial on downloading and installing Visual Basic 2005 Express and MySQL Administrator, visit Part 3 of The VB.NET-MySQL Tutorial.
The following code creates the database and tables, as well as putting in a few stores and employees for practice. You can copy and paste the code into the MySQL Query Browser and execute it (assuming the Query Browser is properly setup):
use mysql; drop database if exists mystore; create database mystore; use mystore;
create table store ( storeID int unsigned auto_increment primary key, city char(50), state char(2), manager char(50) );
create table employee ( employeeID int unsigned auto_increment primary key, storeID int unsigned not null references store(storeID), lastname char(50), firstname char(50) );
insert into store values (default,'San Diego','CA','Russell Smith'), (default, 'St. Louis', 'MO','Jim Marks');
insert into employee values (default,1,'Jones','Bill'), (default,1,'Collins','Howard'), (default,2,'Johnson','Cindy'), (default,2,'Gonzales','Hector'), (default,2,'Hanson','Danny');
This step is optional. Creating a Data Source Name (DSN) provides a convenient connection method for the computer developing the application. However, if the finished application is to be installed on other computers, this step will need to be repeated for each computer. The alternative is to use a Connection String, which will be covered later.
Select your DSN (myodbc-mystore in my case) from the combo box and select OK. User name and Password are not needed here, as they are part of the DSN.
DRIVER={MySQL ODBC 3.51 Driver}; SERVER=linuxbox; PORT=3306; DATABASE=mystore; OPTION=3
Enter the Connection String, User name (user), and Password (user) in the appropriate text boxes and select OK.
The MyODBC Driver will not automatically create Insert, Delete, or Update statements for the Table Adapters. If you wish to alter the database from a Visual Basic application, you will have to create these statements yourself.
WHERE storeID = ?
This allows you to determine which record requires deleting within the database.
Now that the Table Adapters have the basic queries (Select, Insert, Delete, Update), we can start binding them to controls in a Visual Basic form.
Please realize in the following instructions that I am not particularly concerned with how the form looks, only how it acts. I have used Labels and Text Properties to identify the Name Property for each control.
In addition, if this were a real application, I would provide for validation of user input before attempting to commit input to the database. As it is, incorrect inputs will cause this simple application to crash.
Me.Validate() Me.StoreBindingSource.EndEdit() Me.StoreTableAdapter.Update(Me.MystoreDataSet.store)
This code will commit changes to the database from the DataSet when the btnSave button is pushed, and then refresh the DataSet, as well as all databound controls. Closing the application without pressing this button will not save changes to the database.
Me.StoreBindingSource.AddNew()
This code adds a new record to the DataSet when the btnAdd button is pressed, providing blank textboxes for adding new information.
Me.StoreBindingSource.RemoveCurrent()
This code deletes the current record from the DataSet when the btnDelete button is pressed.
By far, the easiest databinding is with a DataGrid, although I do not consider it very user-friendly on the client side. However, I am going to add a wrinkle to this one – I am going to link it to the selected store to only show (and edit) the employees in that store.
Me.EmployeeBindingSource.EndEdit() Me.EmployeeTableAdapter.Update(Me.MystoreDataSet.employee) Notice in the figure below that I put the new commands after the commands to update the store table. this is on purpose, as the employee table is a child of the store table, the store table needs to be updated first.
I think I accomplished what I set out to do with this tutorial – to create a simple step-by-step approach to completing the task of connecting the MyODBC Connector to a Visual Basic application. The finished product is by no means pretty, or even useful, except to demonstrate how to use the nifty databinding abilities in Visual Studio with the MyODBC Connector.
Microsoft® ADO.NET 2.0 Step by Step By Rebecca M. Riordan
Professional ADO.NET 2: Programming with SQL Server 2005, Oracle, and MySQL By Wallace B. McClure, Gregory A. Beamer, IV John J. Croft, J. Ambrose Little, Bill Ryan, Phil Winstanley, David Yack, Jeremy Zongker