首先到网上下载MySql.Data.MySqlClient.dll,然后添加引用到项目
using MySql.Data.MySqlClient; string connectString = @"server=localhost;userid=root;password=xxx;database=sharejs"; /*由于我的数据库在本地就用localhost了,可以替换成IP地址*/ MySqlConnection conn = new MySqlConnection(connectString); conn.Open (); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from info"; cmd.CommandType = CommandType.Text; using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) Console.WriteLine(reader[0].ToString()); }