Accessing MySQL Database in VB.NET
In this article, I will show you how to access MySQL server database using ODBC data provider. There are two ways to access MySQL Server database using ODBC data providers. First, using ODBC data source name and second by passing connection string direct in the OdbcConnection object.
The text of this article is not in this database — only its details are. The old site it was published on is gone, but the Internet Archive kept a copy: Accessing MySQL Database in VB.NET
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

badin aranasPosted Jul 25, 2011, 4:34 AM
Hi, I want to use wampserver as our database for vb.net. Our project is all about library system. How can I do this? Can someone help me to do this for me. Thanks. Best Regards, jenie aranas
NoaheditedPosted Apr 1, 2011, 2:25 AMEdited Apr 1, 2011, 2:42 AM
Hi Mahesh Sir, i need help urgently on this please. “how to fetch time field values from mysql database through vb.net code??” i need to get those values and calculate running time for the day. +————-+————–+——+—–+————+——-+ here is the piece of code im currently working on. +————-+————–+——+—–+————+——-+ strQuery = “select IMEI,Pos_Date,Pos_Time,CountryCode,NetC,LAC,Cellid,SigStr,BattStr,Fuel_Level,ign,AI1,AI2,AI3,DI1,DG2,DG3,status from gensetfuel where imei=’” & imei & “‘ and Pos_Date=’” & dateSelected & “‘ Order by Pos_Time” +————-+————–+——+—–+————+——-+ sqlcomm = New MySqlCommand(strQuery, sqlcon) sqlreader = sqlcomm.ExecuteReader sqlcomm = New MySqlCommand(strQuery, sqlcon) sqlreader = sqlcomm.ExecuteReader +————-+————–+——+—–+————+——-+ If sqlreader.HasRows Then While sqlreader.Read() +————-+————–+——+—–+————+——-+ posDate = sqlreader.GetDateTime(1).ToString(“dd-MMM-yy”) If (firstreading) Then prevtime = sqlreader.GetDateTime(2) prevMovtime = prevtime firstreading = False End If +————-+————–+——+—–+————+——-+ time = sqlreader.GetDateTime(2) ignstate = sqlreader.GetValue(17) TimeDuration = DateDiff(DateInterval.Minute, prevtime, time) If TimeDuration > 10 Then ‘some code…. +————-+————–+——+—–+————+——-+ +————-+————–+——+—–+————+——-+ the values i am getttin for time and prevtime variables in vb.net code is 12:00:00AM and when i use datediff function i get the result as 0 database description of the table used is as: +————-+————–+——+—–+————+——-+ | Field | Type | Null | Key | Default | Extra | +————-+————–+——+—–+————+——-+ | IMEI | varchar(100) | NO | PRI | | | | Pos_Date | date | NO | PRI | 0000-00-00 | | | Pos_Time | time | NO | PRI | 00:00:00 | | | CountryCode | varchar(100) | YES | | NULL | | | NetC | varchar(100) | YES | | NULL | | | LAC | varchar(100) | YES | | NULL | | | Cellid | varchar(100) | YES | | NULL | | | SigStr | varchar(100) | YES | | NULL | | | BattStr | varchar(100) | YES | | NULL | | | Fuel_Level | varchar(100) | YES | | NULL | | | ign | varchar(100) | YES | | NULL | | | AI1 | varchar(100) | YES | | NULL | | | AI2 | varchar(100) | YES | | NULL | | | AI3 | varchar(100) | YES | | NULL | | | DI1 | varchar(100) | YES | | NULL | | | DG2 | varchar(100) | YES | | NULL | | | DG3 | varchar(100) | YES | | NULL | | | ser_inp | varchar(100) | YES | | NULL | | | status | varchar(100) | YES | | NULL | | +————-+————–+——+—–+————+——-+ +————-+————–+——+—–+————+——-+ the query mention above in the comment, strQuery=”select….” fetches me the correct set of records when fired on mysql command line client….........but its not giving the correct result through vb.net code…............. please help asap…............... thanks a tons in advance…...........vl b really obliged :o)
cris dennis cuacaeditedPosted Aug 25, 2010, 9:58 AMEdited Aug 25, 2010, 7:29 PM
i hav vb.net 2005 and MySql as my database.. if anyone could share there code and sample appilcation on how to create a report Pls send me At [email protected] Or post it in here.. thanks..
winlove docorPosted Jun 26, 2010, 4:35 AM
I have this code, and whenever i try it to RUN, I always get an error and it's like this........ ODBCEXCEPTION WAS UNHANDLED ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.51b-community-nt]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'char VALUES('101','noodles')' at line 1 How can i solve this..? below is my complete codes.... and by the way i can't see MySql Odbc in my data source and if i try to add data source mysql doesn't appear.. I'm using VB2008 express edition... tnx -------------------------------------------------------------------------------------------------------------------- 'Imports System.ServiceProcess Imports System Imports Microsoft.Data.Odbc Imports System.Data.SqlClient Imports System.Data.SqlClient.SqlCommand Imports MySql.Data.MySqlClient Imports MySql.Data Imports System.Data Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MyConString As String = "DRIVER={MySQL ODBC 5.1 Driver};" & _ "SERVER=localhost;" & _ "DATABASE=char;" & _ "UID=root;" & _ "PASSWORD=;" & _ "OPTION=0;" Dim da As New OdbcDataAdapter Dim MyConnection As New OdbcConnection(MyConString) Dim MyCommand As New OdbcCommand MyConnection.Open() MyCommand.Connection = MyConnection MyCommand.CommandText = ("INSERT INTO char VALUES('101','1002')") MyCommand.ExecuteNonQuery() MyConnection.Close() MyConnection.Dispose() MyCommand.Dispose() End Sub End Class<!--Session data-->
Thobisa MonoPosted Apr 16, 2010, 1:35 PM
Can you explain this code please. Dim connectionString As String = "Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;" ' Dim conn As New OdbcConnection(connectionString) conn.Open()
Arnaud DupuisPosted Feb 15, 2010, 4:15 AM
Hi, Any idea how to use the Chart forms and Mysql in VB .Net 2008-Express? I’m referring to this library: http://code.msdn.microsoft.com/mschart I’m having trouble to bind the chart to my data, would be great if you can help Here is my code writen in the form containing the chart form: ———————————————— Imports MySql.Data.MySqlClient Imports MySql.Data Imports System.Data Public Class frmIVvsRLZ Private Sub cmdChart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChart.Click Dim rdr As MySqlDataReader Dim conn As New MySqlConnection Dim cmd As New MySqlCommand Dim myAdapter As New MySqlDataAdapter Dim SQL1 As String conn.ConnectionString = My.Settings.connectionString SQL1 = “SELECT CLOSINGDATE, HISTO10D FROM TBLHISTOVOLBBG WHERE EQUITY_ID=1845 ORDER BY CLOSINGDATE DESC” Try conn.Open() Try cmd.CommandText = SQL1 cmd.Connection = conn rdr = cmd.ExecuteReader chartHistoIVvsRLZ.DataSource = rdr chartHistoIVvsRLZ.DataBind() Catch myerror As MySqlException MsgBox(“There was an error reading from the database: ” & myerror.Message) End Try Catch myerror As MySqlException MessageBox.Show(“Error connecting to the database: ” & myerror.Message) Finally If conn.State ConnectionState.Closed Then conn.Close() End Try End Sub End Class -------------------------------- As well I teid this ------------------------------------------ Imports MySql.Data.MySqlClient Imports MySql.Data Imports System.Data Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim query As String = “SELECT CLOSINGDATE, HISTO10D FROM TBLHISTOVOLBBG WHERE EQUITY_ID=1845 ORDER BY CLOSINGDATE DESC” Dim connection As New MySqlConnection(My.Settings.connectionString) Dim da As New MySqlDataAdapter(query, connection) Dim ds As New DataSet() If da.Fill(ds) Then Chart1.DataSource = ds.Tables(0) Chart1.DataBind() End If connection.Close() Catch ex As Exception Console.WriteLine(ex.Message) End Try End Sub End Class
Damion RhodenPosted Aug 17, 2008, 11:32 PM
Mahesh, I notice that the codes for connecting to an access database are not as they were with VB6. I am trying to connect to an access database in ado.net. How is that done? That was a good article by the way!
samiullah kakarPosted Feb 9, 2008, 9:44 AM
dear sir i forgot something to tell you that im make connectiveties with sqlserver2000 toward that send me lectures please sir bye
samiullah kakarPosted Feb 9, 2008, 9:42 AM
Dear Sir my name is samiullah im student of computer science in afhanistan. Dear sir i know some basic thing about vb.net and i have developed some small databases like for shops and schools. dear sir i wanna to learn vb.net more here in afghanistan are not smart teachers that i learn something new,if you dont mind would you please send some lecture that i learn something from your notes i wil be very glad best regard samiullah
Mehrdad MomeniPosted Jan 28, 2008, 12:33 AM
hi mahesh I'm mehrdad from Iran Please tell me a reference for VB.net Database Sql server 2005 and asp.net thak you
nellPosted Dec 14, 2007, 9:09 AM
i was trying your code in listing 1 and i have this error on my vb.net code OdbcConnection not define what seems to be the problem?
kyoshiro mibuuPosted Nov 19, 2007, 10:12 PM
this article really is reliable. thanks !
srivatsan rPosted Oct 29, 2007, 10:18 AM
the article is useful.....
jhae magnoPosted Sep 27, 2007, 12:03 PM
how about... ADo.net and msql in to VB2005? & ADO.net and/or msql in to VCpp.net
rolin abd rashidPosted Mar 2, 2006, 12:48 AM
hi there... u mentioned in ur article : "To test this code, create a Windows application and add a DataGrid control to the form and write code listed in Listing 11-2 on the form load." but i can only see Listing 1. so where can i find the Lingting 11 and 12? thank you...