Solved

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

Posted on 2008-06-11
3
182 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 250 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…
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…

729 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