Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / Visual Basic
Tip/Trick

MySQL Connector for Visual Basic

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
17 Jan 2015CPOL1 min read 19.8K   472   4   4
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUnable to download source Pin
Member 1298181431-May-18 6:41
Member 1298181431-May-18 6:41 
QuestionUnable to download source Pin
BertrandLQ5-May-15 2:49
BertrandLQ5-May-15 2:49 
AnswerRe: Unable to download source Pin
rolandomontero5-May-15 4:55
rolandomontero5-May-15 4:55 
GeneralRe: Unable to download source Pin
BertrandLQ6-May-15 20:53
BertrandLQ6-May-15 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.