PC拒绝访问连接到数据库

时间:2014-03-04 05:24:02

标签: mysql vb.net

嗨,我对VB.NET相对较新,所以请在这里忍受。

我试图设置到我的localhost的数据库连接我的代码中的一切看起来都很好,除了我得到错误,在运行程序时,不允许这台PC连接到这个数据库,如下图所示:

enter image description here

以下是我用来连接数据库的代码

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

我怀疑问题可能出在这行SqlConnection.ConnectionString = ServerString,因为当我运行程序时,上面的行返回错误,但是当我删除它时我得到了它的工作,但得到了不允许连接到DB错误,正如你在图片中看到的那样

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试连接到MySQL服务器,看起来您需要的连接字符串与MSSQL服务器略有不同:

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

来源:https://www.connectionstrings.com/mysql/