Skip to content

Instantly share code, notes, and snippets.

@kiyokura
Created October 27, 2013 17:31
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 kiyokura/7185408 to your computer and use it in GitHub Desktop.
Save kiyokura/7185408 to your computer and use it in GitHub Desktop.
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