Connect to MySQL via C# .NET (Visual studion 2008)

Posted on 2011-05-03
Last Modified: 2013-12-16
Which client library can we use to connect to a MySQL 5.0 database from MS .NET Visual Studio 2008 code?

I use
using System.Data.OracleClient;;
using System.Data.SqlClient;

for Oracle and SQL server databases respectively.

Question by:toooki
    LVL 53

    Assisted Solution

    You can either use ODBC or MySql Client for .NET.


    1. Download MySql Client dll.
    2. Add the dll to your project.

    3. Add connection string to web.config

        <add name="Connstring" connectionString="server=localhost; userid=uid;password=pwd;pooling=yes;Database=DB" providerName="MySql.Data.MySqlClient"/>
          <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>


    thatt's it you can follow same sqlclient connection.


    Author Comment

    Thank you.
    I am going to check the details you sent to me.
    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    You can try OleDbConnection class:

    This way will allow you to connect to any of the most common databases by just changing the connection string.

    The following website can help you to determine the appropriate connection string:

    Author Comment

    Thank you. I am going to check this.
    LVL 16

    Accepted Solution


    Alternatively you can test this too.

    You need to download and install the mysql connector/net. you can download it from

    then in your project, add reference to MySql.Data

    then add "using MySql.Data.MySqlClient;"

    in your code part add the following code,

    private void button1_Click(object sender, System.EventArgs e)
    	string MyConString = "SERVER=localhost;" +
    		"DATABASE=Northwind;" +
    		"UID=testuser;" +
    	MySqlConnection connection = new MySqlConnection(MyConString);
    	MySqlCommand command = connection.CreateCommand();
    	MySqlDataReader Reader;
    	command.CommandText = "select * from Customers";
    	Reader = command.ExecuteReader();
    	string rowData = "";
    	while (Reader.Read())
    		for (int i= 0;i<Reader.FieldCount;i++)
    			rowData += Reader.GetValue(i).ToString() + ", ";
    		rowData += "<br />"
    	literal1.text = rowData;

    Open in new window

    Happy Programming.

    Author Comment

    Thanks srosebabu. I am going to try this soon.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now