Click here to Skip to main content
11,928,740 members (52,315 online)
Click here to Skip to main content
Add your own
alternative version


73 bookmarked

VB.NET to MySQL Database Connection

, 18 Jun 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Connecting from VB.NET to a MySQL database.


I'm currently developing an inventory management system which caters to multiple warehouses. I will share with you some tips and tricks in the development of this program. The system is still under development.

The database I use is MySQL Server 5.0.x together with the MySQL .NET Connector. Firstly, I'll share a few tricks about connecting to MySQL. Discussions on saving, querying, and other useful tricks will follow later on.

Using the code

To be able to create this program, you must have basic knowledge concerning MySQL 5.0 as well as VB.NET.



  1. Install MySQL Server 5.0 and and MySQL .NET Connector.
  2. Import a reference of the MySQL >NET Conenctor in Project Settings.
  3. Go to Settings and type the following settings:
  4. Name         Type      Scope   Value
    myDB ->      String -> User -> DatabaseName
    myServer->   String -> User -> Servername
    myUsername-> String -> User -> DbUserName
    myPassword-> String -> User -> DbPassword
    myPort->     String -> User -> mySQLPort(3306)

  5. Change the values in your MySQL database settings. The purpose of changing the setting is that if the MySQL database settings will change, e.g., the server is migrated, you can easily change the server settings and you won't have to rebuild the project.
  6. Add a Module in your project and name it mdlDataConn6. Paste the following code in your Module:
  7. Imports MySql.Data.MySqlClient
    Module mdlDataConn
        Public conn As New MySqlConnection
        Public Sub ConnectDatabase()
                If conn.State = ConnectionState.Closed Then
                    conn.ConnectionString = "DATABASE=" & My.Settings.myDB & ";" _
                      & "SERVER=" & My.Settings.myServer & ";user id=" & My.Settings.myUsername _
                      & ";password=" & My.Settings.myPassword &";port=" & _
                      My.Settings.myPort & ";charset=utf8"
                End If
            Catch myerror As Exception
                MessageBox.Show("Error Connecting to the database", "Error Database Server", _
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try
        End Sub
        Public Sub DisconnectDatabase()
            Catch myerror As MySql.Data.MySqlClient.MySqlException
            End Try
        End Sub
    End Module
    'To connect to mySQL Database just call.
    'To close the mySQL Database

That's all!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Philippines Philippines
Jayson Pascual is the software developer of the POS Technology Solution located in Philippines. He was more than 4 years in experience developing in business system such as POS, Inventory, Accounting, and etc. He start developing business system at age of 17 using VB6 and MS SQL Server 2000 and current using VB2005 and MySQL 5.0 to reduce cost and maintenance for the SME's Businesses. \

He currently serving SME's in the Philippines and developing easiest as possible business system because most of the employees and employers are first time user in computer.

You may also be interested in...

Comments and Discussions

Questionplease i need help here Pin
Member 117464859-Jun-15 10:20
memberMember 117464859-Jun-15 10:20 
Question'&amp' not declared Pin
Beninda5-Dec-13 1:27
memberBeninda5-Dec-13 1:27 
AnswerRe: '&amp' not declared Pin
Mark_essex27-Dec-13 8:13
memberMark_essex27-Dec-13 8:13 
QuestionError (amp;) Pin
ZoZZ8819-Jun-13 15:18
memberZoZZ8819-Jun-13 15:18 
QuestionHow enumerated MySQL server instances on the network? Pin
Gregorio Cobá14-Jul-09 20:14
memberGregorio Cobá14-Jul-09 20:14 
GeneralHelp! I'm a desperate beginner :( Pin
DarkRaven102416-Nov-07 0:35
memberDarkRaven102416-Nov-07 0:35 
QuestionSample SQL query codes Pin
M Sopian H17-Sep-07 17:45
memberM Sopian H17-Sep-07 17:45 
AnswerRe: Sample SQL query codes Pin
fdchiu24-Sep-10 22:11
memberfdchiu24-Sep-10 22:11 
QuestionMysql 4.2 Pin
zubinkasad26-Jun-07 22:52
memberzubinkasad26-Jun-07 22:52 
AnswerRe: Mysql 4.2 Pin
Irwan Hassan27-Jun-07 4:17
memberIrwan Hassan27-Jun-07 4:17 
GeneralRe: Mysql 4.2 Pin
zubinkasad27-Jun-07 4:36
memberzubinkasad27-Jun-07 4:36 
GeneralRe: Mysql 4.2 Pin
Irwan Hassan27-Jun-07 16:16
memberIrwan Hassan27-Jun-07 16:16 
GeneralRe: Mysql 4.2 Pin
zubinkasad27-Jun-07 19:46
memberzubinkasad27-Jun-07 19:46 
Ok here i go

what i want is there are 2 gridview on same page.

lets give name gridview1 and gridview2

i am populating gridview1 by selecting

Brand (which is listbox)
Category (which is dropdownbox)
Product (which is textbox)

now selecting all this option i will populate gridview1.
There is select link on each gridview1 row

Now what i want is on selecting row by clicking on select link that row or record should be displayed in gridview2

likewise user will keep on changing the options and can populate gridview1 with other rows.

So gridview2 should hold all the records which are selected by the users. After he finishes it off by selecting the records all the records in gridview2 should be stored in database.

I hope i am clear about the problem

If you want i can send you snapshot of the page which you can see it, but i dont think there is any link where i can attach snapshot.

Thanks for your help

GeneralRe: Mysql 4.2 Pin
Irwan Hassan28-Jun-07 18:52
memberIrwan Hassan28-Jun-07 18:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 18 Jun 2007
Article Copyright 2007 by jopascual
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid