2014-03-04 26 views
0

嗨即时通讯相对较新的VB.NET所以请忍受我在这里。PC拒绝访问连接到数据库

我试图建立一个数据库连接到我的本地主机一切看起来不错,在我的代码,除了我得到的错误,当运行该程序,这台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由于上面的行返回错误,当我运行该程序时,但是当我删除它时,我得到它的工作,但得到不允许连接到数据库错误,你可以在图像中看到

+0

不同于论坛的网站,我们不使用“谢谢” ,或者“任何帮助表示赞赏”,或签名[so]。请参阅“[应该'嗨','谢谢',标语和致敬从帖子中删除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be - 从移除帖子中删除) –

+0

对不起,请尝试友好,然后 – Marilee

+0

请阅读下面的链接,这不是一个讨论论坛或一个地方,它与以前使用过的任何站点不同,这就是为什么 –

回答

0

看起来你是尝试连接一个MySQL服务器时,它看起来像您需要的连接字符串比为MSSQL服务器略有不同:

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

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