• September 22, 2021

PHSARIT

Bring Technology Ideas with you

 How to write function in VB.net to connect to MySQL Server Database

1. General Concepts:

There are a lot of database engine to store data table such as MySQL Server, Microsoft SQL Server, SQLLite, etc. Those are working as the RDBMS which is stand for Relational Database Management System. So How to connect from VB.net Programming to MySQL Server Database? As the programs seem to be separated from each other due to the difference companies. VB.net or Visual Basic Dot Net comes Microsoft Company whist MySQL Server might come from various sources of various companies. Therefore, in order to connect from VB.net to MySQL Server Database, please kindly visit to our previous tutorials Database Connection using Vb.net programming, you can either download the .DLL file library and start step by step to install it and connect VB.net and MySQL Server Database.

2. Software Requirement

To work with this tutorials, you need to have:

  1. Microsoft Visual Studio 2015 up to latest version
  2. WAMP Server or XAMPP Server ( these two software contain automatic MySQL Server Database)

    After the installation of WAMP Server, you will see the phpmyadmin in Tools after that click on that then you will see as the following photos

      By Default from MySQL Server Database, Username must be root and Password must be blanked. Server Choice, you can either select MySQL or MariaDB.

      3. Tutorials

      STEP 1: Going to MySQL Database, then create a database. Name it as houserentings. In MySQL Database, please Select on Database then name it and click on Create

        STEP 2: Go to Microsoft Visual Studio the Create a project. In your project, please create a new form

          Then Click on OK. After click on Ok, you can go to Form1 and them implement the code as below:

          Imports MySql.Data.MySqlClient
          Public cn As New MySqlConnection
              Dim cm As New MySqlCommand
              Dim result As Boolean
              Dim conString As String
              Dim sql As String
              Public Function OpenDB() As Boolean
                  Try
                      If cn.State = ConnectionState.Closed Then
                          conString = "server=localhost;user=root;password=;database=houserenting"
                          cn.ConnectionString = conString
                          cn.Open()
                          result = True
                          MsgBox("Connection Successfully", vbInformation)
                      End If
                  Catch ex As Exception
                      MessageBox.Show(ex.Message)
                      result = False
                  End Try
                  Return result
              End Function

          on the Header of the Form, please include Imports MySql.Data.MySqlClient. Then go to your Form1, on the Form_Load Event, please write the code below:

          Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                  OpenDB()
              End Sub

          The result from the compiling:

            Related post