当前位置:网站首页>NetCore uses Dapper to query data
NetCore uses Dapper to query data
2022-08-08 04:07:00 【Some poems are far away】
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
{
//构造函数,Pass in the link string
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;Allows to be passed in as a parameter
"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,生成集合
}
三、效果展示


边栏推荐
- Heterogeneous on the Graph paper to share 】 【 small sample learning: HG - Meta: Graph Meta - learning over Heterogeneous Graphs
- 一行代码统计文本中指定字符串出现的次数
- Lecture 305th game of the week
- leetcode: 122. 买卖股票的最佳时机 II
- 以0为底或以1为底对图片迭代次数的影响
- egg-validate-自定义校验方法报错语言(报错中文提示)
- ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
- 【代码分析】图小样本异常检测方法:GDN:Few-shot Network Anomaly Detection via Cross-network Meta-learning
- DolpinScheduler
- 【直播回顾】昇思MindSpore易用性SIG2022上半年回顾总结
猜你喜欢

leetcode 112.路经总和 递归

Vulfocus Shooting Range Scenario Mode - Intranet Dead End

第4周 一步步搭建多层神经网络以及应用(1 & 2)

Nanny level tutorial!Golang microservices simple architecture in practice

vulnhub-DC-5靶机渗透记录

10款自媒体人必备的免费工具,快速高效运营

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

数据库篇复习篇

Strong Net Cup 2019 - Casual Bet (Stacked Injection)

The difference between orElse and orElseGet in Optional
随机推荐
【论文分享】异质图上的小样本学习:HG-Meta: Graph Meta-learning over Heterogeneous Graphs
MySQL——索引与事务
小程序优化实践
高效记忆法
语音鉴定软件
Bluetooth att gatt agreement
cube-studio 部署过程
An egg - Nodemailer - qq email verification code development configuration
New User Plane Design and Key Technologies in the 6G Era
数据库篇复习篇
leetcode: 122. 买卖股票的最佳时机 II
32. Do you know how Redis strings are implemented?
Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model
保姆级教程!Golang微服务简洁架构实战
leetcode: 874. 模拟行走机器人
【模板引擎】velocity
unity之粒子特效制作图片拼合文字效果
项目管理流程及各环节要点
A line of code counts the number of occurrences of the specified string in the text
[Graph Basics] How to Define Few-Shot Learning on Heterogeneous Graphs: Heterogeneous Graph Few-Shot Learning