问题描述
如果我的 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?
推荐答案
我在使用 sprocs 时没有遇到任何限制,使用 dapper 的风险与使用 sprocs 的风险相同
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"中的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!