65.9K
CodeProject is changing. Read more.
Home

MySQL Connector for Visual Basic

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Jan 17, 2015

CPOL

1 min read

viewsIcon

20512

downloadIcon

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:

    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:

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.

Now go to Properties DBaseMySQL1 and complete data connection.

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:

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

    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.