I will highlight the best practices we can follow to connect to mysql using ado.net on vb.net:
We must avoid defining the connection chains within the code, since to change them we will have to: modify the code, compile and install the new version of the application. The recommended thing then, is to add the configuration file of the application app.config . There we have the section dedicated to this "connectionStrings".
<connectionStrings>
<add name="MySQLConnString" connectionString="Server=localhost;Database=tubasededatos;Uid=root;Pwd=abcd1234"/>
</connectionStrings>
Going back to ADO.NET, we must make sure to release all the resources of the objects after using them. Ex:
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString))
{
conn.Open();
// Este bloque using libera automáticamente los recursos utilizados por "command"
using (var command = new SqlCommand("SELECT * FROM Usuarios", conn))
// Este bloque using cierra y libera automáticamente los recursos utilizados por "reader"
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
// ...
}
}
}
So, your connection class would be like this:
Public NotInheritable Class Conexion
Private Sub New()
End Sub
Public Shared Function ObtenerConeccion() As MySqlConnection
Return New MySqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnString").ToString())
End Function
Public Shared Function ProbarConexion() As Boolean
Using conn = ObtenerConexion()
Try
conn.Open()
Return True
Catch ex As Exception
Return False
End Try
End using
End Function
End Class
Your method to try would fit more or less like this:
Public Sub Prueba()
Try
If (Conexion.ProbarConexion()) Then
MsgBox("La conexion fue exitosa")
End If
Catch ex As Exception
MsgBox("La conexion no fue exitosa")
End Try
End Sub
I hope you resolve, since I'm a bit rusty on vb.net.