-->

数据写不进数据库

2019-01-02 21:18发布

问题:

用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();
}
}
}
}
}

回答1:

断点,调试,看异常信息



回答2:

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



标签: