Problems connecting Mysql with VB.NET

0

I'm trying to connect a Mysql database with .NET and download the connector for this page Link where you download the connector

and this is the code that I use in .NET

Imports MySql.Data.MySqlClient
Public Class conexion
Private MysqlCommand As New MySqlCommand
Dim MysqlConnString As String = "server=localhost; user id=root; password=abcd1234"
Public MysqlConexion As MySqlConnection = New MySqlConnection(MysqlConnString)
Public Sub Probarconexion()
    Try
        MysqlConexion.Open()
        MsgBox("La conexion fue exitosa")
        MysqlConexion.Close()
    Catch ex As Exception
        MsgBox("La conexion no fue exitosa")
    End Try
End Sub
End Class

And looking at the console I get the following error: Control vehciulos.exe Error: 0: The host localhost does not support SSL connections

I do not know what it could be.

I would appreciate your help

    
asked by Familia Valencia Hdz 04.10.2018 в 22:28
source

1 answer

1

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.

    
answered by 05.10.2018 / 02:17
source