TechAid24

Your Technical Consultant

Integrating MongoDB with Windows Console Application: Step-by-Step Guide

Posted on by Anonymous Guest 2 minutes read

In this article, we’ll find out how we will do the MongoDB primary integration with Visible Studio 2015. Before we get began, if you wish to learn about exporting GridView information to Excel, please read the next article: Export GridView to Excel with advanced features.

First, we create a brand new undertaking named “MongoDBTest” and we’ll select a console software. Now, we have to add the references for MongoDB. We will use the NuGet package deal supervisor choice.

Then, browse for “MongoDB” and set up the next two references. Now ,the appliance is nicely set for utilizing MongoDB. Let’s begin coding…

First, we’ll create a website class named “Worker” as follows:

public class Worker
    {
        [BsonId]
        public string EmployeeNo { get; set; }
        public string FirstName  { get; set; }
        public string LastName { get; set; }
        [BsonIgnoreIfNull]
        public int? Age { get; set; }
    }

Right here, we use “[BsonId]” which is the default ID property for MongoDB, and the “[BsonIgnoreIfNull]” token is to keep away from the null value-adding.

Second, we’ll create a Context class inside this system.cs class.

class EmployeeContext
    {
        non-public MongoDatabase db;
        public EmployeeContext()
        {
            MongoClient consumer = new MongoClient();
            var server = consumer.GetServer();
            this.db = server.GetDatabase("HR");
            var assortment = db.GetCollection<Worker>("Worker");
        }

        public MongoCollection<Worker> Workers => db.GetCollection<Worker>("Worker");
    }

Right here, we now have entry to the “HR” database, and we’ll do some actions within the “Worker” collections. Now, We are going to replace or add data to our MongoDB.

class Program
    {
        static void Fundamental(string[] args)
        {
            EmployeeContext ctx = new EmployeeContext();
            Worker aEmployee = new Worker
            {
                EmployeeNo = "B0234235",
                FirstName = "Ahsan",
                LastName = "Rupom",
                Age = 28
            };
            
            ctx.Workers.Save(aEmployee);
            aEmployee = ctx.Workers.AsQueryable().First();

            Console.WriteLine("EmployeeNo  : " + aEmployee.EmployeeNo);
            Console.WriteLine("FirstName : " + aEmployee.FirstName);
            Console.WriteLine("LastName : " + aEmployee.LastName);
            Console.WriteLine("Age : " + aEmployee.Age);
        }
    }

So, as we will see, it is extremely straightforward to combine MongoDB into the visual studio. Ensure that your MongoDB server is operating. I hope you loved this quite simple MongoDB integration with visible studio and C #. Click on right here to obtain the supply. Thanks.

MongoDB with windows Console Application (Fundamental)

Tags:

Anonymous Guest

An anonymous author is a writer who conceals their true identity when publishing content, often for privacy, security, or confidentiality reasons. This approach is often used in technical articles to focus on the information being presented rather than the author's personal identity.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *