Tom's Subject Directory & City Building Fan Site

Search a Section of this site or the Entire Site
site search by freefind

Welcome! Your IP address is; 54.162.250.227





Installed on this system:
  • Microsoft Visual Studio 2008
    Version 9.0.30729.1 SP
    Installed Edition: Standard
  • Microsoft .NET Framework 3.5
  • MySQL Connector/Net 6.2.4

Links you may need to visit:

Microsoft Express Site
Microsoft Update Site
Example databases for MySQL
MySql Connector/NET Tutorials

justhost UK banner


justhost banner










Visual Basic 2008 & Mysql Version 5


An Article and Tutorial:  Connecting to MySql

Part II: Connecting to the MySql Server


Adding Code to Connect to Server

Now that we have the MySql library added as a reference
Imports MySql.Data.MySqlClient
Public Class frmLogin

End Class

The first item to add is above the, Public Class frmLogin (this will be Form1 if you haven't named your form), add the line Imports MySql.Data.MySqlClient which is the first line of the code view. Next we will declare the variables used to build the MySql connection string which will hold our server name, database name, user name and password used to connect to MySql. Variables used to build the connection string are;
  1. Server - holds the MySql server IP or URL
    1. www.yoursite.com  will also work
  2. DBname is the database name
  3. User holds the MySql user name
  4. Password is the MySql login password
  5. statConnect is used for the connect status at the top of our form
  6. statFail is used as status if we can't connect

Next we'll write the Public Sub Connect() that will hold our connection code. The first thing this code block does is declare a variable, Connection, as a MySqlConnection and pass the connection string, connStr, to it. Next we open the connection to the MySql server with "Connection.Open()". This code is all you need to connect to MySql on a web host. However if a problem occurs, we probably want to know what it is and deal with it.

In the Try statement we'll open the connection and display the server status in our label;

  • Connection.Open - opens a connection to the MySql database
  • svrStatus.Text = statConnect - sets the status label to connected

Next, if connection couldn't be made, we'll catch any exception that has been thrown, change server status label and pop a message box to alert the user;

  • Catch ex As MySqlException     
  •     ' Connection not made, change status     
  •     svrStatus.Text = statFail
  •     MsgBox(ex.Message)

Finally we'll close the connection and change the status label.

  • Connection.Close()
  • svrStatus.Text = "Connection Closed"

If you are using Visual Studio, pressing F5 will display the form with a status of Connected as below provided the connect string is correct, and your IP is listed in the Remote MySql list;

Use "Complete Code" for copy & paste
Imports MySql.Data.MySqlClient
Public Class frmLogin
    ' Declare and Assign values to MySql login variables
    'Set these four values to your server and database values
    Dim Server As String = "222.222.222.22" 		'www.yoursite.com will also work
    Dim DBname As String = "healers1_moose"
    Dim User As String = "healers1_tomnobl"
    Dim Password As String = "@123!abc#"
    ' MySql Connection String
    Dim connStr As String = "Server=" & Server & ";Database=" & DBname & ";uid=" & User & ";pwd=" & Password & ";"
    Dim statConnect As String = "Server Status: Connected."
    Dim statFail As String = "Could Not Connect!"

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Connect()
    End Sub

    Public Sub Connect()
        Dim Connection As New MySqlConnection(connStr)
	Try
	    Connection.Open()
	    ' Connection made, change form ststus
	    svrStatus.Text = statConnect
	    Catch ex As MySqlException
	    ' Connection not made, change status label
	    svrStatus.Text = statFail
	    MsgBox("Error " & ex.Number & ": Access denied" & vbCrLf & "Please try again", MsgBoxStyle.Critical)
	Finally
            Connection.Close()
            svrStatus.Text = connClosed
        End Try
    End Sub
End Class

Part I    Part III    Next









Warning: mysql_connect(): Access denied for user 'tmnobles_tom'@'192.232.216.159' (using password: YES) in /home4/tmnobles/public_html/Subject_Directory/Visual_Basic/comments/config.php on line 15
Could not connect to MySQL