Solved

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

Posted on 2008-06-11
3
177 Views
Last Modified: 2010-04-23
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
Comment
Question by:Caracena
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
NIMTUG_Simon earned 250 total points
ID: 21764575
0
 
LVL 6

Author Comment

by:Caracena
ID: 21765210
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
 
LVL 6

Author Comment

by:Caracena
ID: 21765335
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question