Dapper

.netCoreApi通过Dapper 连接Mysql数据库

1.NuGet搜索安装

Dapper&SapientGuardian.MySql.Data

2.新建user类(与数据库表字段对应)

public class User_t
{
[Key]
public string id { get; set; }
public string name { get; set; }
public string password { get; set; }
public string role { get; set; }
}

3.连接Mysql数据库,查询数据

using MySql.Data.MySqlClient;
using Dapper;

 

string mysqlConnectionStr = "server=localhost;uid=userid;pwd=password;port=3306;database=db;"; 

MySqlConnection connection = new MySqlConnection(mysqlConnectionStr);

var userList = (List<User_t>)connection.Query<User_t>("select * from user_t ");
return userList;

posted @ 2022-01-18 22:10  起风了zzz  阅读(47)  评论(0)    收藏  举报