Exemplo n.º 1
0
        /// <summary>
        /// 添加Demo
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Sex"></param>
        /// <param name="Age"></param>
        /// <param name="Remark"></param>
        /// <returns></returns>
        public async Task <OperationResult <bool> > InsertDemoAsync(string Name, string Sex, int Age, string Remark)
        {
            using (IDbTransaction tran = _demoRepository.BeginTransaction())//开始事务
            {
                var result = new OperationResult <bool>();
                try
                {
                    byte[] buffer = Guid.NewGuid().ToByteArray();
                    Demo   demo   = new Demo();
                    demo.Id     = BitConverter.ToInt64(buffer, 0);
                    demo.Name   = Name;
                    demo.Sex    = Sex;
                    demo.Age    = Age;
                    demo.Remark = Remark;
                    //string sql = @"INSERT INTO Demo(Id, Name, Sex, Age, Remark) VALUES(@Id, @Name, @Sex, @Age, @Remark)";
                    //StringBuilder sb = new StringBuilder();
                    //sb.AppendFormat(" INSERT INTO `Demo` (`Id`, `Name`, `Sex`, `Age`, `Remark`) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}');", GuidToLongId(), Name, Sex, Age, Remark);
                    //result.data = await _demoRepository.InsertAsync(demo,sql);//dapper
                    //result.data = await _demoRepository.ExecuteSqlAsync(sb.ToString());//执行sql
                    result.data = await _demoRepository.InsertAsync(demo); //dapper扩展方法

                    _demoRepository.CommitTransaction(tran);               //提交事务
                    return(result);
                }
                catch (Exception ex)
                {
                    _demoRepository.RollbackTransaction(tran);//回滚事务
                    throw ex;
                }
            }
        }
Exemplo n.º 2
0
        public Task AddDemo()
        {
            using (var tran = _repository.BeginTransaction())
            {
                _repository.InsertAsync(new Demo
                {
                    UserName = "******",
                    Age      = 26
                });

                tran.Commit();
            }
            return(Task.CompletedTask);
        }