กลับหน้าหลัก
views
How to Connect Visual Basic with MySQL on XAMPP Server
Last updated on

How to Connect Visual Basic with MySQL on XAMPP Server


How to Connect Visual Basic with MySQL on XAMPP Server

This tutorial shows step-by-step how to connect Visual Basic (.NET) with MySQL database running on XAMPP Server. Practical and straightforward.

Prerequisites

  • XAMPP installed with MySQL service running
  • Visual Studio (VB.NET) ready to use
  • MySQL Connector for .NET

Step 1: Prepare Database in XAMPP

Open phpMyAdmin in browser at http://localhost/phpmyadmin, then create a database and table as needed. In this example, we use a database named databook

screenshot of phpMyAdmin showing sidebar on the left, Database tab selected and new database creation field

Step 2: Download MySQL Connector

Download MySQL.Data.dll from MySQL website version compatible with your .NET Framework, or use NuGet Package Manager in Visual Studio.

Step 3: Add Reference to Project

  1. Open VB.NET project in Visual Studio
  2. Right-click on References → Add Reference
  3. Browse to the mysql.data.dll file you downloaded
  4. Click OK to add it

[image: Example image of Solution Explorer in VS expanding References and showing MySql.Data added]

Step 4: Write Connection Code

Open a Form with a button for testing connection and write this code:

Imports MySql.Data.MySqlClient

Public Class Form1
    Private MysqlConn As MySqlConnection
    Private COMMAND As MySqlCommand
    
    Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
        MysqlConn = New MySqlConnection()
        
        ' Set Connection String according to your XAMPP config
        MysqlConn.ConnectionString = "server=localhost;" &_
                                    "userid=root;" &_
                                    "password=11111;" &_
                                    "database=databook;" &_
                                    "Character Set=utf8;"
        Try
            MysqlConn.Open()
            
            Dim Query As String
            Query = "SELECT * FROM data.edata WHERE condition"
            
            COMMAND = New MySqlCommand(Query, MysqlConn)
            
            ' TODO: Read data from COMMAND and display
            ' e.g., using MySqlDataAdapter or DataReader
            
            MessageBox.Show("Connection Successful")
            
            MysqlConn.Close()
            
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message)
        Finally
            MysqlConn.Dispose()
        End Try
    End Sub
End Class

Points to modify:

  • Change password=11111 to your actual XAMPP MySQL password
  • Change database=databook to your actual database name
  • Modify Query = "SELECT * FROM ..." to match your real table structure

[image: flow diagram showing: Button Click → Create Connection → Set ConnectionString → Open → Execute Query → Display Results → Close/Dispose]

Important Notes

  • Make sure XAMPP MySQL service is running. If stopped, VB cannot connect.
  • Newer XAMPP versions have empty default root password, not 11111.
  • Using Try-Catch-Finally is essential to handle potential errors.

Reference Video

https://www.youtube.com/embed/cEljPk7swwI

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...