Post: VB.net connect to mySQL Database Connection
07-31-2011, 01:34 PM #1
FrenziosMix
Who’s Jim Erased?
(adsbygoogle = window.adsbygoogle || []).push({}); In the next tutorial i will show you guys how to to make a Register and login Form that's Connected With your Database.

Thanks For Clicking here.

in This Tutorial i will show you how to connect Vb.net to Mysql.

[multipage=Introduction]

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.

[multipage=Start]


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)


You must login or register to view this content.


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.


The Code

    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

The following user thanked FrenziosMix for this useful post:

07-31-2011, 11:04 PM #2
Ameht!
Dark Knight
good shit homey but your talents are wasted on this site :(

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo