dapper的使用
1、引用包
2、student类
[Table("dbo.Student")] public partial class Student { [Required] public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } }
2、DapperRepository中以下两个都需要
①DapperRepository类
public partial class DapperRepository : IDapperRepository { /// <summary> /// 数据库连接对象 /// </summary> private readonly IDbConnection _db; /// <summary> /// 服务提供器 /// </summary> private readonly IServiceProvider _serviceProvider; /// <summary> /// 构造函数 /// </summary> /// <param name="serviceProvider"></param> /// <param name="db"></param> public DapperRepository( IDbConnection db , IServiceProvider serviceProvider = null ) { _serviceProvider = serviceProvider; _db = db; } /// <summary> /// 连接上下文 /// </summary> public virtual IDbConnection Context { get { if (_db.State != ConnectionState.Open) _db.Open(); return _db; } } /// <summary> /// 动态连接上下文 /// </summary> public virtual dynamic DynamicContext { get; } /// <summary> /// 查询返回动态类型 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="buffered"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> public virtual IEnumerable<dynamic> Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) { return Context.Query(sql, param, transaction, buffered, commandTimeout, commandType); } /// <summary> /// 查询返回特定类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="buffered"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> public virtual IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) { return Context.Query<T>(sql, param, transaction, buffered, commandTimeout, commandType); } /// <summary> /// 查询返回动态类型 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> public virtual Task<IEnumerable<dynamic>> QueryAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Context.QueryAsync(sql, param, transaction, commandTimeout, commandType); } /// <summary> /// 查询返回特定类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> public virtual Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Context.QueryAsync<T>(sql: sql, param, transaction, commandTimeout, commandType); } /// <summary> /// 执行命令 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> public virtual int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Context.Execute(sql, param, transaction, commandTimeout, commandType); } /// <summary> /// 执行命令 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> public virtual Task<int> ExecuteAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Context.ExecuteAsync(sql, param, transaction, commandTimeout, commandType); } /// <summary> /// 切换仓储 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <returns>仓储</returns> public virtual IDapperRepository<TEntity> Change<TEntity>() where TEntity : class, new() { throw new NotImplementedException(); // return _serviceProvider.GetService<IDapperRepository<TEntity>>(); } }
②DapperRepository
/// <summary> /// Dapper 仓储实现类 /// </summary> /// <typeparam name="TEntity"></typeparam> public partial class DapperRepository<TEntity> : DapperRepository, IDapperRepository<TEntity> where TEntity : class, new() { /// <summary> /// 构造函数 /// </summary> /// <param name="serviceProvider"></param> /// <param name="db"></param> public DapperRepository(IDbConnection db , IServiceProvider serviceProvider = null) : base(db, serviceProvider) { } /// <summary> /// 获取一条 /// </summary> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual TEntity Get(object id, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.Get<TEntity>(id, transaction, commandTimeout); } /// <summary> /// 获取一条 /// </summary> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual Task<TEntity> GetAsync(object id, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.GetAsync<TEntity>(id, transaction, commandTimeout); } /// <summary> /// 获取所有实体 /// </summary> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual IEnumerable<TEntity> GetAll(IDbTransaction transaction = null, int? commandTimeout = null) { return Context.GetAll<TEntity>(transaction, commandTimeout); } /// <summary> /// 获取所有实体 /// </summary> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual Task<IEnumerable<TEntity>> GetAllAsync(IDbTransaction transaction = null, int? commandTimeout = null) { return Context.GetAllAsync<TEntity>(transaction, commandTimeout); } /// <summary> /// 新增一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual long Insert(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.Insert(entity, transaction, commandTimeout); } /// <summary> /// 新增一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="sqlAdapter"></param> /// <returns></returns> public virtual Task<int> InsertAsync(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null, ISqlAdapter sqlAdapter = null) { return Context.InsertAsync(entity, transaction, commandTimeout, sqlAdapter); } /// <summary> /// 新增多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual long Insert(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.Insert(entities, transaction, commandTimeout); } /// <summary> /// 新增多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="sqlAdapter"></param> /// <returns></returns> public virtual Task<int> InsertAsync(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null, ISqlAdapter sqlAdapter = null) { return Context.InsertAsync(entities, transaction, commandTimeout, sqlAdapter); } /// <summary> /// 更新一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual bool Update(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.Update(entity, transaction, commandTimeout); } /// <summary> /// 更新一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual Task<bool> UpdateAsync(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.UpdateAsync(entity, transaction, commandTimeout); } /// <summary> /// 更新多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual bool Update(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.Update(entities, transaction, commandTimeout); } /// <summary> /// 更新多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual Task<bool> UpdateAsync(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.UpdateAsync(entities, transaction, commandTimeout); } /// <summary> /// 删除一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual bool Delete(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.Delete(entity, transaction, commandTimeout); } /// <summary> /// 删除一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual Task<bool> DeleteAsync(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.DeleteAsync(entity, transaction, commandTimeout); } /// <summary> /// 删除多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual bool Delete(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.Delete(entities, transaction, commandTimeout); } /// <summary> /// 删除多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public virtual Task<bool> DeleteAsync(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null) { return Context.DeleteAsync(entities, transaction, commandTimeout); } }
3、以下两个都需要
①IDapperRepository
/// <summary> /// 非泛型 Dapper 仓储 /// </summary> public partial interface IDapperRepository { /// <summary> /// 连接上下文 /// </summary> IDbConnection Context { get; } /// <summary> /// 动态连接上下文 /// </summary> dynamic DynamicContext { get; } /// <summary> /// 查询返回动态类型 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="buffered"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> IEnumerable<dynamic> Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null); /// <summary> /// 查询返回特定类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="buffered"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null); /// <summary> /// 查询返回动态类型 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> Task<IEnumerable<dynamic>> QueryAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null); /// <summary> /// 查询返回特定类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null); /// <summary> /// 执行命令 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null); /// <summary> /// 执行命令 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> Task<int> ExecuteAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null); /// <summary> /// 切换仓储 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <returns>仓储</returns> IDapperRepository<TEntity> Change<TEntity>() where TEntity : class, new(); }
②IDapperRepository
/// <summary> /// Dapper 仓储接口定义 /// </summary> /// <typeparam name="TEntity"></typeparam> public partial interface IDapperRepository<TEntity> : IDapperRepository where TEntity : class, new() { /// <summary> /// 获取一条 /// </summary> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> TEntity Get(object id, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 获取一条 /// </summary> /// <param name="id"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> Task<TEntity> GetAsync(object id, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 获取所有实体 /// </summary> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> IEnumerable<TEntity> GetAll(IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 获取所有实体 /// </summary> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> Task<IEnumerable<TEntity>> GetAllAsync(IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 新增一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> long Insert(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 新增一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="sqlAdapter"></param> /// <returns></returns> Task<int> InsertAsync(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null, ISqlAdapter sqlAdapter = null); /// <summary> /// 新增多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> long Insert(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 新增多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="sqlAdapter"></param> /// <returns></returns> Task<int> InsertAsync(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null, ISqlAdapter sqlAdapter = null); /// <summary> /// 更新一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> bool Update(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 更新一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> Task<bool> UpdateAsync(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 更新多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> bool Update(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 更新多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> Task<bool> UpdateAsync(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 删除一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> bool Delete(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 删除一条 /// </summary> /// <param name="entity"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> Task<bool> DeleteAsync(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 删除多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> bool Delete(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null); /// <summary> /// 删除多条 /// </summary> /// <param name="entities"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <returns></returns> Task<bool> DeleteAsync(IEnumerable<TEntity> entities, IDbTransaction transaction = null, int? commandTimeout = null); }
4、MSReposity类
public class MSReposity : DapperRepository { public MSReposity(object sqlConnection) : base(MSReposity.MssConnection, null) { } /// <summary> /// 必须设置数据库连接地址 /// </summary> public static SqlConnection MssConnection { get; set; } } public class MSReposity<T> : DapperRepository<T> where T : class, new() { MSReposity(IDbConnection db, IServiceProvider serviceProvider = null) : base(MSReposity.MssConnection, serviceProvider) { } public MSReposity() : base(MSReposity.MssConnection, null) { } }
5、Service类
internal class Service { private readonly MSReposity<Student> repo; string QuerySql = $"Select * from student"; string UpdateSql = "UPDATE student SET name= @name, address= @address WHERE id = @id;"; string InsertSql = "INSERT INTO student (name, address) VALUES (@name, @address);"; string DeleteSql = "DELETE FROM student WHERE id = @id;"; public Service() { MSReposity.MssConnection = new System.Data.SqlClient.SqlConnection(ConnectionConfiguration.Connection); repo = new MSReposity<Student>(); } /// <summary> /// 查询 /// </summary> /// <returns></returns> public IEnumerable<Student> Query() { List<Student> res = new List<Student>(); try { var tmps = repo.Query<Student>(QuerySql); foreach (var item in tmps) { Student student = new Student() {Id = item.Id, Name = item.Name, Address = item.Address }; res.Add(student); } } catch (Exception exp) { } return res; } /// <summary> /// 更新 /// </summary> /// <param name="input"></param> public void Update(Student input) { try { if (null != input) { repo.Execute(UpdateSql, input); } } catch (Exception exp) { throw exp; } } /// <summary> /// 插入 /// </summary> /// <param name="student"></param> public void Insert(Student input) { try { if (null != input) { repo.Execute(InsertSql, input); } } catch (Exception exp) { throw exp; } } /// <summary> /// 删除 /// </summary> /// <param name="student"></param> public void Delete(Student input) { try { if (null != input) { repo.Execute(DeleteSql, new { id=input.Id}); } } catch (Exception exp) { throw exp; } } }
6、ConnectionConfiguration类
public class ConnectionConfiguration { public static string Connection { get; set; } = @"Data Source=DESKTOP-NJ2V6IC;Initial Catalog=Study;Integrated Security=True"; }
7、页面
public partial class Form1 : Form { Service service; public Form1() { InitializeComponent(); service = new Service(); } /// <summary> /// 加载数据 /// </summary> void Load() { dataGridView1.Rows.Clear(); List<Student> students = service.Query().ToList(); foreach (var student in students) { int index = dataGridView1.Rows.Add(); dataGridView1.Rows[index].Cells[0].Value = student.Id; dataGridView1.Rows[index].Cells[1].Value = student.Name; dataGridView1.Rows[index].Cells[2].Value = student.Address; } } /// <summary> /// 查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { Load(); } /// <summary> /// 修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { try { int index = dataGridView1.CurrentRow.Index; if (index != -1) { Student student = new Student(); student.Id = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString()); student.Name = dataGridView1.Rows[index].Cells[1].Value.ToString(); student.Address = dataGridView1.Rows[index].Cells[2].Value.ToString(); service.Update(student); MessageBox.Show("更新成功"); } } catch (Exception ex) { } } /// <summary> /// 插入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { try { int index = dataGridView1.CurrentRow.Index; if (index != -1) { Student student = new Student(); student.Name = dataGridView1.Rows[index].Cells[1].Value.ToString(); student.Address = dataGridView1.Rows[index].Cells[2].Value.ToString(); service.Insert(student); MessageBox.Show("插入成功"); } } catch (Exception ex) { } } /// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { try { int index = dataGridView1.CurrentRow.Index; if (index != -1) { Student student = new Student(); student.Id = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString()); service.Delete(student); MessageBox.Show("删除成功"); } } catch (Exception ex) { } } }
标签:commandTimeout,transaction,IDbTransaction,实现,int,CURD,dapper,null,public From: https://www.cnblogs.com/wokkkkkk/p/17873810.html