Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

MySQL Connector for Visual Basic

4.00/5 (1 vote)
17 Jan 2015CPOL1 min read 20.5K   477  
MySQL Connector for Visual Basic, obtaining a data table

Introduction

When I have to make a source code into Visual Basic and connect to server MySQL, I make a long source code, I take a DLL provided MySQL, where I make call a MySqlConnection, MySqlDataAdapter and MySqlCommandBuilder class for example:

VB.NET
    Public T_MARCO As DataTable
    Public T_PREGUNTAS As DataTable
    Public CONEXION As String = "Data Source=servidor.com;User Id=root;
        Password=contraseƱa;pooling=false;Database=myBase;persist security info=True"
    Public Archivo As String

Public Function DB_Cargar(ByRef Datos As DataTable, 
                           ByVal sql As String, 
                           Optional ByVal Aviso As Boolean = True) As Boolean
       
    Dim conector As MySqlConnection
    Dim adaptador As MySqlDataAdapter
    Dim comandos As MySqlCommandBuilder
    Dim EstadoConexion As Boolean = False
        Try
            conector = New MySqlConnection(CONEXION)
            conector.Open()
            adaptador = New MySqlDataAdapter(sql, conector)
            Datos = New DataTable
            adaptador.Fill(Datos)
            adaptador.Dispose()
        Catch ex As Exception
            If Aviso Then MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
            EstadoConexion = True
        End Try
        conector.Dispose()

        Return EstadoConexion
    End Function

Incorporating the Connect into a Project

Install into Project

  1. Download the file zip.
  2. Go to your project at My Project in the referencias tab, add DBaseMySQL.dll and MySQL.Data.dll.
  3. Then go to your form, and in the box tool, search the Data tab.
  4. Click with right button and you'll choose element matter DBaseMySQL.dll.

Adding Link Controls

Before you begin, you must add the following to link DBaseMySQL.dll, with the reset of the controls:

VB.NET
Public Class Form1
    'THIS IS IMPORTANT
    Friend WithEvents Tabla As DataTable

    <system.diagnostics.debuggerstepthrough()> _
    Public Sub MisTablas()
        Me.Tabla = New DataTable
    End Sub
End Class

After the tool box, you add: BindingSource, BindingNavigator, DataGridView and DBaseMySQL.

Image 1

Now go to Properties DBaseMySQL1 and complete data connection.

Image 2

You must fill out the following fields:

  • Source: Server MySQL's
  • User: user name
  • Password: password
  • SelectCommand: the query string in SQL language
  • TablaDatos: This is a data table Friend WithEvents Tabla As DataTable.
  • Conectado: True

With this step, the connector is connected to the server and loaded in Table query. Which can bind the BindingSource using the DataSource property: Table and then the other controls at the same BindingSource being:

Image 3

To run the project, you must update the connection form adding to the charger:

VB.NET
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DBaseMySQL1.zConectar()
End Sub

I hope you found this tip helpful and I will accept suggestions, if any.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)