首先下载一个mysql.data.dll拷贝到bin下面并引用一下
using MySql.Data.MySqlClient;
class Program { static void Main(string[] args) { string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString; using (MySqlConnection conn = new MySqlConnection(connStr)) { string sql = "select * from sys_user "; conn.Open(); using (MySqlCommand cmd = new MySqlCommand(sql, conn)) { using (MySqlDataReader dataReader = cmd.ExecuteReader()) { while (dataReader.Read()) { string obj = (string)dataReader[1]; Console.WriteLine(obj); } } } } } }
mysql的mysql库中user表的Host字段设置为%,配置文件连接字符串为:
<connectionStrings>
<add name="sql" connectionString="server=127.0.0.1;Database=crm_db;UID=root;PWD=1" />
</connectionStrings>