Ahmad Farid
Ahmad Farid

Reputation: 14774

How to connect to MySQL database through c#?

I read that I have to download this http://dev.mysql.com/downloads/mirror.php?id=13427#mirrors, but it says that I can't install it because I need .NET Framework. I already have 4.0?!

Upvotes: 2

Views: 322

Answers (4)

kmarks2
kmarks2

Reputation: 4885

I pieced this together by copy/paste from an existing project then sanitizing it...so it's not been compiled and tested, but you get the idea. So here's some sample code to get you started:

using MySql.Data.Types;
using MySql.Data.MySqlClient;    

private void Function()
    {
                    //Set up connection, SqlHost/etc are classwide and declared elsewhere:
                    MySql connection = new MySqlConnection("SERVER=" + SqlHost + ";DATABASE=" + DatabaseName + ";UID=" + user + ";PASSWORD=" + password + ";pooling=false");

                    //Setup query:
                    MySqlCommand command = connection.CreateCommand();
                    MySqlDataReader Reader;    
                    command.CommandText = "your query here";

                    //Connect to relation system and execute query:
                    connection.Open();
                    Reader = command.ExecuteReader();
                    while(Reader.Read())
                    {
                        MessageBox.Show("here's a row from the query response: " + Reader[0].ToString());
                    }

                    //Clean up:
                    connection.Close();
                    Reader.Close();
    }

Upvotes: 0

MiSHuTka
MiSHuTka

Reputation: 1310

You should use MySQL Connector/Net 6.3.5 available at mentioned location (http://dev.mysql.com/downloads/connector/net/)

Upvotes: 0

Stefan P.
Stefan P.

Reputation: 9519

Use this link, it will work if you have VS.NET 2010 http://dev.mysql.com/downloads/connector/net/

Upvotes: 1

Fredrik Leijon
Fredrik Leijon

Reputation: 2792

Installer checking for .Net 3.5 or 2.0 mabey =

Upvotes: 0

Related Questions