Dapper.Lite 使用教程
以MySQL数据库为例一. 安装
NuGet搜索Dapper.Lite并安装最新版本。
https://img2023.cnblogs.com/blog/174862/202306/174862-20230602155913303-757935399.jpg
NuGet搜索MySql.Data并安装最新版本。
https://img2023.cnblogs.com/blog/174862/202306/174862-20230602160130367-1455478811.jpg
二. 实现数据库Provider
using Dapper.Lite;
using MySql.Data.MySqlClient;
using System.Data.Common;
namespace DAL
{
public class MySQLProvider : MySQLProviderBase, IDbProvider
{
#region 创建 DbConnection
public override DbConnection CreateConnection(string connectionString)
{
return new MySqlConnection(connectionString);
}
#endregion
#region 生成 DbParameter
public override DbParameter GetDbParameter(string name, object value)
{
return new MySqlParameter(name, value);
}
#endregion
}
}三. 创建IDapperLiteClient实例
IDapperLiteClient db = new DapperLiteClient(
"Data Source=localhost;Port=3306;User ID=root;Password=123456;Initial Catalog=litesql_test;Charset=utf8mb4;SslMode=none;Allow User Variables=True;",
DBType.MySQL,
new MySQLProvider());四. 查询示例
SQL查询
IDbSession session = db.GetSession();ISqlString sql = session.Sql("select * from sys_user where id
页:
[1]