Skip to content

Instantly share code, notes, and snippets.

@webgio
Last active June 9, 2017 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webgio/5383f259c38436a4247c71e9c92d1e76 to your computer and use it in GitHub Desktop.
Save webgio/5383f259c38436a4247c71e9c92d1e76 to your computer and use it in GitHub Desktop.
run with dapper (and NHibernate session and ransaction)
public void RunInSql(Action<IDbConnection, IDbTransaction> action)
{
using (var connection = session.Connection)
{
using (var transaction = GetTransaction(session))
{
try
{
action(connection, transaction);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
}
}
DapperRun.RunInSql((conn, trans) => {
var id = conn.ExecuteScalar<int>("select top 1 new_id from mytable", trans);
conn.ExecuteScalar("insert into mytable", trans);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment