--> Skip to main content
Pelajar Koding

follow us

Connecting Vb.Net To Mysql Database Using Mysql Connector

Hi everyone!! This article wants to show you how to create a connection to MySQL database using MySQL connector. First, you have to download the MySQL Connector from this link: http://dev.mysql.com/downloads/connector/net/ For writing this article, I used MySQL Connector Net 6.5.4 version. Install MySQL Connector on our PC, then we can start using this connector. Let's start to create a new project solution, I'm using Visual Studio 2005. However, the codes are still relevant when I'm using the 2015 version. Files -> New -> Project... Choose Visual Basic --> Windows --> Windows Application, name the project,  then set file saving location. Finally, click OK button. A new project will be added with a form names Form1. Show All Files by clicking Icon as below. Right-click Reference --> Add Reference...  On  Add Reference  obrolan, click  Browse tab then explore path: C:\Program Files\MySQL\MySQL Connector Net 6.5.4\Assemblies\v2.0\MySql.Data.dll (Notes: adjust your MySQL Connector version and framework used in project properties) Click the  OK  button. MySQL.Data is added on Reference. Make sure that System.Data has been also added on Reference. Set your UI design as following image. Add a button named btnKoneksi. This is the code behind. Imports MySql.Data.MySqlClient Public Class Form1     Dim conn As MySqlConnection     Private Sub btnKoneksi_Click( ByVal sender As System.Object, _             ByVal e As System.EventArgs) Handles btnKoneksi.Click         conn = New MySqlConnection()         conn.ConnectionString = "server=localhost;user id=root;" & _                                 "password=;database=latihan"         Try             conn.Open()             MessageBox.Show( "Koneksi Berhasil" )             conn.Close()         Catch myerror As MySqlException             MessageBox.Show( "Error Koneksi: " & myerror.Message)         Finally             conn.Dispose()         End Try     End Sub End Class I set my database server on my local PC so the server name is localhost. If your database server is on another PC, you can call it with IP or Server name alias. I used the default user id, which is "root" with a blank password. Fill password if you have one. Latihan is my database name used for this sample. Let's try to Run/Debug then click "Cek Koneksi" button. A message box "Koneksi Berhasil" will appear if the connection is successfully opened. Check also the video, on this video I'm using VS 2019 Community Edition Next step would be displaying data on datagridview . FAQ: What if your database server is on web hosting? This is a different case, each hosting has its own privilege setting. But the general solution is using web service.
Sumber http://rani-irsan.blogspot.com

You Might Also Like:

Comment Policy: Silahkan tuliskan komentar Anda yang sesuai dengan topik postingan halaman ini. Komentar yang berisi tautan tidak akan ditampilkan sebelum disetujui.
Buka Komentar