Пример #1
0
        public void SearchBook(GridView GridView1, Dictionary <string, string> queryString)
        {
            SearchResult searchResult = new SearchResult();
            Book         book         = new Book();
            DataTable    dt           = new DataTable();

            try
            {
                IMongoDatabase mdb = MongoDBConnectionManager.ConnectToMongoDB();

                BsonDocument query = new BsonDocument();

                if (queryString.ContainsKey("Title"))
                {
                    query.Add("Title", queryString["Title"]);
                }

                if (queryString.ContainsKey("ISBN"))
                {
                    query.Add("Isbn", queryString["ISBN"]);
                }

                if (queryString.ContainsKey("Author"))
                {
                    query.Add("Author", queryString["Author"]);
                }

                if (queryString.ContainsKey("Publisher"))
                {
                    query.Add("Publisher", queryString["Publisher"]);
                }

                var collection = mdb.GetCollection <Book>("Book");

                var sort = Builders <Book> .Sort.Ascending("Title");

                if (query.ElementCount > 0)
                {
                    var list = collection.Find(query).Sort(sort).ToList();
                    dt = ConvertToDataTable(list);
                }
                else
                {
                    var list = collection.Find(Builders <Book> .Filter.Empty).Sort(sort).ToList();
                    dt = ConvertToDataTable(list);
                }

                // Bind Book List to GridView
                GridView1.DataSource = dt;

                GridView1.DataBind();
            }
            catch (Exception error)
            {
                throw new Exception(error.Message);
            }
        }
Пример #2
0
        public void DeleteBook(ObjectId id)
        {
            try
            {
                IMongoDatabase _database  = MongoDBConnectionManager.ConnectToMongoDB();
                var            collection = _database.GetCollection <Book>("Book");

                var filter = Builders <Book> .Filter.Eq("_id", id);

                var result = collection.DeleteOne(filter);
            }

            catch (Exception error)
            {
                throw new Exception(error.Message);
            }
        }
Пример #3
0
        public void InsertBook(string isbn, string title, string publisher, string author, string description)
        {
            try
            {
                IMongoDatabase _database  = MongoDBConnectionManager.ConnectToMongoDB();
                var            collection = _database.GetCollection <Book>("Book");

                this.isbn        = isbn;
                this.title       = title;
                this.publisher   = publisher;
                this.author      = author;
                this.description = description;

                collection.InsertOne(this);
            }
            catch (Exception error)
            {
                throw new Exception(error.Message);
            }
        }
Пример #4
0
        public void UpdateBook(ObjectId id, string isbn, string title, string publisher, string description)
        {
            try
            {
                IMongoDatabase _database = MongoDBConnectionManager.ConnectToMongoDB();

                var collection = _database.GetCollection <Book>("Book");
                var filter     = Builders <Book> .Filter.Eq("_id", id);

                var update = Builders <Book> .Update
                             .Set("Isbn", isbn)
                             .Set("Title", title)
                             .Set("Publisher", publisher)
                             .Set("Description", description);

                var result = collection.UpdateOne(filter, update);
            }
            catch (Exception error)
            {
                throw new Exception(error.Message);
            }
        }