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
- Download the file zip.
- Go to your project at My Project in the referencias tab, add DBaseMySQL.dll and MySQL.Data.dll.
- Then go to your form, and in the box tool, search the Data tab.
- 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
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.