Before starting, this tutorial assumes you already have some familiarity with:

  • MySQL Connector/NET
  • basic MySQL administration
  • SQL
  • ASP.NET

The overall workflow is simple: install Connector/NET, add the required library to your ASP.NET project, define a connection string, and wrap the connection logic in a reusable class.

After creating your web project, open web.config and add your MySQL connection string. The database name, user, and password should match your local test environment.

Next, add an App_Code folder and create a class such as DatabaseProvider.vb. The original article used VB.NET for the sample:

Here is the core of the provider class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Imports System.Configuration
Imports MySql.Data.MySqlClient

Public Class DatabaseProvider
    Public objConnection As MySqlConnection

    Function connect_db() As Boolean
        Try
            objConnection = New MySqlConnection(ConfigurationManager.ConnectionStrings("MySqlConnection").ConnectionString)
            objConnection.Open()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Function close_db() As Boolean
        Try
            If objConnection.State = Data.ConnectionState.Open Then
                objConnection.Close()
            End If
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

Then, in the page code-behind, you can instantiate the provider and test the connection:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Imports MySql.Data.MySqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim db As New DatabaseProvider

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If db.connect_db() Then
            Response.Write("Now we're connected!")
        Else
            Response.Write("No we're not!")
        End If
    End Sub
End Class

That is enough for a basic connection test and gives you a simple place to start before expanding into real queries and data access logic.

The original post linked to VB and C# tutorial ZIP files, but those attachments were not present in the migrated WordPress uploads, so they are no longer available in this archive copy.