var result = 0;
var userId = 0;
using (var db = _Sql.Connection)
using (var tran =db.BeginTransaction())
{
var insertUserActionSql = @"INSERT INTO [dbo].[Base_User]
([UserName]
,[Password]
,[PhoneNumber]
,[Email]
,[IsDeleted]
,[CreateDate]
,[CreateUserId]
,[CreateUserName]
,[ModifyDate]
,[ModifyUserId]
,[ModifyUserName])
VALUES
(@UserName
,@Password
,@PhoneNumber
,@Email
,@IsDeleted
,@CreateDate
,@CreateUserId
,@CreateUserName
,@ModifyDate
,@ModifyUserId
,@ModifyUserName); SELECT CAST(SCOPE_IDENTITY() AS BIGINT) AS [UserId] "; //必须这么写才行 不能用@@identity 方式,dapper不支持 

var insertUserRoler = @" INSERT INTO [dbo].[Base_UserRole]([UserId],[RoleId]) VALUES(@UserId,@RoleId)";
try
{

userId = db.ExecuteScalar<int>(insertUserActionSql, editViewModel, tran);

result = db.Execute(insertUserRoler, new { UserId = userId, RoleId = editViewModel.RoleId },tran);
tran.Commit();
}
catch (SqlException ex)
{
tran.Rollback();
LogHelper.AddErrorLog("Create", this.GetType(), ex);
return false;
}
catch (Exception ex)
{
tran.Rollback();
LogHelper.AddErrorLog("Create", this.GetType(), ex);
return false;
}
}//end using
return result > 0;

posted on 2017-10-27 14:47  Hai_yi  阅读(1421)  评论(0编辑  收藏  举报