SIGN UP MEMBER LOGIN:    
ARTICLE

How to Connect a Mongo DB Server Through C# .Net Using C# Driver

Posted by Shirsendu Nandi Articles | ADO.NET in C# May 14, 2011
In this article I will describe how to connect a Mongo db server in C#.Net and after that how to create the collection in the Mongo db.
Reader Level:
Download Files:
 

In this article I will describe how to connect a Mongo db server in C#.Net and after that how to create the collection in the Mongo db.

Before beginning the coding, as we all know we have to open the "mongogod.exe" application.

Step 1: First create a console application named "Mongocreateserver".

Step 2:

After that paste the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;
using System.Collections;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Builders;
using MongoDB;

namespace mongocreateserver
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "mongodb://192.168.40.27";
            var server = MongoServer.Create(connectionString);
            if (server.State == MongoServerState.Disconnected)
                server.Connect();
            var OneConsoleDB = server.GetDatabase("One");
            if (!OneConsoleDB.CollectionExists("NextMessages"))
                OneConsoleDB.CreateCollection("NextMessages", null);
            var NextMessages = OneConsoleDB.GetCollection("NextMessages");
            server.Disconnect();
            Console.WriteLine("Server get connected in to this" + connectionString + "Server");
            server.GetDatabaseNames();
            server.Disconnect();
            Console.ReadLine();
        }
    }
}

See here we are defining the connection string.

After that we are creating the server with the connection string which means specify my system IP.

After that we are creating the server connection.

Then we are creating the Database named "One".

After that we are creating the collection name "NextMessages".

Finallly we are disconnecting from the server.

So when we run the application it will look like:

Severconenctinmongodb.jpg
 
Conclusion:

So in this article we have seen how to create a connection to the Mongo db and after that create the collection in that Mongo db.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor