0

Hi im relatively new to VB.NET so please bear with me here.

Im trying to setup a DB connection to my localhost everything looks good in my code except I get the error, when running the program, this PC is not allowed to connect to this DB, as you can see in the following image:

enter image description here

Here is my code I use to connect to the DB

Imports MySql.Data.MySqlClient
Public Class sreg
    Dim ServerString As String = "Server=localhost;User Id=root;Password="";Database=vbdb"
    Dim SqlConnection As MySqlConnection = New MySqlConnection

Private Sub sreg_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim SqlConnection As MySqlConnection = New MySqlConnection

        SqlConnection.ConnectionString = ServerString

        Try
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
                MsgBox("Successfully connected to MySQL DB")
            Else
                SQLConnection.Close()
                MsgBox("Connection is Closed")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try
    End Sub

    Public Sub SaveNames(ByRef SQLStatment As String)
        Dim cmd As MySqlCommand = New MySqlCommand

        With cmd
            .CommandText = SQLStatment
            .CommandType = CommandType.Text
            .Connection = SqlConnection
            .ExecuteNonQuery()
        End With

        SqlConnection.Close()
        MsgBox("Succesfully Added!")
        SqLConnection.dispose()
    End Sub
End Class

I suspect the problem might be in this line SqlConnection.ConnectionString = ServerString since above line returns error when I run the program, however when I remove it I get it to work, but get the not allowed to connect to DB error, as you can see in the image

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
Marilee
  • 1,598
  • 4
  • 22
  • 52
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Mar 04 '14 at 05:26
  • sorry for trying to be friendly then – Marilee Mar 04 '14 at 05:27
  • Please read the link. This is not a discussion forum, or a place to hang out. It's different from any site you've used before, and that's why it's been so successful. No nonsense - just Questions and Answers. – John Saunders Mar 04 '14 at 05:28
  • Are you using the correct password? Try using "password=;" instead of the double quotes after the equals sign. – charlesw Mar 04 '14 at 05:31
  • @charlesw Thanks ill give it a try now and let you know – Marilee Mar 04 '14 at 05:33
  • @charlesw Nope, I even added a pword still not allowed to connect – Marilee Mar 04 '14 at 05:38
  • Check out this question and let me know if it helps: http://stackoverflow.com/questions/1559955/host-xxx-xx-xxx-xxx-is-not-allowed-to-connect-to-this-mysql-server. I am used to MSSQL and it looks like you are using MySQL, so the connection string might be slightly different. – charlesw Mar 04 '14 at 05:47

1 Answers1

0

It looks like you are trying to connect to a MySQL server and it looks like the connection string you need is slightly different than for a MSSQL server:

"Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"

Source: https://www.connectionstrings.com/mysql/

charlesw
  • 572
  • 6
  • 25