How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

How to connect to a MySQL database and retrieve data in C#

Connecting to a MySQL database is fairly simple and straight forward. The first thing you will need to do is to add a reference to MySQL.Data in your project.

This can be done in a few ways:

  • Right click on references in your project -> click Manage NuGet packages -> Search and install MySQL.Data from Oracle
  • Open Package Manager Console from Tools -> NuGet Package Manager -> Paste in the console the following line: Install-Package MySql.Data
  • If you have installed the .NET Connector from MySQL you can navigate to where you have installed the connector and add a reference to the MySQL.Data.dll file

After you have added a reference to MySQL.Data you can tailor the snippet below to suit your requirements.

If you are interested in other type of databases you can check my Microsoft Access Database and SQLite database tutorials.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

2 Responses to How to connect to a MySQL database and retrieve data in C#

  1. Supernova says:

    How would I convert my result to a string?

    • Shriyal Padte says:

      if (DBnull.value == reader[“SomeColumnName”])
      return “”;

      return Convert.ToString(reader[“SomeColumnName”]);

Leave a Reply

Your email address will not be published. Required fields are marked *