MVC + SQLite:更新不起作用。句法?

时间:2019-04-15 09:27:32

标签: c# sqlite

我正在用MVC编程一个库应用程序。我是第一次使用数据库和 Dapper 连接。从MVC到SQLite

我的数据库如下:

  • 表名:SubscriberBook
  • 列:SubscriberId / BookId / BorrowDate / ReturnDate

创建借项后, ReturnDate 设置为空。

当订户退回一本书时,我想保留其他数据并使用当前日期更新 ReturnDate ,例如DateTime.now.Ticks。

型号:

public class SubscriberBookEntity
{
    public int BookId { get; set; }
    public int SubscriberId { get; set; }
    public DateTime BorrowDate { get; set; }
    public DateTime ReturnDate { get; set; }
}

控制器,调用创建+更新:

[HttpPost]
    public ActionResult SubscriberBorrow(Borrow borrow)
    {
        SubscriberEntity subscriberEntity = new SubscriberEntity();
        SubscriberBookEntity subscriberBookEntity = new SubscriberBookEntity();
        List<SubscriberBookEntity> subscriberBookEntityList = new List<SubscriberBookEntity>();
        subscriberBookEntityList = Manager.GetAllBorrow();

            foreach (Book borrowBook in borrow.Library.BookList)
            {
                if (borrowBook.IsChecked && Error == false)
                {
                    subscriberBookEntity.DateEmprunt = DateTime.Now;
                    subscriberBookEntity.DateRetour = new DateTime();

                    Manager.CreateBorrow(borrow.SelectedSubscriberId, borrowBook.Id, subscriberBookEntity.DateEmprunt, subscriberBookEntity.DateRetour);
                    subscriberBookEntityList = Manager.GetAllBorrow();
                }
            }

        if (borrow.Button.Equals("Return"))
        {
            foreach (Book borrowBook in borrow.Library.BookList)
            {
                if (borrowBook.IsChecked)
                {
                    subscriberBookEntity.ReturnDate = DateTime.Now;
                    Manager.UpdateBorrow(subscriberBookEntity);
                }
            }

创建:

public static void CreateBorrow(int subscriberId, int bookId, DateTime dateEmprunt, DateTime dateRetour)
    {
        using (var connection = new SQLiteConnection(GetConnectionString()))
        {
            connection.Execute("INSERT INTO SubscriberBook (SubscriberId, BookId, DateEmprunt) VALUES (@subscriberId, @bookId, @dateEmprunt)",
            new
            {
                SubscriberId = subscriberId,
                BookId = bookId,
                DateEmprunt = dateEmprunt.Ticks,
            });
        }
    }

获取:

public static List<SubscriberBookEntity> GetAllBorrow()
    {
        using (var connection = new SQLiteConnection(GetConnectionString()))
        {
            return connection.Query<SubscriberBookEntity>("SELECT BookId, SubscriberId FROM SubscriberBook").ToList();
        }
    }

更新:

public static void UpdateBorrow(SubscriberBookEntity subscriberBookEntity)
    {
        using (var connection = new SQLiteConnection(GetConnectionString()))
        {
            if(subscriberBookEntity != null)
            {
                connection.Execute("UPDATE SubscriberBook SET ReturnDate = @returnDate WHERE BookId = @bookId AND SubscriberId = @subscriberId",
                new
                {
                    returnDate = subscriberBookEntity.ReturnDate.Ticks,
                    bookId = subscriberBookEntity.BookId,
                    subscriberId = subscriberBookEntity.SubscriberId,
                });
            }
        }
    }

我没有错误,但是数据库没有任何变化。 ReturnBorrow的值仍为null。

感谢您的耐心和帮助。

1 个答案:

答案 0 :(得分:0)

经过一些调查,我在控制器中用于“返回”的代码未使用正确的变量。

更正后:

if (borrow.Button.Equals("Retourner"))
        {
            foreach (SubscriberBookEntity subscriberBookEntity in subscriberBookEntityList)
            {
                foreach(Book borrowBook in borrow.Library.BookList)
                {
                    if(borrowBook.IsChecked && borrowBook.Id == subscriberBook.BookId && subscriberBook.SubscriberId == borrow.SelectedSubscriberId)
                    {
                        subscriberBook.DateRetour = DateTime.Now;
                        Manager.UpdateBorrow(subscriberBook);
                    }
                }
            }

            List<SubscriberBookEntity> SubscriberBookEntityList = Manager.GetAllBorrow();

        }

感谢@Steve和@Patrick Hofman的建议。