Connecting to a MySQL Database Using ASP.NET

Generic selectors
Exact matches only
Search in title
Search in content
Filter by Categories
Account Management Help
add photos, video and audio
Add-on features and social media
Advanced campaign settings
All about analytics
All about images
Basic steps
Configure the Web Application Firewall (WAF)
Connect my Calendar and Online Storage to Workspace
contact support
copy files to my site
Create my email address
Customize campaign
Discover domains
discover linux hosting with cPanel
Discover Microsoft 365
Discover Online Storage
Discover Workspace Email
edit content
Email Marketing demo
expand your site with sections and pages
Explore email add-ons
Explore my account
Find server and port settings (IMAP/POP)
fine tune my website
get social
Get started with SSL certificates
Get the most out of Microsoft 365
help my site get found and monitor my metrics
Hosting & Servers
keep my account secure
Keep my email secure
know your privacy rights
Manage your SSL certificate
Managed WordPress
Manual email configuration
Migrate and export my emails
My email account isn’t working (troubleshooting)
nameservers and DNS
online store
parking, forwarding and monetizing
privacy and protection
publish my site
Quick Shopping Cart
Renew my products and services
Set up a campaign
Set up email on my devices
set up my payment methods
Set up my Workspace Email account
start with the basics
Stay productive anywhere with Office apps
Streamline email tasks to help my business
transfer between accounts
transfer between registrars
Troubleshoot email and email setup
Upgrade and renewal options for email
Web & Classic Hosting
Website backups
Website Builder 7
Website Builder version 6
Website Security and Backups
work with databases
work with delegates
working with blogs

This example describes using ASP.NET/MySql.Data to connect to a MySQL Database. A few important things you need before you get started:

  • Knowledge of computer programming.
  • Microsoft® Visual Studio .NET.
  • MySql Connector/NET on your development computer. For more information, click here.
  • Knowledge of MySql and specifically the MySql.Data Namespace.
  • A setup MySql Database.

To Connect to a MySQL Database Using ASP.NET

  • Find your database’s connection strings (Plesk). 

Note: Change the your password value to your real database password value.

  1. Using Microsoft Visual Studio .NET create an ASP.NET Project.
  2. Add a reference to MySql.Data.dll.
  3. Replace the value in the following code with your_ConnectionString with your database information.
  4. Insert the following code into your project including your modified your_ConnectionString value: 

Note: If your MySql database was created with the Allow Direct Database Access enabled, you can connect to the database from your development computer. If you did not enable Allow Direct Database Access, your MySql is in a secure environment and you cannot connect to the database from your development computer. A connection can only be successful when your code is deployed to the hosting site.

MySql.Data.MySqlClient.MySqlConnection mySqlConnection = new 
mySqlConnection.ConnectionString = “your_ConnectionString”; try
mySqlConnection.Open(); switch (mySqlConnection.State)
case System.Data.ConnectionState.Open:
// Connection has been made
case System.Data.ConnectionState.Closed:
// Connection could not be made, throw an error
throw new Exception("The database connection state is Closed");
// Connection is actively doing something else
} // Place Your Code Here to Process Data //
catch (MySql.Data.MySqlClient.MySqlException mySqlException)
// Use the mySqlException object to handle specific MySql errors
catch (Exception exception)
// Use the exception object to handle all other non-MySql specific errors
// Make sure to only close connections that are not in a closed state
if (mySqlConnection.State != System.Data.ConnectionState.Closed)
// Close the connection as a good Garbage Collecting practice

Changing the currency for the products.

Prices shown in US Dollars. You can convert to your own currency after selecting a product.