• June 12, 2021

PHSARIT

Bring Technology Ideas with you

 Database Connection using Vb.net programming

Some business needs to have their own database in order to make sure that they can manage their information in effective and efficient way.

    This is the trick for beginner who are willing to learn Database within VB.net programming. The purpose of the VB.net Programming is to use it as DBMS for various projects. Below there are three various connections from VB.net to MySQL Server, MS SQL Server, and MS Access. It mays help for some reasons:

    To connect to MS ACCESS and MS SQL SERVER seem to be easy for VB.net programming; however, for MySQL Server, you need to install mysql connector based on your Visual Studio, please visit the link https://dev.mysql.com/downloads/

    1. Connect to MS SQL Server: to connect to MSSQL, it needs to Imports System.Data.SqlClient

    See the full code below:

    Imports System.Data.SqlClient
    Public Class DatabaseConnect
        Dim cn As SqlConnection
        Private Sub DatabaseConnect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim conString As String = "Data Source =HEM_TOLA\SQLEXPRESS; Initial Catalog =SQL_SimpleSelect; Integrated Security = True"
            cn = New SqlConnection(conString)
            Try
                cn.Open()
                MessageBox.Show("Connected successfully", "Success", MessageBoxButtons.OK)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    End Class

    2. To connect to Microsoft Access, it needs to import one library is Imports System.Data.OleDb

    It needs to create one Access Database in Microsoft Access in your local project path (Ex:DBConnection.accdb)

      Create Database in local machine

      See the full code below:

      Imports System.Data.OleDb
      Public Class DatabaseConnect
          Dim cn As OleDbConnection
          Private Sub DatabaseConnect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
              Dim conString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\DBConnection.accdb"
              cn = New OleDbConnection(conString)
              Try
                  If cn.State = ConnectionState.Closed Then
                      cn.Open()
                      MessageBox.Show("Connected successfully", "Success", MessageBoxButtons.OK)
                  End If
              Catch ex As Exception
                  MessageBox.Show(ex.Message)
              End Try
          End Sub
      End Class

      3. To connect from VB.net to MySQL Server database, it needs to have reference as connector, called “MySQL Connector”, you can download and install into you machine. After installing, please go to your project solution then select your project name after that right click Add Reference

        Then in Extensions, select MySQL.Data as image below:

          Then Click Ok, after that go back to project solution under References Tab, it will show

            After the reference added to your project solution, on the top of your public class, please import the library as :Imports MySql.Data.MySqlClient

            The write the program to create connection. Please see the full code below:

            Imports MySql.Data.MySqlClient
            Public Class DatabaseConnect
                Dim cn As MySqlConnection
                Private Sub DatabaseConnect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                    Dim conString As String = "server=localhost; userid=root; password=; database=hrm"
                    cn = New MySqlConnection(conString)
                    Try
                        cn.Open()
                        MsgBox("Database connected successfully", vbInformation)
                    Catch ex As Exception
                        MessageBox.Show(ex.Message)
                    End Try
                End Sub
            End Class

            Please make sure you have install WAMP server for your Apache database

            In conclusion, please try your own way!

            Related post