本文整理汇总了C#中DataBase.BeginTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# DataBase.BeginTransaction方法的具体用法?C# DataBase.BeginTransaction怎么用?C# DataBase.BeginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataBase
的用法示例。
在下文中一共展示了DataBase.BeginTransaction方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryToVote
public bool TryToVote(PostVotingData instance)
{
using (var db = new DataBase())
{
db.BeginTransaction();
var post = db.PostTable.FirstOrDefault(i => i.Id == instance.PostId);
if (post == null)
return false;
//Update vote
var copyVote = db.PostVotingTable.FirstOrDefault(i => i.PostId == instance.PostId && i.UserId == instance.UserId);
if (copyVote != null)
{
//Delete opposite vote
if (copyVote.IsVoteUp != instance.IsVoteUp)
{
db.PostVotingTable.Where(i => i.PostId == copyVote.PostId && i.UserId == copyVote.UserId).Delete();
}
else
{
//User already have such vote
return false;
}
}
else
{
db.Insert(instance);
}
//Update post ratig
var postRating = post.Rating + (instance.IsVoteUp ? 1 : -1);
db.PostTable.Where(i => i.Id == instance.PostId).Set(i => i.Rating, postRating).Update();
db.CommitTransaction();
return true;
}
}
示例2: Transaction
/// <summary>
/// 开始事务
/// </summary>
/// <param name="database">数据库对象</param>
/// <param name="isolationLevel">事务方法</param>
public Transaction(DataBase database, IsolationLevel isolationLevel)
{
_dbHelper = database;
_dbHelper.BeginTransaction(isolationLevel);
}