Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RazorでのDapperサンプル(トランザクション使ってUpdate)
@using Dapper;
@{
using( var cn = new System.Data.SqlServerCe.SqlCeConnection("接続文字列"))
{
cn.Open();
using(var tr = cn.BeginTransaction()){
try
{
// Executeメソッドの第三引数にトランザクションオブジェクトを渡す
cn.Execute("UPDATE users SET Age = @Age Where ID = @ID", new {Age = 25 , ID = 1} , tr);
tr.Commit();
}
catch(Exception e)
{
tr.Rollback();
//実際のコードだと、ここでログ出力や何かエラー処理すればいい
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.