ASP Hosting sale!
1,000MB space, 40GB transfer, 1 SQL Server 2000 db! - $9.95 per month*!
Limited Time Offer!

aspdev | articles | tutorials | forums

ASPdev -> All Articles -> Connect to MySQL from ASP.NET

Connect to MySQL from ASP.NET

vMySQL database is a low-cost alternative to MS SQL Server database and can be
used easily in web applications, including ASP.NET projects. In this article
Iíll explain the necessary steps to connect to MySQL from ASP.NET. We will
connect to our MySQL database using ODBC .NET data provider and DSN-less

. Make sure you have installed the .Net Framework on your server

. Download the ODBC .Net data provider and install it on your server. You can
find the download here:

. Make sure that file is located in the following folder
on your server C:\Program Files\Microsoft.NET\Odbc.Net\.

. Install Microsoft Data Access Components (MDAC) 2.6 or later. MDAC 2.8 is
recommended. You can download it here:

. Install MySQL Server.

. install MySQL ODBC Driver-MyODBC 3.51. You can download it here:

Here is the actual code connection to the MySQL database and retrieving and
displaying all the records from Table1 table in your database.

<%@ Page CompilerOptions='/R:"C:\Program Files\Microsoft.NET\Odbc.Net\"'%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Microsoft.Data.Odbc" %>
<SCRIPT Language="VB" Runat="server">
Sub Page_Load(Source as object, e as EventArgs)
Dim sConString As String = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;
DATABASE=Your_Mysql_DB; UID=mysql_username; PASSWORD=mysql_password; OPTION=3"
Dim oConnection as ODBCConnection = new ODBCConnection(sConString)
Dim sSQL as String = "SELECT * FROM Table1"
Dim oDataAdapter as ODBCDataAdapter = New ODBCDataAdapter(sSQL, oConnection)
Dim oDataSet as DataSet = new DataSet()
oDataGrid.DataSource = oDataSet
End Sub
<ASP:DataGrid ID="oDataGrid" Runat="server" />

After you have tested your solution on your development workstation, you need
to find a web hosting company offering ASP.NET hosting and supporting MySQL
database, in order to deploy your web application and make it available on

Contact Us