示例#1
0
        public static bool InsertOrUpdateBooks(string path, List <Book> books, string userId)
        {
            var result = true;

            ClearBooks(path, userId);
            using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), path))
            {
                db.CreateTable <BookShelfSchema>();
                db.RunInTransaction(() =>
                {
                    try
                    {
                        foreach (var book in books)
                        {
                            var schema = new BookShelfSchema()
                            {
                                BookId   = book.BookId,
                                UserId   = userId,
                                BookJson = JsonConvert.SerializeObject(book),
                            };
                            db.Insert(schema);
                        }
                    }
                    catch (Exception)
                    {
                        result = false;
                    }
                });
            }
            return(result);
        }
示例#2
0
        public static bool InsertOrUpdateBook(string path, Book book, string userId)
        {
            bool result = true;

            using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), path))
            {
                db.CreateTable <BookShelfSchema>();
                db.RunInTransaction(() =>
                {
                    try
                    {
                        var temp = (from m in db.Table <BookShelfSchema>()
                                    where m.BookId == book.BookId && m.UserId == userId
                                    select m
                                    ).FirstOrDefault();
                        if (temp == null)
                        {
                            var schema = new BookShelfSchema()
                            {
                                BookId   = book.BookId,
                                UserId   = userId,
                                BookJson = JsonConvert.SerializeObject(book),
                            };
                            db.Insert(schema);
                        }
                        else
                        {
                            temp.BookJson = JsonConvert.SerializeObject(book);
                            temp.UserId   = userId;
                            db.Update(temp);
                        }
                    }
                    catch (Exception)
                    {
                        result = false;
                    }
                });
            }
            return(result);
        }