2017-05-26 102 views
0

我是C#和mongoDB的新成員。我寫了一個簡單的代碼來連接vs到MongoDB。但是當我運行程序我得到這個錯誤「當連接到MongoDB時發生mscorlib.dll中發生'System.IO.FileNotFoundException'

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll 

Additional information: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. 

這是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using MongoDB.Driver; 
using MongoDB.Bson; 
using MongoDB.Driver.GridFS; 

namespace MongoDbTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
    { 


     var connectionString = "mongodb://localhost:27017"; 

     MongoClient oClient = new MongoClient(connectionString); 

     MongoServer oServer = oClient.GetServer(); 

     MongoDatabase db = oServer.GetDatabase("dblearnfiles"); 

     MongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); 

     foreach (var item in collection.FindAll()) 
     { 
      Console.WriteLine(item.ToString()); 
     } 
     Console.WriteLine("LearnFiles"); 
     Console.ReadKey(); 

    } 
} 
} 

我在這一行有錯誤:

MongoClient oClient = new MongoClient(connectionString); 

OCLIENT爲空!

回答

0

解決那 我改變這段代碼我nto:

  var client = new MongoClient(); 
     var db = client.GetDatabase("dblearnfiles"); 
     var coll = db.GetCollection<BsonDocument>("user"); 

     foreach(var item in coll.Find(new BsonDocument()).ToList()) 
     { 
      Console.WriteLine(item); 
     } 
     Console.ReadKey(); 

和工作。

相關問題