首页 新闻 会员 周边 捐助

数据写不进数据库

0
[已解决问题] 解决于 2018-11-22 16:02

用MVC写的代码在VS中用IIS服务器向数据库写入数据失败,

但是同样的数据库和同样的代码运行能在其他电脑上能显示出来!

代码如下:

public class DatabaseOrderProcessor : IOrderProcessor
{
private 校园自助图书管理系统Entities db = new 校园自助图书管理系统Entities();
public IQueryable<Borrow> Borrows { get { return db.Borrow; } }
public IQueryable<Fine> Fines { get { return db.Fine; } }
public void ProcessBorrow(List<Book> books, Reader reader)
{
//db是一个具有各项模型属性的上下文类
using (var db = new 校园自助图书管理系统Entities())
{
//创建事务
using (var dbContextTransaction = db.Database.BeginTransaction( ))
{
try
{
foreach (var book in books)
{
Borrow borrow = new Borrow();
borrow.BookId = book.Id;
borrow.ReaderId = reader.Id;
borrow.BorrowTime = DateTime.Now;
borrow.DateShouldBeReturn = DateTime.Now.AddMonths(1);
db.Borrow.Add(borrow);
Fine fine = new Fine();
fine.BookId = book.Id;
fine.BorrowId = borrow.Id;
fine.FinePrice = 0;
db.Fine.Add(fine);
}
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}
}
}
public void ProcessReturn(Book book,Reader reader)
{
//db是一个具有各项模型属性的上下文类
using (var db = new 校园自助图书管理系统Entities())
{
//创建事务
using (var dbContextTransaction = db.Database.BeginTransaction())
{
try
{
Borrow borrow = db.Borrow.FirstOrDefault(br => br.BookId == book.Id&&br.Reader.Id== reader.Id);
if(borrow.DateShouldBeReturn<DateTime.Now)
{
db.Fine.FirstOrDefault(f=>f.BookId==book.Id).FinePrice = (Decimal)((DateTime.Now - borrow.DateShouldBeReturn).TotalDays)/10;
}

db.Borrow.Remove(borrow);
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}
}
}
public void ProcessBorrowAgain(Book book, Reader reader)
{
//db是一个具有各项模型属性的上下文类
using (var db = new 校园自助图书管理系统Entities())
{
//创建事务
using (var dbContextTransaction = db.Database.BeginTransaction())
{
try
{
Borrow borrow = db.Borrow.FirstOrDefault(br => br.BookId == book.Id && br.Reader.Id == reader.Id);
if(borrow.DateShouldBeReturn>DateTime.Now)
{
borrow.WhetherToRenew = "已续借";
borrow.DateShouldBeReturn = borrow.DateShouldBeReturn.AddMonths(1);
}
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}
}
}

public bool Norepeat(Book book,Reader reader)
{
if(db.Borrow.Where(br => br.BookId == book.Id&&br.ReaderId==reader.Id).Count() == 0)
{
return true;
}
else
{
return false;
}
}

public IEnumerable<Borrow> FindBorrowsByReaderId(Reader reader)
{
return db.Borrow.Where(u => u.ReaderId == reader.Id);
}
public Borrow FindBorrowsByReaderIdandBookId(Reader reader,Book book)
{
return db.Borrow.FirstOrDefault(u => u.ReaderId == reader.Id&&u.BookId==book.Id);
}
public Fine FindFineByBookIdandBorrowId(Book book,Borrow borrow)
{
return db.Fine.FirstOrDefault(f => f.BookId == book.Id&&f.BorrowId==borrow.Id);
}

public void ProcessPayFine(Reader reader,Book book)
{
//db是一个具有各项模型属性的上下文类
using (var db = new 校园自助图书管理系统Entities())
{
//创建事务
using (var dbContextTransaction = db.Database.BeginTransaction())
{
try
{
Fine fine = db.Fine.FirstOrDefault(f => f.BookId == book.Id);
reader.Price -= fine.FinePrice;
fine.FinePrice = 0;
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}
}
}
public void updateReader(Reader reader)
{
//db是一个具有各项模型属性的上下文类
using (var db = new 校园自助图书管理系统Entities())
{
//创建事务
using (var dbContextTransaction = db.Database.BeginTransaction())
{
try
{
Reader re = db.Reader.FirstOrDefault(r => r.Id == reader.Id);
re.Password = reader.Password;
re.Price = reader.Price;
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}
}
}
}

RookieBoy666的主页 RookieBoy666 | 初学一级 | 园豆:156
提问于:2017-12-05 18:34
< >
分享
最佳答案
0

断点,调试,看异常信息

奖励园豆:5
西漠以西 | 小虾三级 |园豆:1675 | 2017-12-06 09:54

谢谢

RookieBoy666 | 园豆:156 (初学一级) | 2017-12-06 15:55
其他回答(1)
0

代码应该没问题,可能和环境有关,你应该贴出来的是错误日志...

顾星河 | 园豆:7330 (大侠五级) | 2017-12-06 09:45

谢谢

支持(0) 反对(0) RookieBoy666 | 园豆:156 (初学一级) | 2017-12-06 15:54

如果没写进数据库,普遍的问题在哪里?排除系统环境

支持(0) 反对(0) RookieBoy666 | 园豆:156 (初学一级) | 2017-12-06 15:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册