Click here to Skip to main content
12,997,531 members (60,572 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 22 Mar 2008

C# and MySQL/Connector 5.2

, 22 Mar 2008
Rate this:
Please Sign up or sign in to vote.
An article on how to connect to a MySQL database using the Connector/NET 5.2


I'm a junior C# developer and I had a heck of a time trying to find a "how to" article that explains how to connect to a MySQL database using the Connector/NET 5.2.

So here's a simple article explaining how to do it.


To understand the code, you need an understanding of database technology.
I've got MySQL 5 up and running and I installed the Connector/NET 5.2

I created a database name="cart" with a table="members" with fields= "fname" & "lname"

In Visual Studio 2005, I created a consoleApp.
I then added the Mysql.Data provider to the Reference folder.

Understanding the Provider Object

| |--MySqlCommand
| |--MySqlConnection
| |--MySqlDataAdapter
| |--MySqlDataReader
| |--MySqlException
| |--MySqlParameter
| |--MySqlDbType
| |--MySqlError
| |--MySqlHelper
| |--MySqlScript

You can see the total content of the Provider by using the Object Browser in Visual Studio 2005.

Using the Code

The code was tested using C# and Visual Studio 2005.

using System;
using System.Collections.Generic;
using System.Text;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace ConsoleApp
    class Program
              static void Main(string[] args)
                    //Set up connection string
                    string connString = @"
                        server = localhost;
                        database = cart;
                        user id = root;
                password =;
                    //Set up query string
                    string sql = @" select * from members ";

            MySqlConnection conn = null;
            MySqlDataReader reader = null;

                //open connection
                conn = new MySqlConnection(connString);

                //Execute the Query
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                reader = cmd.ExecuteReader();

                //Display output header
                Console.WriteLine("This program demonstrates the use of"
                    + "the MYSQL Server Data Provider");

                Console.WriteLine("Querying the database {0} with {1}\n"
                    , conn.Database
                    , cmd.CommandText

                Console.WriteLine("{0} | {1}"

                //Process the result set
                while (reader.Read())
                    Console.WriteLine("{0} | {1}"
                        , reader["fname"].ToString().PadLeft(10)
                        , reader["lname"].ToString().PadLeft(10)
            catch (Exception e)
                Console.WriteLine("Error " + e);

To test, just use the Ctrl + F5 combination.

Points of Interest

Being a junior and a novice in writing an article, I had to use a reference.
A good book on the subject C# and SSE is "Beginning C# 2005 Databases".


  • 22nd March, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Belgium Belgium
Developer within C#, Dynamics NAV (Navision), Php environments.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
cohay_indonesia12-Mar-10 20:37
membercohay_indonesia12-Mar-10 20:37 
GeneralRe: My vote of 1 Pin
AspDotNetDev3-May-11 9:15
mvpAspDotNetDev3-May-11 9:15 
Generalsimplified mysql approach [modified] Pin
anoftc16-Oct-09 5:54
memberanoftc16-Oct-09 5:54 
GeneralRe: simplified mysql approach Pin
Karavaev Denis5-Sep-11 2:38
memberKaravaev Denis5-Sep-11 2:38 
Useful, thanks, but include in your class next time this:

string FProto;
string FPipe;
MySqlConnection conn;
public wMySql(string server, string user, string pass, string db, string proto, string pipe)
    FServer = server; FUser = user; FPass = pass; FDB = db; FProto = proto; FPipe = pipe;
    FConnStr = "server = " + FServer + ";\n" +
               "database = " + FDB + ";\n" +
               "user id = " + FUser + ";\n" +
               "password = " + FPass + ";\n" +
               "Protocol = " + FProto + ";\n" +
               "pipe = " + FPipe + ";\n";
    conn = new MySqlConnection(FConnStr);

some people use named pipes to connect to mysql, not tcp

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 22 Mar 2008
Article Copyright 2008 by kribo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid