Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

Connect to MySQL database

$
0
0

I have refered to one of the website and tested using the code. But I got the problem to link to mysql database. As requested, I have created a module, named MySQL_mod.vb, and below is the coding. I write the code in vb.net using the SharpDevelop.

Public Module MySQL_mod
    Public con As MySql.Data.MySqlClient.MySqlConnection
    Public command As MySql.Data.MySqlClient.MySqlCommand
    Public adapter As MySql.Data.MySqlClient.MySqlDataAdapter
    Public reader As MySql.Data.MySqlClient.MySqlDataReader
    Public Sub startconnect
        Try
            con=New MySQL.Data.MySqlClient.MySqlConnection
            con.ConnectionString="server=127.0.0.1:3306;user=root;password=123456;database=test"
            'con.Close
            con.Open

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

And I have created a button to get the tables for testing in the form as shown below.

Sub Button1Click(sender As Object, e As EventArgs)
            Try
            MySQL_mod.startconnect
            Dim SQL As String = "SHOW TABLES;"
            MySQL_mod.command=New MySql.Data.MySqlClient.MySqlCommand(SQL,MySQL_mod.con)
            MySQL_mod.reader = MySQL_mod.command.ExecuteReader
            'Clear the ListBox item
            ListBox1.Items.Clear
            Do While MySQL_mod.reader.Read
                ListBox1.Items.Add(MySQL_mod.reader.Item(0))
            Loop
            MySQL_mod.reader.Dispose
            'Close the connection after all
            MySQL_mod.con.Close
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
End Sub

And when I click on the button, I got the message "Unable to connect to any of the specified MySQL hosts" , "Connection must be valid and open". Can I know what is the problem. From the mysql, I found the server host is 127.0.0.1 and the port is 3306. And I try to figure out what is the problem, but I can't find it.

Please do help me on this. Your advice and help is much more appreciated.


Viewing all articles
Browse latest Browse all 1764

Trending Articles