Connecting to MySQL Database with ASP.NET Web Pages (Razor)

John Avis by | May 20, 2011 | ASP.NET Web Pages

To connect to a MySQL database from ASP.NET Web Pages (Razor) you need to do the following:

1. Add a "Bin" ASP.NET folder to your project and copy the file MySQl.Data.dll in to it.

You can obtain this file by downloading the latest version of the MySQL Connector/NET (, installing it, then locate the file in the installation folder.

2. In your web.config file add a connection string for your database, for example:

<add name="Any-name" connectionString="Server=server-name-or-ip;Port=3306;Database=database-name;Uid=username;Pwd=password;" providerName="MySql.Data.MySqlClient" />

3. In your code you can then open a connection to your database:

var db = Database.Open("Any-name");

Related Posts

ASP.NET Web Forms ASP.NET Web Pages

Using WebMatrix.Data in ASP.NET Web Forms

by John Avis | July 17, 2013

ASP.NET Web Pages has a simple to use Namespace that help you open, query and send commands to a database, and to work with rows that are returned by SQL queries.

ASP.NET Web Pages

Installing ASP.NET Web Pages Helpers in Visual Web Developer 2010

by John Avis | May 21, 2011

ASP.NET Web Pages ASP.NET Web Forms

Finally... A Replacement for Classic ASP

by John Avis | May 18, 2011


Tony Dunsworth

by Tony Dunsworth | August 2, 2013

Would this work with the same set up for an Oracle database using ODP.NET dll's?


John Avis

by John Avis | August 2, 2013

You can connect to an Oracle database (or any ADO.NET provider) in the same way with WebPages. Just add your connection string.


Leave a Comment
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.


Get the latest posts delivered to your inbox.