- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
MySQL Visual Basic tutorial
This is a Visual Basic tutorial for the MySQL database. It covers the basics of MySQL programming with Visual Basic. In this tutorial, we use the Connector/Net driver. This driver is based on the ADO.NET specification. The examples were created and tested on Ubuntu Linux. There is a similar MySQL C# tutorial , MySQL Perl tutorial and SQLite Visual Basic tutorial on ZetCode.
If you need to refresh your knowledge of the Visual Basic language, there is a full Visual Basic tutorial on ZetCode.
About MySQL database
MySQL is a leading open source database management system. It is a multi user, multithreaded database management system. MySQL is especially popular on the web. It is one of the parts of the very popular LAMP platform consisting of Linux, Apache, MySQL, and PHP. Currently MySQL is owned by Oracle. MySQL database is available on most important OS platforms. It runs on BSD Unix, Linux, Windows, or Mac OS. Wikipedia and YouTube use MySQL. These sites manage millions of queries each day. MySQL comes in two versions: MySQL server system and MySQL embedded system.
Before we start
We need to install several packages to execute the examples in this tutorial: libmysql6.1-cil
, mysql-server
, mysql-client
. We need to install Visual Basic compiler from the Mono project. Either from a package or from sources.
The libmysql6.1-cil is the MySQL database connector for CLI. It is written in C# and is available for all CLI languages. C#, Visual Basic, Boo and others.
$ ls /usr/lib/cli/MySql.Data-6.1/MySql.Data.dll /usr/lib/cli/MySql.Data-6.1/MySql.Data.dll
From the technical point of view, we need a DLL. On my system (Ubuntu Lucid Lynx), it was located under the above path. We need to know the path to the DLL library. To compile our examples.
If you don't already have MySQL installed, we must install it.
$ sudo apt-get install mysql-server
This command installs the MySQL server and various other packages. While installing the package, we are prompted to enter a password for the MySQL root account.
Next, we are going to create a new database user and a new database. We use the mysql
client.
$ service mysql status mysql start/running, process 1238
We check if the MySQL server is running. If not, we need to start the server. On Ubuntu Linux, this can be done with the service mysql start
command.
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 30 Server version: 5.0.67-0ubuntu6 (Ubuntu) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | +--------------------+ 2 rows in set (0.00 sec)
We use the mysql monitor client application to connect to the server. We connect to the database using the root account. We show all available databases with the SHOW DATABASES
statement.
mysql> CREATE DATABASE testdb; Query OK, 1 row affected (0.02 sec)
We create a new testdb
database. We will use this database throughout the tutorial.
mysql> CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'test623'; Query OK, 0 rows affected (0.00 sec) mysql> USE testdb; Database changed mysql> GRANT ALL ON testdb.* TO 'testuser'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> quit; Bye
We create a new database user. We grant all privileges to this user for all tables of the testdb
database.
Definitions
ADO.NET
is an important part of the .NET framework. It is a specification that unifies access to relational databases, XML files and other application data. A MySQL Connector/Net
is an implementation of the ADO.NET specification for the MySQL database. It is a driver written in C# language and is available for all .NET languages.
The Connection
, Command
, DataReader
, DataSet
, and DataProvider
are the core elements of the .NET data provider model. The Connection
creates a connection to a specific data source. The Command
object executes an SQL statement against a data source. The DataReader
reads streams of data from a data source. The DataSet
object is used for offline work with a mass of data. It is a disconnected data representation that can hold data from a variety of different sources. Both DataReader
and DataSet
are used to work with data; they are used under different circumstances. If we only need to read the results of a query, the DataReader
is the better choice. If we need more extensive processing of data, or we want to bind a Winforms
control to a database table, the DataSet
is preferred.
MySQL version
If the following program runs OK, then we have everything installed OK. We check the version of the MySQL server.
Option Strict On Imports MySql.Data.MySqlClient Module Example Sub Main() Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(cs) Try conn.Open() Console.WriteLine("MySQL version : {0}", conn.ServerVersion) Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) Finally conn.Close() End Try End Sub End Module
We connect to the database and get some info about the MySQL server.
Imports MySql.Data.MySqlClient
We import the elements of the MySQL data provider.
Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623"
This is the connection string. It is used by the data provider to establish a connection to the database. We specify the database name, host, user name and password.
Dim conn As New MySqlConnection(cs)
A MySQLConnection
object is created. This object is used to open a connection to a database.
conn.Open()
This line opens the connection.
Console.WriteLine("MySQL version : {0}", conn.ServerVersion)
Here we print the version of MySQL using the ServerVersion
property of the connection object.
Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString())
In case of an exception, we print the error message to the console.
$ vbnc -r:/usr/lib/cli/MySql.Data-6.1/MySql.Data.dll connect.vb
We compile our example. A path to the MySQL connector DLL is provided.
$ ./connect.exe MySQL version : 5.1.41-3ubuntu12.6
This is the output of the program on my system.
A more complex program follows.
Option Strict On Imports MySql.Data.MySqlClient Module Example Sub Main() Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim stm As String = "SELECT VERSION()" Dim version As String Dim conn As MySqlConnection Try conn = New MySqlConnection(cs) conn.Open() Dim cmd As MySqlCommand = New MySqlCommand(stm, conn) version = Convert.ToString(cmd.ExecuteScalar()) Console.WriteLine("MySQL version: {0}", version) Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) Finally conn.Close() End Try End Sub End Module
We check for the version of the MySQL database. This time using an SQL query.
Dim stm As String = "SELECT VERSION()"
This is the SQL SELECT
statement. It returns the version of the database. The VERSION()
is a built-in MySQL function.
Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
The MySqlCommand
is an object, which is used to execute a query on the database. The parameters are the SQL statement and the connection object.
version = Convert.ToString(cmd.ExecuteScalar())
There are queries which return only a scalar value. In our case, we want a simple string specifying the version of the database. The ExecuteScalar()
is used in such situations. We avoid the overhead of using more complex objects.
$ ./connect2.exe MySQL version : 5.1.41-3ubuntu12.6
Same result as in the previous example.
Creating and populating tables
Next we are going to create database tables and fill them with data. These tables will be used throughout this tutorial.
DROP TABLE IF EXISTS Books, Authors; CREATE TABLE IF NOT EXISTS Authors(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25)) ENGINE=INNODB; INSERT INTO Authors(Id, Name) VALUES(1, 'Jack London'); INSERT INTO Authors(Id, Name) VALUES(2, 'Honore de Balzac'); INSERT INTO Authors(Id, Name) VALUES(3, 'Lion Feuchtwanger'); INSERT INTO Authors(Id, Name) VALUES(4, 'Emile Zola'); INSERT INTO Authors(Id, Name) VALUES(5, 'Truman Capote'); CREATE TABLE IF NOT EXISTS Books(Id INT PRIMARY KEY AUTO_INCREMENT, AuthorId INT, Title VARCHAR(100), FOREIGN KEY(AuthorId) REFERENCES Authors(Id) ON DELETE CASCADE) ENGINE=INNODB; INSERT INTO Books(Id, AuthorId, Title) VALUES(1, 1, 'Call of the Wild'); INSERT INTO Books(Id, AuthorId, Title) VALUES(2, 1, 'Martin Eden'); INSERT INTO Books(Id, AuthorId, Title) VALUES(3, 2, 'Old Goriot'); INSERT INTO Books(Id, AuthorId, Title) VALUES(4, 2, 'Cousin Bette'); INSERT INTO Books(Id, AuthorId, Title) VALUES(5, 3, 'Jew Suess'); INSERT INTO Books(Id, AuthorId, Title) VALUES(6, 4, 'Nana'); INSERT INTO Books(Id, AuthorId, Title) VALUES(7, 4, 'The Belly of Paris'); INSERT INTO Books(Id, AuthorId, Title) VALUES(8, 5, 'In Cold blood'); INSERT INTO Books(Id, AuthorId, Title) VALUES(9, 5, 'Breakfast at Tiffany');
We have a books.sql
file. It creates two database tables: Authors
, and Books
. The tables are of InnoDB type. InnoDB databases support foreign key constraints and transactions. We place a foreign key constraint on the AuthorId
column of the Books
table. We fill the tables with initial data.
mysql> source books.sql Query OK, 0 rows affected (0.07 sec) Query OK, 0 rows affected (0.12 sec) Query OK, 1 row affected (0.04 sec) ...
We use the source command to execute the books.sql
script.
In the following example, we are going to insert a new author into the Authors
table.
Option Strict On Imports MySql.Data.MySqlClient Module Example Sub Main() Dim connString As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(connString) Dim cmd As New MySqlCommand() Try conn.Open() cmd.Connection = conn cmd.CommandText = "INSERT INTO Authors(Name) VALUES(@Name)" cmd.Prepare() cmd.Parameters.AddWithValue("@Name", "Trygve Gulbranssen") cmd.ExecuteNonQuery() conn.Close() Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) End Try End Sub End Module
We add a new author to the Authors
table. We use a parameterized command.
cmd.CommandText = "INSERT INTO Authors(Name) VALUES(@Name)" cmd.Prepare()
Here we create a prepared statement. When we write prepared statements, we use placeholders instead of directly writing the values into the statements. Prepared statements are faster and guard against SQL injection attacks. The @Name
is a placeholder, which is going to be filled later.
cmd.Parameters.AddWithValue("@Name", "Trygve Gulbranssen")
A value is bound to the placeholder.
cmd.ExecuteNonQuery()
The prepared statement is executed. We use the ExecuteNonQuery()
method of the MySQLCommand
object when we don't expect any data to be returned. This is when we create databases or execute INSERT
, UPDATE
, DELETE
statements.
$ ./prepared.exe mysql> select * from Authors; +----+--------------------+ | Id | Name | +----+--------------------+ | 1 | Jack London | | 2 | Honore de Balzac | | 3 | Lion Feuchtwanger | | 4 | Emile Zola | | 5 | Truman Capote | | 6 | Trygve Gulbranssen | +----+--------------------+ 6 rows in set (0.00 sec)
We have a new author inserted into the table.
Retrieving data with MySqlDataReader
The MySqlDataReader
is an object used to retrieve data from the database. It provides fast, forward-only, read-only access to query results. It is the most efficient way to retrieve data from tables.
Option Strict On Imports MySql.Data.MySqlClient Module Example Sub Main() Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(cs) Try conn.Open() Dim stm As String = "SELECT * FROM Authors" Dim cmd As MySqlCommand = New MySqlCommand(stm, conn) Dim reader As MySqlDataReader = cmd.ExecuteReader() While reader.Read() Console.WriteLine(reader.GetInt32(0) & ": " _ & reader.GetString(1)) End While reader.Close() Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) Finally conn.Close() End Try End Sub End Module
We get all authors from the Authors
table and print them to the console.
Dim reader As MySqlDataReader = cmd.ExecuteReader()
To create a MySQLDataReader
, we must call the ExecuteReader()
method of the MySqlCommand
object.
While reader.Read() Console.WriteLine(reader.GetInt32(0) & ": " _ & reader.GetString(1)) End While
The Read()
method advances the data reader to the next record. It returns true if there are more rows; otherwise false. We can retrieve the value using the array index notation, or use a specific method to access column values in their native data types. The latter is more efficient.
reader.Close()
Always call the Close()
method when done reading.
$ ./read.exe 1: Jack London 2: Honore de Balzac 3: Lion Feuchtwanger 4: Emile Zola 5: Truman Capote 6: Trygve Gulbranssen
This is the output of the example.
Column headers
Next we will show, how to print column headers with the data from the database table.
Option Strict On Imports MySql.Data.MySqlClient Module Example Sub Main() Dim connString As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(connString) Try conn.Open() Dim stm As String = "SELECT Name, Title From Authors, " _ & "Books WHERE Authors.Id=Books.AuthorId" Dim cmd As MySqlCommand = New MySqlCommand(stm, conn) Dim reader As MySqlDataReader = cmd.ExecuteReader() Console.WriteLine("{0} {1}", reader.GetName(0), _ reader.GetName(1).PadLeft(18)) While reader.Read() Console.WriteLine(reader.GetString(0).PadRight(18) _ & reader.GetString(1)) End While reader.Close() Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) Finally conn.Close() End Try End Sub End Module
In this program, we select authors from the Authors
table and their books from the Books
table.
Dim stm As String = "SELECT Name, Title From Authors, " _ & "Books WHERE Authors.Id=Books.AuthorId"
This is the SQL statement which joins authors with their books.
Dim reader As MySqlDataReader = cmd.ExecuteReader()
We create a MySqlDataReader
object.
Console.WriteLine("{0} {1}", reader.GetName(0), _ reader.GetName(1).PadLeft(18))
We get the names of the columns with the GetName()
method of the reader. The PadLeft()
method returns a new string of a specified length in which the beginning of the current string is padded with spaces. We use this method to align strings properly.
While reader.Read() Console.WriteLine(reader.GetString(0).PadRight(18) _ & reader.GetString(1)) End While
We print the data that was returned by the SQL statement to the terminal.
$ ./columns.exe Name Title Jack London Call of the Wild Jack London Martin Eden Honore de Balzac Old Goriot Honore de Balzac Cousin Bette Lion Feuchtwanger Jew Suess Emile Zola Nana Emile Zola The Belly of Paris Truman Capote In Cold blood Truman Capote Breakfast at Tiffany
Ouput of the program.
DataSet & MySqlDataAdapter
A DataSet
is a copy of the data and the relations among the data from the database tables. It is created in memory and used when extensive processing on data is needed or when we bind data tables to a Winforms control. When the processing is done, the changes are written to the data source. A MySqlDataAdapter
is an intermediary between the DataSet
and the data source. It populates a DataSet
and resolves updates with the data source.
Option Strict On Imports System.Data Imports MySql.Data.MySqlClient Module Example Sub Main() Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(cs) Dim stm As String = "SELECT * FROM Authors" Try conn.Open() Dim da As New MySqlDataAdapter(stm, conn) Dim ds As New DataSet da.Fill(ds, "Authors") Dim dt As DataTable = ds.Tables("Authors") dt.WriteXml("authors.xml") For Each row As DataRow In dt.Rows For Each col As DataColumn In dt.Columns Console.WriteLine(row(col)) Next Console.WriteLine("".PadLeft(20, "=")) Next Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) Finally conn.Close() End Try End Sub End Module
We print the authors from the Authors
table. This time, we use the MySqlDataAdapter
and DataSet
objects.
Dim da As New MySqlDataAdapter(stm, conn)
A MySqlDataAdapter
object is created. It takes an SQL statement and a connection as parameters.
Dim ds As New DataSet da.Fill(ds, "Authors")
We create and fill the DataSet
.
Dim dt As DataTable = ds.Tables("Authors")
We get the table called Authors
. We have given a DataSet
only one table, but it can contain multiple tables.
dt.WriteXml("authors.xml")
We write the data to an XML file.
For Each row As DataRow In dt.Rows For Each col As DataColumn In dt.Columns Console.WriteLine(row(col)) Next Console.WriteLine("".PadLeft(20, "=")) Next
We display the contents of the Authors
table to the terminal. To traverse the data, we utilize the rows and columns of the DataTable
object.
In the next example, we are going to bind a table to a Winforms DataGrid
control.
Option Strict On Imports System.Windows.Forms Imports System.Drawing Imports System.Data Imports MySql.Data.MySqlClient Public Class WinVBApp Inherits Form Private dg As DataGrid Private da As MySqlDataAdapter Private ds As DataSet Public Sub New() Me.Text = "DataGrid" Me.Size = New Size(350, 300) Me.InitUI() Me.InitData() Me.CenterToScreen() End Sub Private Sub InitUI() dg = New DataGrid dg.CaptionBackColor = System.Drawing.Color.White dg.CaptionForeColor = System.Drawing.Color.Black dg.CaptionText = "Authors" dg.Location = New Point(8, 0) dg.Size = New Size(350, 300) dg.TabIndex = 0 dg.Parent = Me End Sub Private Sub InitData() Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(cs) Dim stm As String = "SELECT * FROM Authors" ds = New DataSet Try conn.Open() da = New MySqlDataAdapter(stm, conn) da.Fill(ds, "Authors") dg.DataSource = ds.Tables("Authors") Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) Finally conn.Close() End Try End Sub Public Shared Sub Main() Application.Run(New WinVBApp) End Sub End Class
In this example, we bind a Authors
table to a Winforms DataGrid
control.
Imports System.Windows.Forms Imports System.Drawing
These two namespaces are for the GUI.
Me.InitUI() Me.InitData()
Inside the InitUI()
method, we build the user interface. In the InitData()
method, we connect to the database, retrieve the data into the DataSet
and bind it to the DataGrid
control.
dg = New DataGrid
The DataGrid
control is created.
Dim stm As String = "SELECT * FROM Authors"
We will display the data from the Authors
table in the DataGrid
control.
dg.DataSource = ds.Tables("Authors")
We bind the DataSource
property of the DataGrid
control to the chosen table.
vbnc -r:/usr/lib/mono/2.0/System.Windows.Forms.dll -r:/usr/lib/cli/MySql.Data-6.1/MySql.Data.dll grid.vb
To compile the example, we must include two DLLs. The DLL for the Winforms and the DLL for the MySQL connector.

Transaction support
A transaction
is an atomic unit of database operations against the data in one or more databases. The effects of all the SQL statements in a transaction can be either all committed to the database or all rolled back.
The MySQL database has different types of storage engines. The most common are the MyISAM and the InnoDB engines. The MyISAM is the default one. There is a trade-off between data security and database speed. The MyISAM tables are faster to process and they do not support transactions. On the other hand, the InnoDB tables are more safe against the data loss. They support transactions. They are slower to process.
Option Strict On Imports MySql.Data.MySqlClient Module Example Sub Main() Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(cs) Dim cmd As New MySqlCommand() Dim tr As MySqlTransaction Try conn.Open() tr = conn.BeginTransaction() cmd.Connection = conn cmd.Transaction = tr cmd.CommandText = "UPDATE Authors SET Name = 'Leo Tolstoy' WHERE Id = 1" cmd.ExecuteNonQuery() cmd.CommandText = "UPDATE Books SET Title = 'War and Peace' WHERE Id = 1" cmd.ExecuteNonQuery() cmd.CommandText = "UPDATE Books SET Titl = 'Anna Karenina' WHERE Id = 2" cmd.ExecuteNonQuery() tr.Commit() conn.Close() Catch ex As MySqlException tr.Rollback() Console.WriteLine("Error: " & ex.ToString()) End Try End Sub End Module
In this program, we want to change the name of the author on the first row of the Authors
table. We must also change the books associated with this author. A good example where a transaction is necessary. If we change the author and do not change the author's books, the data is corrupted.
Dim tr As MySqlTransaction
The MySqlTransaction
is an object for working with transactions.
tr = conn.BeginTransaction()
We begin a transaction.
cmd.CommandText = "UPDATE Books SET Titl = 'Anna Karenina' WHERE Id = 2" cmd.ExecuteNonQuery()
The third SQL statement has an error. There is no Titl column in the table.
tr.Commit()
If there is no exception, the transaction is committed.
Catch ex As MySqlException tr.Rollback() Console.WriteLine("Error: " & ex.ToString())
In case of an exception, the transaction is rolled back. No changes are committed to the database.
$ ./transaction.exe Error: MySql.Data.MySqlClient.MySqlException: Unknown column 'Titl' in 'field list' at MySql.Data.MySqlClient.MySqlStream.ReadPacket () [0x00000] at MySql.Data.MySqlClient.NativeDriver.ReadResult () [0x00000] mysql> SELECT Name, Title From Authors, Books WHERE Authors.Id=Books.AuthorId; +-------------------+----------------------+ | Name | Title | +-------------------+----------------------+ | Jack London | Call of the Wild | | Jack London | Martin Eden | | Honore de Balzac | Old Goriot | | Honore de Balzac | Cousin Bette | | Lion Feuchtwanger | Jew Suess | | Emile Zola | Nana | | Emile Zola | The Belly of Paris | | Truman Capote | In Cold blood | | Truman Capote | Breakfast at Tiffany | +-------------------+----------------------+ 9 rows in set (0.00 sec)
An exception was thrown. The transaction was rolled back and no changes took place.
However, without a transaction, the data is not safe.
Option Strict On Imports MySql.Data.MySqlClient Module Example Sub Main() Dim cs As String = "Database=testdb;Data Source=localhost;" _ & "User Id=testuser;Password=test623" Dim conn As New MySqlConnection(cs) Dim cmd As New MySqlCommand() Try conn.Open() cmd.Connection = conn cmd.CommandText = "UPDATE Authors SET Name = 'Leo Tolstoy' WHERE Id = 1" cmd.ExecuteNonQuery() cmd.CommandText = "UPDATE Books SET Title = 'War and Peace' WHERE Id = 1" cmd.ExecuteNonQuery() cmd.CommandText = "UPDATE Books SET Titl = 'Anna Karenina' WHERE Id = 2" cmd.ExecuteNonQuery() conn.Close() Catch ex As MySqlException Console.WriteLine("Error: " & ex.ToString()) End Try End Sub End Module
We have the same example. This time, without the transaction support.
$ ./update.exe Error: MySql.Data.MySqlClient.MySqlException: Unknown column 'Titl' in 'field list' at MySql.Data.MySqlClient.MySqlStream.ReadPacket () [0x00000] at MySql.Data.MySqlClient.NativeDriver.ReadResult () [0x00000] mysql> SELECT Name, Title From Authors, Books WHERE Authors.Id=Books.AuthorId; +-------------------+----------------------+ | Name | Title | +-------------------+----------------------+ | Leo Tolstoy | War and Peace | | Leo Tolstoy | Martin Eden | | Honore de Balzac | Old Goriot | | Honore de Balzac | Cousin Bette | | Lion Feuchtwanger | Jew Suess | | Emile Zola | Nana | | Emile Zola | The Belly of Paris | | Truman Capote | In Cold blood | | Truman Capote | Breakfast at Tiffany | +-------------------+----------------------+ 9 rows in set (0.00 sec)
An exception is thrown again. Leo Tolstoy did not write Martin Eden. The data is corrupted.
This was the MySQL Visual Basic tutorial, with MySQL Connector. You might be also interested in MySQL C API tutorial , MySQL Python tutorial or MySQL PHP tutorial .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论