博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dapper学习(三)之其他用法
阅读量:5038 次
发布时间:2019-06-12

本文共 2562 字,大约阅读时间需要 8 分钟。

这里说的其他用法,是指 Async,Buffered,Transaction,Stored Procedure。

1. 首先 dapper支持异步

ExecuteAsync, QueryAsync, QueryFirstAsync, QueryFirstOrDefaultAsync, QuerySingleAsync, QuerySingleOrDefaultAsync, QueryMultipleAsync 

ExecuteAsync 用法示例:
string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){    var affectedRows = connection.ExecuteAsync(sql, new {CustomerName = "Mark"}).Result;    Console.WriteLine(affectedRows);    var customer = connection.Query
("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList(); FiddleHelper.WriteTable(customer);}

2. Buffered

默认是:True

A buffered query return the entire reader at once. 

A non-bufferd query is equivalent as streaming. you only load objects on demand. That can be useful for a very large query to reduce memory usage. 

string sql = "SELECT * FROM OrderDetails;";using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){    var orderDetails = connection.Query
(sql, buffered: false).ToList(); FiddleHelper.WriteTable(orderDetails.Take(10));}

3. Transaction

dapper支持 transaction 和 TransactionScope

Transaction

使用示例如下:

string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())){    connection.Open();        using (var transaction = connection.BeginTransaction())    {        var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"}, transaction: transaction);                transaction.Commit();                Console.WriteLine(affectedRows);    }}

TransactionScope

在连接之前,使用 transaction scope

// using System.Transactions;using (var transaction = new TransactionScope()){    var sql = "Invoice_Insert";    using (var connection = My.ConnectionFactory())    {        connection.Open();        var affectedRows = connection.Execute(sql,            new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},            commandType: CommandType.StoredProcedure);    }    transaction.Complete();}

4. Stored Procedure

在dapper中使用存储过程,只需要指定命令类型

var sql = "Invoice_Insert";using (var connection = My.ConnectionFactory()){    connection.Open();    var affectedRows = connection.Execute(sql,        new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},        commandType: CommandType.StoredProcedure);    My.Result.Show(affectedRows);}

 

转载于:https://www.cnblogs.com/Vincent-yuan/p/11521148.html

你可能感兴趣的文章
OI再见
查看>>
自定义单选框或者复选框
查看>>
xml知识点
查看>>
隐式类型转换
查看>>
目前国内几大著名报表软件(2014更新)
查看>>
我想要得那块牌—记烟台大学第一届"ACM讲堂"
查看>>
【LaTeX排版】LaTeX论文模版
查看>>
mysql 如何实现跨裤查询
查看>>
redis的string类型操作命令
查看>>
Uva 10305 给任务排序
查看>>
Uva 11396 爪分解
查看>>
Windows2012启动自动帐户登陆
查看>>
python中包的语法
查看>>
那些常考的问答面试题(三)
查看>>
使用Maven管理Spring、Spring MVC、Hibernate的Library
查看>>
iview实战 : 树形组件自定义
查看>>
cookie
查看>>
【Head First Java 读书笔记】(六)认识Java API
查看>>
什么是跨域,什么是同源
查看>>
Python基础之【第一篇】
查看>>