Matt
Matt

Reputation: 1570

VB.NET MySQL Connection (DB not local)

I'm trying to connect to a MySQL server via VB.NET, but my program keeps freezing on the con.Open() line.

Imports System.Data.SqlClient
Imports System.Data

....

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand

    Try
        con.ConnectionString = "Server=000.000.000.000;Database=db_name;Uid=db_user;Pwd=db_pw;"
        con.Open()

        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM courses"
        Dim lrd As SqlDataReader = cmd.ExecuteReader()

        While lrd.Read()
            MessageBox.Show(lrd.ToString)
        End While
    Catch ex As Exception
        MessageBox.Show("Error while connecting to SQL Server: " & ex.Message)
    Finally
        con.Close()
    End Try

The stuff in the connection string is the form of what I have in those spots, using those words as actual value placeholders for this example. Apart from containing the actual values, they are exactly identical. So if there are form errors (i.e. a missing apostrophe), please let me know. For the Server, am I supposed to put the IP address of the server or something else? Furthermore, in the reading loop, I'm not sure how to display all of the contents of the SQL query. Is what I have correct?

Upvotes: 3

Views: 1443

Answers (1)

Michael
Michael

Reputation: 1879

You are using ADO.NET for SQL Server instead of what you should be using, an ADO.NET client that works with MySQL, like MySQL Connector

You will need to install this and change SqlClient, SqlCommand, SqlConnection in your code to MySqlClient, MySqlCommand, MySqlConnection in accordance with how the MySQL Connector works.

To display first column values:

  While lrd.Read()
            MessageBox.Show(lrd.GetValue(0))
  End While

Upvotes: 2

Related Questions