SQL连接错误40 000webhost与Visual Basic

时间:2016-08-03 23:59:32

标签: mysql vb.net

我很确定我的所有信息都在这里,但我一直收到此错误

http://puu.sh/qoZDQ/7294d6e682.png

我使用的代码:(不是我的)

我有正确的用户名密码和数据库名称(我认为)

'SET THE CONNECTION BETWEEN VISUAL BASIC AND MYSQL DATABASE
Dim con As SqlConnection = New SqlConnection("Data Source=mysql9.000webhost.com;" & "Initial Catalog=databasename;" & "User ID=username;" & "Password=password;")

'A SET OF COMMAND IN MYSQL
Dim cmd As New SqlCommand
'SET A CLASS THAT SERVES AS THE BRIDGE BETWEEN A DATASET AND DATABASE FOR SAVING AND RETRIEVING DATA.
Dim da As New SqlDataAdapter
'SET A CLASS THAT CONSISTS SPECIFIC TABLE IN THE DATABASE
Dim dt As New DataTable
Dim sqlQuery As String
Dim result As Integer

Private Sub register(ByVal sqlQuery As String)
    Try
        'OPENING THE CONNECTION
        con.Open()
        'HOLDS THE DATA TO BE EXECUTED
        With cmd
            .Connection = con
            .CommandText = sqlQuery
        End With
        'EXECUTE THE DATA
        Result = cmd.ExecuteNonQuery
        'CHECKING IF THE DATA HAS BEEN EXECUTED OR NOT
        If result > 0 Then
            MsgBox("User has been registered.")
        Else
            MsgBox("Failed to register the user.")
        End If
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub login(ByVal sqlQuery As String)
    Try
        con.Open()
        With cmd
            .Connection = con
            .CommandText = sqlQuery
        End With
        'FILLING THE DATA IN A SPECIFIC TABLE OF THE DATABASE
        da.SelectCommand = cmd
        dt = New DataTable
        da.Fill(dt)
        'DECLARING AN INTEGER TO SET THE MAXROWS OF THE TABLE
        Dim maxrow As Integer = dt.Rows.Count
        'CHECKING IF THE DATA IS EXIST IN THE ROW OF THE TABLE
        If maxrow > 0 Then
            MsgBox("Welcome " & dt.Rows(0).Item(4))
        Else
            MsgBox("Account does not exist.")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    con.Close()
End Sub

Private Sub btn_Register_Click(sender As Object, e As EventArgs) Handles btn_Register.Click
    sqlQuery = "INSERT INTO `dbuser` ( `user_name`, `user_username`, `user_pass`, `user_type`, `user_steamid`)" & "VALUES ('" & txtname.Text & "','" & txtusername.Text & "','" & txtpassword.Text & "','" & cbotype.Text & "','" & txtsteamid.Text & "')"
    register(sqlQuery)
End Sub

Private Sub btn_Login_Click(sender As Object, e As EventArgs) Handles btn_Login.Click
    sqlQuery = "SELECT * FROM `dbuser` WHERE user_username ='" & txtusername.Text & "' AND user_pass = '" & txtpassword.Text & "'"
    login(sqlQuery)
End Sub

我使用了来自http://puu.sh/qoZXo/a391cba854.jpg的数据库信息(也不是我的信息只是一个例子所以我不公开发布我的信息)

1 个答案:

答案 0 :(得分:0)

我在Plutonix评论的帮助下解决了我的问题

  

MySql与Microsoft SqlServer - Plutonix

不同

所以我做了一些谷歌搜索,发现了这个:https://dev.mysql.com/downloads/connector/net/

这是MySql的.Net框架(我认为这是正确的术语)

无论如何安装它然后从

更改我的代码的顶行
imports System.Data.SqlClient

要:

imports MySql.Data.MySqlClient

并将代码中的sql变量更改为MySql变量,只需将My添加到第一位,它似乎工作"更好"我现在有一个新问题,但它有000webhosts mysql数据库,而不是代码。