0

I have been trying to connect to a local MySQL database in Visual Studio 2015. This is the code I am using.

Imports System.Data.Sql

Imports System.Data.SqlClient

Public Class SQLControl

Public SQLCon As New SqlConnection With {.ConnectionString = "Server=localhost,3306;user=root;database=database;pwd=xxxx;"}
Public SQLCmd As SqlCommand

Public Function HasConnection() As Boolean
    Try
        SQLCon.Open()

        SQLCon.Close()
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    Finally
        SQLCon.Dispose()
    End Try
End Function

I receive the error: "Internal Connection Fatal Error. Error State: 18"

OR I receive the error: "A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider:TCP Provider, error:0 - An established connection was aborted by the software in your host machine.)

Which error I receive appears to be random. I have changed many settings to no avail, including the port of the server to 1433, which gives me the same errors.

I was wondering if there was any way to solve this through VS 2015 or perhaps the server settings themselves.

Side note: I am able to connect to the database without a problem through VS's server explorer and other programs on this machine (Anypoint Studio), but this does not fulfill the purpose of the project.

Drew
  • 24,851
  • 10
  • 43
  • 78
Z. M.
  • 11
  • 4
  • 1
    the c# equiv is `using MySql.Data.MySqlClient; using MySql.Data;` so you are not using the right classes – Drew Oct 14 '16 at 14:53
  • Is this the actual answer? I must write this project in VB.net. Wouldn't using the incorrect classes throw a different exception instead of the ones I am receiving? – Z. M. Oct 14 '16 at 14:56
  • All I know is that people very often point it toward sql-server classes and not mysql. You definitely (imo) need the stuff seen in the last picture [Here](http://stackoverflow.com/a/38706288) . And then even after some people do the `using` (or `Imports`) then often turn around and just use the sqlserver classes anyway all intermingled and error-prone. See this too http://stackoverflow.com/a/38277659 .... you could translate that one very easily in your head to vb.net – Drew Oct 14 '16 at 15:06

1 Answers1

1

Drew was right. I was using the SQL class instead of the MySQL class.

Importing the MySQL.Data.MySQLClient class and using MySQLConnection instead of SqlConnection solved the issue:

Imports MySql.Data.MySqlClient


Public Class MySQLControl

Public MySQLCon As New MySqlConnection With {.ConnectionString = "Server=localhost;user=root;database=database;pwd=xxxx;"}
Public MySQLCmd As MySqlCommand

Public Function HasConnection() As Boolean
    Try
        MySQLCon.Open()

        MySQLCon.Close()
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
        MySQLCon.Close()
    Finally
        MySQLCon.Dispose()
    End Try
End Function
Z. M.
  • 11
  • 4
  • Please improve this answer with the code so people know. Once you sort the whole thing out – Drew Oct 14 '16 at 15:46