Example C# driver code?

1,807 views
Skip to first unread message

Kevin Burton

unread,
Dec 30, 2012, 11:56:07 PM12/30/12
to mongod...@googlegroups.com
I inherited some code that now when I compile the compiler issues warnings. I have read help files but they seem like trying to solve the problem with an encyclopedia. There is alot of information but now examples, I have the following "legacy" code

            string mongoConnectionString = ConfigurationManager.AppSettings["MongoConnectionString"];
            MongoServer server = MongoServer.Create(mongoConnectionString);
            
            MongoDatabase db = null;
            if (!server.DatabaseExists("database"))
            {
                db = MongoDatabase.Create(server.Settings, "database");
            }
            else
            {
                db = server.GetDatabase("database");
            }

The error/warnings are as follows:

1>C:\Program.cs(2635,34,2635,75): warning CS0618: 'MongoDB.Driver.MongoServer.Create(string)' is obsolete: 'Use MongoClient.GetServer instead.'
1>C:\Program.cs(2640,22,2640,73): warning CS0618: 'MongoDB.Driver.MongoDatabase.Create(MongoDB.Driver.MongoServerSettings, string)' is obsolete: 'Use MongoClient, GetServer and GetDatabase instead.'

So one of the problems is that Create is obsolete and I should use GetServer instead. The problem is that there is no static method GetServer on the MongoClient class. So that says I need to create a MongoClient instance. How is that done? Next error indicates that MongoDatabase.Create is obsolete and I should use MongoClient, GetServer, and GetDatabase instead. Even if I knew how to create a client then a server I still would only be getting a database not creating it. Any help with this conversion would be greatly appreciated. Thank you.

craiggwilson

unread,
Dec 31, 2012, 9:41:20 AM12/31/12
to mongod...@googlegroups.com
You can see the C# documentation here: http://www.mongodb.org/display/DOCS/CSharp+Language+Center.  The quickstart and the tutorial are the two you should read.  Below is the supported method for what you are attempting to do.

            var connectionString = ConfigurationManager.AppSettings["MongoConnectionString"];
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            var database = server.GetDatabase("database");
Reply all
Reply to author
Forward
0 new messages