Ejemplo n.º 1
0
        /// <summary>
        /// Adds to database.
        /// </summary>
        /// <returns></returns>
        public static int AddToDatabase()
        {
            using ( SQLiteConnection conn = new SQLiteConnection(Db.TableLocation) )
                            {
                                using ( SQLiteCommand cmd = new SQLiteCommand() )
                                    {
                                        cmd.Connection = conn;
                                        conn.Open();
                                        SqLiteHelper sh = new SqLiteHelper(cmd);

                                        // Look for item in database and return its ID
                                        DataTable dt =
                                            sh.Select(
                                                "SELECT item.id, item.title, type.type " + "FROM item " +
                                                "JOIN Type ON item.typeId = Type.id " +
                                                "WHERE UPPER(title) = UPPER(@title) AND " + "type = @type ;",
                                                new[]
                                                    {
                                                        new SQLiteParameter("@title", SearchItem.Media.Title),
                                                        new SQLiteParameter("@type", SearchItem.Media.MediaType)
                                                    });

                                        if ( dt.Rows.Count == 0 )
                                            {
                                                // Item doesn't exist in DB - Add it and get ID
                                                Dictionary <string, object> newItemData = GetItemDetails();
                                                sh.Insert("item", newItemData);

                                                return Convert.ToInt32(sh.LastInsertRowId()); // ID of last insert
                                            }

                                        // Item exists - Get ID
                                        int itemId = Convert.ToInt32(dt.Rows[0]["id"]);
                                        Dictionary <string, object> newData = GetItemDetails();
                                        sh.Update("item", newData, "id", itemId);

                                        return itemId;
                                    }
                            }
        }