?
Solved

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

Posted on 2008-06-11
3
Medium Priority
?
183 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:Cesar Aracena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

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

Author Comment

by:Cesar Aracena
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:Cesar Aracena
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

765 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