当前位置:网站首页>NetCore使用Dapper查询数据
NetCore使用Dapper查询数据
2022-08-08 04:01:00 【有诗亦有远方】
NetCore使用Dapper查询数据
Nuget包安装
Dapper
MySqlConnector
二、Class
1.Student
public class Student
{
public int ID {
get; set; }
public string Name {
get; set; } ;
public string Sex {
get; set; } ;
public string Phone {
get; set; }
public string Address {
get; set; }
}
2.IStudentQueries
public interface IStudentQueries
{
Task<List<Student>> GetStudent(int studentId);
}
3.StudentQueries
public class StudentQueries :IStudentQueries
{
private string _connectionString = string.Empty;
public StudentQueries(string constr)
{
_connectionString = !string.IsNullOrWhiteSpace(constr) ? constr : throw new ArgumentNullException(nameof(constr));
}
public async Task<List<Student>> GetStudent(int studentId)
{
using(var dbconnection = new MySqlConnection(_connectionString))
{
dbconnection.Open();
var sb = new StringBuilder();
var sql = "" ;
if (studentId >0)
{
sql = [email protected]" select * from student where id = @studentId";
}
return (await dbconnection.QueryAsync<Student>(sql, new {
studentId })).ToList();
}
}
}
4.MediatorModule 使用Autofac注入
public class MediatorModule : Autofac.Module
{
//构造函数,将链接字符串传入
public string QueriesConnectionString {
get; }
public MediatorModule(string qconstr)
{
QueriesConnectionString = qconstr;
}
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new StudentQueries(QueriesConnectionString))
.As<IStudentQueries>()
.InstancePerLifetimeScope();
}
5.starup
与ConfigureServices同级
public virtual void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new MediatorModule(Configuration.GetConnectionString("ConnectionMysql")));
}
6.connectionstring添加allowuservariables=True
allowuservariables=True;允许以参数传入
"ConnectionMysql": "server=localhost;uid=root;pwd=123456;database=studenttest;allowuservariables=True; "
7.Controller
public async Task<List<Student>> GetSqlData(int studentId)
{
return await _productQueries.GetStudent(studentId);
//操纵stream,生成集合
}
三、效果展示


边栏推荐
- 抽象工厂模式:其他工厂的工厂
- 力扣第 305 场周赛
- The live broadcast of agricultural products continues to heat up, Economic Daily: Don’t forget quality when rushing
- 风控策略必学|这种用决策树来挖掘规则的方法
- three.js中射线对性能的影响
- C# "Original CSharp" 4th Chapter It is common for people to change in time, but it is difficult to know the succession of humanities
- egg-Alibaba Cloud SMS Configuration
- 【图基础】如何定义异质图上的小样本学习:Heterogeneous Graph Few-Shot Learning
- vulnhub-DC-3靶机渗透记录
- 高效记忆法
猜你喜欢

从精装到智装,下一波浪潮浮现,听听智能家居的大咖们怎么说?

fail-fast 和 fail-safe 快速学习

egg-Alibaba Cloud SMS Configuration

Week 4 Step by step building multi-layer neural network and application (1 & 2)

PC Museum (Fanwai 01)-Chenghuiwan, junior high school students develop a large-scale navigation game with physical scales

136. Single Number A number that only appears once

手把手教你手撕SPF生成树(OSPF区域内防环)

妙才周刊

06 tp6 的数据更新(改)及删除 《ThinkPHP6 入门到电商实战》

监控工具Prometheus及项目总结,220805,,
随机推荐
hreg说明备忘
项目管理流程及各环节要点
模拟登录——添加cookies,使用postmanget请求网页数据
Hangzhou Electric Power Multi-School 6 1010. Planar graph
Solve the problem of word flashback when Endnote inserts references
强网杯 2019-随便注 (堆叠注入)
mfc对话框添加OnMouseMove
高效记忆法
失业在家的6个月,我通过外包全款买了房:你看不起的行业,往往很赚钱
杭电多校6 1010. Planar graph
文本生成介绍
easypoi自定义模板导出
使用z-file和七牛云对象存储构建个人网盘
New retail project and offline warehouse core interview,, 220807,,
Mini Program Optimization Practice
SQL 2016 如何跟踪是哪个存储或语句导致记录表中记录被修改了
数据在内存如何分布的?
【模板引擎】velocity
开发如何尽可能的避免BUG
保姆级教程!Golang微服务简洁架构实战