• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Please provide a step-by-step guide to connect my VB.NET app to MySQL

Hi everyone,

I'm working on a program for my company that needs to grab data remotely, so I decided to use a hosted MySQL DB for which I have full access. I've tried several different options to connect to it from my application and it worked with a couple of them but, I don't know if what I'm doing is the best approach. Can you guys please describe what is the best/safer way to do it, including which drivers to download?

Also, I'm very familiar with MySQL queries but usually made from PHP. How can I, for example, get the data from a specific column and populate a Combobox?

Thanks a lot in advanced!
0
Cesar Aracena
Asked:
Cesar Aracena
  • 2
1 Solution
 
NIMTUG_SimonCommented:
0
 
Cesar AracenaPHP EnthusiastAuthor Commented:
Thanks a lot for the quick answer.

I had seen this already, and now I read it all over again. It helped me understand a lot of things but what I'm trying to achieve is kinda the exact oposite. I need the users to login using his/her username and password stored in the MySQL DB and not the DB user.

Also, I'm getting very confused when trying to populate a combobox (at the login form) with all the usernames (not too many employees in the company). Actually, I want to display "Last Name, Fisrt Name" in the combobox, which are fields stored in different fields (columns) in the DB.

Here's what I've got so far but doesn't work.

Thanks a lot again!
'Top of the script
 
Imports MySql.Data.MySqlClient
Imports System.Data
 
Public Class frmLogin
    Dim conn As MySqlConnection
    Dim myCommand As New MySqlCommand
    Dim myAdapter As New MySqlDataAdapter
    Dim myData As New DataSet
    Dim SQL As String
 
 
'Where it all happens
 
    Private Sub tmrConn_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrConn.Tick
        SQL = "SELECT user_name, user_last_name FROM users"
        txtConnStatus.Text = "Stablishing connection to Remote Server"
 
        conn = New MySqlConnection()
        conn.ConnectionString = "server=**************;" _
        & "user id=**************;" _
        & "password=************;" _
        & "database=********************"
 
        Try
            conn.Open()
            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL
 
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)
                comUsername.DataSource = myData
 
                Dim i As Integer = 0
                Do While i < dsUsers.Tables.Count
                    comUsername.Items.Add(i)
                    i = i + 1
                Loop
 
 
            Catch myerror As MySqlException
                MsgBox("There was an error reading from the database: " & myerror.Message)
            End Try
 
            txtConnStatus.Text = "Connection Opened Successfully"
            comUsername.Enabled = True
            txtPassword.Enabled = True
            'conn.Close()
        Catch myerror As MySqlException
            txtConnStatus.Text = "Error Connecting to Database: " & myerror.Message
        Finally
            'conn.Dispose()
        End Try
        tmrConn.Stop()
    End Sub

Open in new window

0
 
Cesar AracenaPHP EnthusiastAuthor Commented:
Ok, Me and my buddy just found the solution. We changed the final part to the following:

Thanks ;)
        Try
            conn.Open()
            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL
 
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)
 
                Dim oReader As MySqlDataReader
                oReader = myCommand.ExecuteReader
 
                Do While oReader.Read
                    comUsername.Items.Add(oReader("user_last_name") _
                    & ", " _
                    & oReader("user_name"))
                Loop
 
                oReader.Close()
 
            Catch myerror As MySqlException
                MsgBox("There was an error reading from the database: " & myerror.Message)
            End Try
 
            txtConnStatus.Text = "Connection Opened Successfully"
            comUsername.Enabled = True
            txtPassword.Enabled = True
            'conn.Close()
        Catch myerror As MySqlException
            txtConnStatus.Text = "Error Connecting to Database: " & myerror.Message
        Finally
            'conn.Dispose()
        End Try

Open in new window

0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now