Created
October 27, 2013 17:31
-
-
Save kiyokura/7185408 to your computer and use it in GitHub Desktop.
RazorでのDapperサンプル(トランザクション使ってUpdate)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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