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 http://dev.mysql.com/downloads/windows/visualstudio.
- 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 https://dev.mysql.com/downloads/connector/net.
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 hosting.com servers. The following procedure demonstrates how to use the Server Explorer to view MySQL databases on your hosting.com account.- Start Visual Studio.
- On the View menu, click Server Explorer.
- Click the Connect to Database icon. The Choose Data Source dialog box appears.
-
In the Data source list box, select MySQL Database, and then click Continue.
📘 Note 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.
-
In the Server name text box, type the name of the hosting.com server for your account.
📘 Note For information about how to determine your account’s server name, please see this article.
- In the User name text box, type the name of the database user.
-
In the Password text box, type the password for the database user you specified in step 6.
👍 Tip 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.
- In the Database name text box, type the name of the database you want to access.
- 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.
- Click OK. Server Explorer adds the server and database to the list of available connections.
-
Double-click the server and database name to view the following items:
- Tables
- Views
- Stored Procedures
- Stored Functions
- UDFs (User-defined functions)
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.- Start Visual Studio.
- On the File menu, click New, and then click Project. The New Project dialog box appears.
-
Under Templates, select your preferred coding language:
- To use C#, select Visual C#.
- To use VB.NET, select Visual Basic.
- In the list of templates, click Console Application.
-
In the Name text box, type
MySQL_test. - Click OK. Visual Studio creates the project.
- In the code window, delete all of the existing code.
- 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#:
Visual Basic
- On the Project menu, click Add Reference. The Reference Manager dialog box appears.
- Under Assemblies, click Extensions.
-
Scroll down the list of assemblies, and then double-click MySql.Data. A check box appears next to the assembly name.
📘 Note 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.
- Click OK.
- On the Build menu, click Build Solution. Visual Studio compiles the application.
- On the Debug menu, click Start Without Debugging. The application runs and prints all of the data from the selected table.