Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations
Programming, VB.NET March 18th, 2009Perform SQL Operations
From the previous part, I have successfully connect to world database on MySQL Server from VB.NET. Next, I try to perform basic SQL operations (SELECT, INSERT, UPDATE and DELETE) on the world database.
Note: This post is continued from the previous part. If you start a new project, you’ll need to add MySql.Data reference. See the previous post for more detail.
You can see index of this series at Accessing MySQL on VB.NET using MySQL Connector/Net, Part I: Introduction
Section
- Declare a Connection String
- Retrieve Data from Database
- Update Record on Database
- Download Code
- Summary
Declare a Connection String
If you continue from the previous part, you simply move the connStr variable from TestConnection() method to global scope. Otherwise, declare a new global variable as connection string with the value below.
Note: For more detail about how to build a connection string, see the previous post.
1 2 3 4 | Private connStr As String = "Database=world;" & _ "Data Source=192.168.125.21;" & _ "User Id=worldUser;Password=worldpassword;" & _ "Connection Timeout=20" |
The code will look similar as the figure below.

Back to top
Retrieve data from database
To retrieve data from MySQL, I’ll use MySqlDataReader Class from MySql.Data library. First, I open the connection to the world database on MySQL Server. Then, executes the query command by using ExecuteReader method and assigns to MySqlDataReader object. After that, looping on MySqlDataReader object to get result. Let’s see the code below.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | Public Sub retriveData() Try Dim query As String = "SELECT * FROM Country" Dim connection As New MySqlConnection(connStr) Dim cmd As New MySqlCommand(query, connection) connection.Open() Dim reader As MySqlDataReader reader = cmd.ExecuteReader() While reader.Read() Console.WriteLine((reader.GetString(0) & ", " & _ reader.GetString(1))) End While reader.Close() connection.Close() Catch ex As Exception Console.WriteLine(ex.Message) End Try End Sub |
Code Explanation:
- Line 21: Create a query variable as string.
- Line 22: Create a MySQLConnection object with the defined connection string in global as parameter.
- Line 23: Create a MySQLCommand object with previous 2 variables as parameters.
- Line 25: Open a connection to MySQL Server using the defined connection string.
- Line 27-28: Call ExecuteReader() method and assign the result to MySqlDataReader
object. - Line 30-33: Looping on MySqlDataReader object to get results to the console.
- Line 35-36: Close the reader and connection. I recommend to close these objects after using everytime.
- Line 38: If there is any error in the method, send to console.
The code will look similar as the figure below.

The result of the query shows the first and second columns in the output window.

Back to top
Update Record on Database
Coding on INSERT, UPDATE and DELETE SQL operations are identical except only sql command that is executed. When I perform these operations to database, there is no need to get records from the database. So I use ExecuteNonQuery() Method from MySqlCommand Class. For INSERT, UPDATE and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.
Note: You can use ExecuteNonQuery to perform any type of database operation, however any result sets returned will not be available.
Update Function
This function accepts a parameter as sql command and send to execute on MySQL Server. So the function can be use for INSERT, UPDATE and DELETE operations also any operation that doesn’t need a return result sets. Also, it returns an integer value of affected rows.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | Function updateRecord(ByVal query As String) As Integer Try Dim rowsEffected As Integer = 0 Dim connection As New MySqlConnection(connStr) Dim cmd As New MySqlCommand(query, connection) connection.Open() rowsEffected = cmd.ExecuteNonQuery() connection.Close() Return rowsEffected Catch ex As Exception Console.WriteLine(ex.Message) End Try End Function |
Code Explanation:
- Line 50: The code is similar to retrieve data section only it call ExecuteNonQuery() method and the return value is affected rows.
The function code will look similar as the figure below.

Example INSERT
To execute INSERT command, try the statement below. The sql command will insert a new record to Country table on world database. Then, output the affected rows to console.
14 | Console.WriteLine(updateRecord("INSERT INTO Country (Code, Name) VALUES ('AAA','Test Name')")) |
Example UPDATE
The UPDATE command belows change Name to ‘Test2′ on row which has code = ‘AAA’. And output the affected rows to console.
15 | Console.WriteLine(updateRecord("UPDATE Country SET Name='Test2' WHERE Code ='AAA'")) |
Example DELETE
The DELETE command deletes a record which has code equals ‘AAA’. And output the affected rows to console.
16 | Console.WriteLine(updateRecord("DELETE FROM Country WHERE Code ='AAA'")) |
When I have run the application with the 3 statements above, the output window shows the row affected of each statement as the figure below.

Back to top
Download Code
You can download a complete project file at here – SampleMySQL (zip format). The project was created on Microsoft Visual Studio 2005.
Back to top
Summary
Now you have reach the end of the article. After 7 parts, you should be able to develop an simple application to access MySQL Server on your own. I think that the article is quite clear than other accessing database server articles that I wrote last year. If you have any question, feel free to leave a comment below.
Back to top
Related post
- Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations From Part I, I have only established a connection with local MySQL. Next I’ll show how to retrieve and modify...
- Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations From Part I, I have only established a connection with local SQL Server. Next I’ll show how to retrieve and...
- Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations This article is one of the series of Accessing Access 2007 on NetBeans 6.5 using JDBC. You can see the...
- Accessing MySQL on VB.NET using MySQL Connector/Net, Part VIII: Display Result on GUI Introduction Actually, I don’t prepare to write this post while I was writing the series. But there are some people...
- Accessing MySQL on VB.NET using MySQL Connector/Net, Part IV: Create & Grant MySQL User Account Create & Grant MySQL User Account By default, the root account on MySQL Server has all privileges on every tables...
Related posts:




March 25th, 2009 at 3:10 pm
hi i justwant to know that if we want to make a code in vb for inserting in an sql database for makina login form like user name and password so what will we do
March 28th, 2009 at 8:10 pm
Hi, Deepti bhatt
Simply develop project as Windows Form Application. Then, you design the login form and implement the code from this article. That’s it.
April 20th, 2009 at 5:56 am
Thank you for everything….this is sweet
But insteadof getting everything on Console I want it on text box or in a table form
could yopu help me
Anyways you have done a great job for sophomore
Thank you Sir!
April 21st, 2009 at 7:07 am
nice one,
but if i want to retrieve it on a datagrid??? could u help me out??
April 24th, 2009 at 3:18 pm
Hi, iam very gratefull that you did this article!
I have been searching fr such for days
cheer
Best Regards
liku
June 12th, 2009 at 1:06 am
thanks, a concise, accurate tutorial!
August 27th, 2009 at 10:02 am
how do i make a report on using component one in vb.net…
please show me the basic step
March 20th, 2010 at 5:44 am
Thank you, again an excellent article!
April 8th, 2010 at 4:12 am
Hi Linglom,
Thank you for all your help.
I have a small problem. The previous tutorial worked fine for me and I got the “Connection okay” message.
But after trying this code several times, I still get the following error:No symbols are loaded for any call stack frame. The source code cannot be displayed.
I have tried to google responses to this error because I’m new at using Visual Basic (2010) and don’t understand the error.
Please help!
May 19th, 2010 at 3:08 am
Hi, after trying this code several times (To retrieve data from MySQL), I can’t see the result, I’m using VisualBasic 2010 express, I have NO errors but I couldn’t see the register.
Thanks
May 20th, 2010 at 9:14 pm
Hi, Daemon
Check on your console windows when it runs, there may be an exception.