LiamB
LiamB

Reputation: 18606

MongoDB Connection Management

I'm using the standard .net driver (http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial) and am interested to know how we can use the driver to manage connections in a similar way to how we do on our SQL DB's.

Here is how we connect to our SQL DB

public static SqlConnection GetOpenConnection()
        {
            var ConnectionStrO = ConfigurationManager.ConnectionStrings[_ConnectionStringName];
            var connection = new SqlConnection(ConnectionStrO.ConnectionString);
            connection.Open();
            return connection;  
        }

Then to use that connection at any point in the app, we simply call

using(var CurConnection = Database.GetOpenConnection())
{

    //Use connection here.

}

Are there any example of how we can do a similar thing with MongoDB and manage the connection like above?

Or any other suggestions on managing connections with the MongoDb driver?

Upvotes: 0

Views: 1181

Answers (1)

Luke
Luke

Reputation: 8407

Copied from http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-TheC%23Driver

public static function MongoServer GetConnection() {
    var url = MongoUrl.Create(ConfigurationManager.ConnectionStrings[_ConnectionStringName]);
    var server = MongoServer.Create(url);
    return server;
}

Upvotes: 1

Related Questions