Click here to Skip to main content
6,594,932 members and growing! (15,965 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Beginner

VB.net to mySQL Database Connection

By jopascual

Connecting from VB.net to mySQL Database Connection
VB 8.0.NET 2.0, WinXP, Vista, .NET 3.0, ADO.NET, WinForms, MySQL, VS2005, Dev
Posted:18 Jun 2007
Views:79,392
Bookmarked:43 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.23 Rating: 4.06 out of 5

1
1 vote, 10.0%
2
2 votes, 20.0%
3
3 votes, 30.0%
4
4 votes, 40.0%
5

Introduction

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

The database I used was mySQL Server 5.0.x together with mySQL DotNet Connector. Firstly, I'll start to share few tricks in 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.

Prerequisite:
1. mySQL Server 5.0
2. mySQL Dot Net Connector 5.1
3. VB.net Express edition/ VB.net Family

Steps:
1. Install first mySQL Server 5.0 and and mySQL Dot Net Connector.
2. Import the reference of mySQL DotNet Conenctor. in the Project Settings3. Go to Settings and type the following settings:

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)




4. 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 was migrated, you can furthermore easily change server settings and you dont have to rebuild the project.
5. Add Module in your project and name it as mdlDataConn6. Paste the follwing code in your module.
Imports MySql.Data.MySqlClient

Module mdlDataConn
    Public conn As New MySqlConnection

    Public Sub ConnectDatabase()

        Try
            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"
                conn.Open()
            End If

        Catch myerror As Exception

MessageBox.Show("Error Connecting to the database", "Error Database
Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End
        End Try
    End Sub

    Public Sub DisconnectDatabase()
        Try
            conn.Close()
        Catch myerror As MySql.Data.MySqlClient.MySqlException

        End Try
    End Sub
End Module
'To connect to mySQL Database just call.
ConnectDatabase

'To close the mySQL Database
DisconnectDatabase 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

jopascual


Member
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.
Occupation: Web Developer
Location: Philippines Philippines

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralHow enumerated MySQL server instances on the network? PinmemberGregorio Cobá20:14 14 Jul '09  
GeneralHelp! I'm a desperate beginner :( PinmemberDarkRaven10240:35 16 Nov '07  
QuestionSample SQL query codes PinmemberM Sopian H17:45 17 Sep '07  
QuestionMysql 4.2 Pinmemberzubinkasad22:52 26 Jun '07  
AnswerRe: Mysql 4.2 PinmemberIrwan Hassan4:17 27 Jun '07  
GeneralRe: Mysql 4.2 Pinmemberzubinkasad4:36 27 Jun '07  
GeneralRe: Mysql 4.2 PinmemberIrwan Hassan16:16 27 Jun '07  
GeneralRe: Mysql 4.2 Pinmemberzubinkasad19:46 27 Jun '07  
GeneralRe: Mysql 4.2 PinmemberIrwan Hassan18:52 28 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jun 2007
Editor:
Copyright 2007 by jopascual
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project