How to connect to MySQL using Microsoft .NET

This article describes how to add MySQL support to Microsoft Visual Studio. With MySQL and Visual Studio integration, you can develop Microsoft .NET applications that access MySQL databases onUltaHost Hosting servers.

Adding MySQL support to Visual Studio

Visual Studio does not include support for MySQL by default. To add MySQL support to Visual Studio, you must install the following components:

  • MySQL for Visual Studio: This component adds MySQL support to Visual Studio's visual database tools, such as Server Explorer. To download MySQL for Visual Studio, please visit
  • Connector/Net: This component adds .NET drivers for MySQL to Visual Studio. If you want to write .NET code that accesses MySQL databases, you must install this component. To download Connector/Net, please visit

You should download and install both of these components to obtain the best possible MySQL integration with Visual Studio.

To access MySQL databases from a remote computer, you must add your IP address to the list of IP addresses allowed for remote access. For information about how to do this, please see this article. If you do not add your IP address, you receive Access denied messages when you try to access a MySQL database remotely.

The following procedures were developed and tested using Visual Studio 2015 (Community Edition). The exact steps or user interface labels for other Visual Studio versions may differ slightly.

Using Server Explorer

After you install the MySQL for Visual Studio component, you can use Visual Studio's visual database tools to access and view MySQL databases on UltaHost  servers.

The following procedure demonstrates how to use the Server Explorer to view MySQL databases on your UltaHost account.

A MySQL database and user must already exist on your account before you go through the following procedure.

  1. Start Visual Studio.
  2. On the View menu, click Server Explorer.
  3. Click the Connect to Database icon. The Choose Data Source dialog box appears.
  4. In the Data source list box, select MySQL Database, and then click Continue.

If you do not see the MySQL Database option, the MySQL for Visual Studio component is probably not installed or registered correctly with Visual Studio. Try re-installing the MySQL for Visual Studio component.

    5.In the Server name text box, type the name of the UltaHost server for your account.

    6.In the User name text box, type the name of the database user.

    7.In the Password text box, type the password for the database user you specified in step 6.

If you do not want to re-type the password every time Visual Studio connects to the database, select the Save my password check box.

    8.In the Database name text box, type the name of the database you want to access

    9.Click Test Connection. You should receive a Test connection succeeded message. If you do not, check the values you provided in steps 5 to 8, and then try again.

    10Click OK. Server Explorer adds the server and database to the list of available connections.

    11.Double-click the server and database name to view the following items:

  • Tables
  • Views
  • Stored Procedures
  • Stored Functions
  • UDFs (User-defined functions)

You can double-click any of these items to navigate through the database. For example, to see the tables defined in the database, double-click Tables. To view the actual data stored in a table, right-click the table name, and then click Retrieve Data.

Connecting to MySQL using .NET

After you install the Connector/Net component, you can write .NET code that accesses MySQL databases. To do this, you must add a reference to the MySQL .NET library in your project, and specify the correct parameters in a database connection string.

The following procedure demonstrates how to create a simple C# or Visual Basic console application that connects to a remote MySQL database and runs an SQL query.

A MySQL database and user must already exist on your account before you go through the following procedure.

  1. Start Visual Studio.
  2. On the File menu, click New, and then click Project. The New Project dialog box appears.
  3. Under Templates, select your preferred coding language:
  4. To use C#, select Visual C#.
  5. To use VB.NET, select Visual Basic.
  6. In the list of templates, click Console Application.
  7. In the Name text box, type MySQL_test.
  8. Click OK. Visual Studio creates the project.
  9. In the code window, delete all of the existing code.
  10. Copy the following code for the language you selected in step 3, and then paste it into the code window. Modify the connstring definition to use the login information for your own database. Additionally, replace the three instances of table_name with the name of the table you want to query.

Visual C#:

using System;

using System.Data;

using MySql.Data.MySqlClient;

namespace MySQL_test


    class Program


        static void Main(string[] args)


            string connstring = @";userid=example_user;password=example_password;database=example_database";

            MySqlConnection conn = null;



                conn = new MySqlConnection(connstring);


                string query = "SELECT * FROM table_name;";

                MySqlDataAdapter da = new MySqlDataAdapter(query, conn);

                DataSet ds = new DataSet();

                da.Fill(ds, "table_name");

                DataTable dt = ds.Tables["table_name"];

                foreach (DataRow row in dt.Rows)


                    foreach (DataColumn col in dt.Columns)


                        Console.Write(row[col] + "\t");





            catch (Exception e)


                Console.WriteLine("Error: {0}", e.ToString());




                if (conn != null)








Visual Basic:

Imports System

Imports System.Data

Imports MySql.Data.MySqlClient

Module Module1

    Sub Main()

        Dim connstring As String = ";userid=example_user;password=example_password;database=example_database"

        Dim conn As MySqlConnection = Nothing


            conn = New MySqlConnection(connstring)


            Dim query As String = "SELECT * FROM table_name;"

            Dim da As New MySqlDataAdapter(query, conn)

            Dim ds As New DataSet()

            da.Fill(ds, "table_name")

            Dim dt As DataTable = ds.Tables("table_name")

            For Each row As DataRow In dt.Rows

                For Each col As DataColumn In dt.Columns

                    Console.Write(row(col).ToString() + vbTab)




        Catch e As Exception

            Console.WriteLine("Error: {0}", e.ToString())


            If conn IsNot Nothing Then


            End If

        End Try

    End Sub

End Module

       11.On the Project menu, click Add Reference. The Reference Manager dialog box appears.

       12.Under Assemblies, click Extensions.

       13.Scroll down the list of assemblies, and then double-click MySql.Data. A check box appears next to the assembly name.

If you do not see the MySql.Data assembly listed, the Connector/Net component is probably not installed or registered correctly with Visual Studio. Try re-installing the Connector/Net component.

       14.Click OK.

       15.On the Build menu, click Build Solution. Visual Studio compiles the application.

       16.On the Debug menu, click Start Without Debugging. The application runs and prints all of the data from the selected table.

More Information

For more information about Microsoft Visual Studio, please visit

Was this article helpful?

Thanks for your opinion!
Have more questions? Submit a request
LastModified: November 9, 2021 6:36 am