Srikanth Technologies

Using MySQL 5.0 with MS.NET

In this blog, I list out the steps to be taken to install MySql 5.0 and Connector/NET. Then I explain the steps required to connect to MySql Database.

The theme of this article is how to connect to MySql from .NET. So, I will not get into details of MySql and how to use it.

Installing MySql and Connector/Net

Follow the steps given below to download and install MySql 5.0.
  1. Go to www.mysql.com website.
  2. Select MySQL 5.0 Community Server
  3. Select download link and select "Windows Essentials(x86)"
  4. After file is downloaded, double click on .msi file and start installing MySQL 5.0
  5. Connect to MySQL using MySQL -> MySQL Server 5.0 -> MySQl Command Client
  6. When it prompt you to enter password, just enter password that you gave at the time of installation for "root" user
  7. Give "Use MySQL" command at "MySql>" command prompt
  8. You are now connected to MySQL database
  9. Create some tables and other objects. For syntax details, see MySQL Manual using MySQL->MySQL Server 5.0-> MySql Manaual.

Once you installed and created required objects in MySQL, use the following steps to download .NET provider for MySQL.

  1. Select Download link and then select Connectors link in the home page of MySql.com
  2. Select "Connector/Net" link
  3. Select "Windows Source and Binaries (ZIP)"
  4. Unzip .ZIP file to get .MSI file and install it by double clicking on it.

Connect to MySQL using .NET

Now, I will show you how to create a simple application in C# to connect to MySQL 5.0. Create a new project( of type Console Application using C#) and then follow the steps given below to make a reference to MySQL provider.
  1. Go to solution explorer and select Add References option from Popup (Context) menu
  2. From Add References dialog box, select "MySQL.Data" option and click on Ok.
  3. Write the following code to connect to MySQL and modify data in ADDRESSES table, which was created by me in MySQL Database. In your case, you must create this table on your own.

using System;
using MySql.Data.MySqlClient;  // required for MySQL

namespace MySQLDemo
{
    class ChangeEmail
    {
        static void Main(string[] args)
        {
            MySqlConnection con = new MySqlConnection("server=localhost;Database=mysql;Uid=root;Pwd=srikanth");
            try
            {
                con.Open();
                // parameters are created with ? followed by a name - ?email
                MySqlCommand cmd = new MySqlCommand("update addresses set email = ?email where name =
                    ?name", con);
                cmd.Parameters.Add("?email", MySqlDbType.VarChar, 50).Value = "srikanthpragada@yahoo.com";
                cmd.Parameters.Add("?name", MySqlDbType.VarChar, 30).Value = "P.Srikanth";
                if (cmd.ExecuteNonQuery() == 1)
                    Console.WriteLine("Updated Successfully");
                else
                    Console.WriteLine("Name Not Found");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error-->" + ex.Message);
            }
            finally
            {
                con.Close();
            }
        } // end of main
    }
}

That's all we have to do to connect to MySQL Server from MS.NET.