本文介绍了"dapper-dot-net"中的交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的DAL使用dapper-dot-net,如何创建交易?

How do I create a transaction if my DAL is using dapper-dot-net?

我的c#winform应用程序将在网络中使用,数据将保存到中央sql服务器.

My c# winform application will be used in network and the data will be saved to a central sql server.

我的用例需要使用事务.我可以使用dapper进行此操作,还是需要使用NHibernate之类的内容?

My use case requires use of transactions. Can I do this using dapper, or will I need to use something like NHibernate?

此外,如果我使用存储过程,此框架是否存在任何风险或限制?由于任何可能的限制,我是否需要更改方法?

Also, is there any risk or limitation with this framework if I am using stored procedures? Will I need to change my approach due any possible limitations?

推荐答案

使用sproc并没有遇到任何限制,使用dapper的风险与使用sproc的风险相同

I haven't run into any limitations with using sprocs and the risks you have with dapper are the same risks you would have with sprocs

这是一个关于如何使用dapper进行交易的简单示例

Here is a simple example on how to use transactions with dapper

using (var connection = Db.GetConnection())
{
     connection.Open();
     IDbTransaction transaction = connection.BeginTransaction();
     try
     {
         var newId= connection.Query<int>(@"Select id from table1 where id=@id", new{id}, transaction).Single();
         connection.Execute(@"INSERT into table1 ...",new {p1, p2}, transaction);
         connection.Execute(@"INSERT into table2 ...",new {p1, p2}, transaction);
         transaction.Commit();
     }
     catch (Exception ex)
     {
         transaction.Rollback();
     }
}

这篇关于"dapper-dot-net"中的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:16