如何通过c#连接到MySQL数据库?

如何通过c#连接到MySQL数据库?,c#,mysql,C#,Mysql,我读到我必须下载这个,但它说我不能安装它,因为我需要.NET Framework。我已经有4.0了 安装程序正在检查.Net 3.5或2.0 mabey=使用此链接,如果您有VS.Net 2010,它将工作您应该使用MySQL Connector/Net 6.3.5,在提到的位置可用(http://dev.mysql.com/downloads/connector/net/)我从一个现有的项目中复制/粘贴了这些内容,然后对其进行了清理…因此它没有被编译和测试,但你明白了。下面是一些示例代码,让您

我读到我必须下载这个,但它说我不能安装它,因为我需要.NET Framework。我已经有4.0了

安装程序正在检查.Net 3.5或2.0 mabey=

使用此链接,如果您有VS.Net 2010,它将工作您应该使用MySQL Connector/Net 6.3.5,在提到的位置可用(http://dev.mysql.com/downloads/connector/net/)

我从一个现有的项目中复制/粘贴了这些内容,然后对其进行了清理…因此它没有被编译和测试,但你明白了。下面是一些示例代码,让您开始:

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();
    }